VBA: Why isn't variable working in named range?

后端 未结 3 1491
遥遥无期
遥遥无期 2021-01-20 19:14

This is part of a larger code, but this snippet isn\'t working. I\'m trying to just set two cells equal to each other, but it\'s not working. When I use the .Range(\"v1_copy

相关标签:
3条回答
  • 2021-01-20 19:18

    Range will only apply to the currently active worksheet unless you add the Worksheet reference at the time of assignment (not at the time usage as you have done).

    Since you are access a different worksheet, your second assignment will fail.

    myCopyRange = myWS1.Range("v1_copy") 
    myPasteRange = myPasteRange = Range("v1_paste")
    

    See the Range Object Documentation:

    When it's used without an object qualifier (an object to the left of the period), the Range property returns a range on the active sheet ... Use the Activate method to activate a worksheet before you use the Range property without an explicit object qualifier

    If you are trying to refer to NamedRanges and not a name held in a VBA variable, you need to change the way you are accessing the range.

    Workbook-scope NamedRanges do not use worksheet reference - since they don't apply to a worksheet, they apply at the workbook level. If you need to add a qualifier, you add the workbook:

    Range("MyBook.xls!MyRange")
    

    If you are referring to Worksheet-scope NamedRange, you need a qualifier, but it goes inside the quotations:

    Range("Sheet1!Sales")
    
    0 讨论(0)
  • 2021-01-20 19:22

    You're missing the Set keyword for your Range object reference assignments to myCopyRange and myPasteRange.

    But for retrieving a named range, the best place to go if you want fully explicit code that does what it says and says what it does, is to dereference the Name from the appropriate Names collection.

    If the names are workbook-scoped, qualify with a Workbook object - here a book object variable, but depending on needs ActiveWorkbook or ThisWorkbook work just as well:

    Set myRange = book.Names("name").RefersToRange
    

    If the names are worksheet-scoped, qualify with a Worksheet object - here a sheet object variable, but ActiveSheet works just as well:

    Set myRange = sheet.Names("name").RefersToRange
    

    That way the code won't break if the workbook is renamed, or if the user changes the "tab name" of the sheet. It won't break as long as the name exists in the queried Names collection.


    'myWS2.Range("v1_paste").Value = myWS1.Range("v1_copy").Value
    ' This line works, but the below line doesn't
     myWS2.myPasteRange.Value = myWS1.myCopyRange.Value
    ' This should be the exact same, just substituting the variable, but doesn't work
    

    This should be the exact same - no. myWS1.myCopyRange is illegal: myWS1 is a Worksheet object: the Worksheet interface doesn't have a myCopyRange member, hence method or data member not found.

    Since myCopyRange is a Range object, it knows about its Parent which is the Worksheet it belongs to: there's no need to qualify it... and there's no need to dereference it again either - this is enough:

    myPasteRange.Value = myCopyRange.Value
    
    0 讨论(0)
  • 2021-01-20 19:39

    Properly create ranges by using Set and don't refer to worksheets before them. Workbook-scoped ranges don't need to be tied to any worksheet.

    Sub copy_paste_test()
    
        Dim myCopyRange As Range
        Dim myPasteRange As Range
    
        Set myCopyRange = Range("v1_copy")
        Set myPasteRange = Range("v1_paste")
    
        Range("v1_paste").Value = Range("v1_copy").Value
        'myPasteRange.Value = myCopyRange.Value
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题