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

白昼怎懂夜的黑 提交于 2020-01-11 09:35:26

问题


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"), the code runs, but when I name that range and place it as a variable (myCopyRange), the code doesn't run and I get the error: Compile error: Method or data member not found. Any help would be appreciated!

Sub copy_paste_test()

Dim myCopyRange As Range
Dim myPasteRange As Range
Dim myWS1 As Worksheet
Dim myWS2 As Worksheet

Set myWS1 = Sheets("Sheet1")
Set myWS2 = Sheets("Sheet2")

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

'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

End Sub

回答1:


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



回答2:


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")



回答3:


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


来源:https://stackoverflow.com/questions/50997029/vba-why-isnt-variable-working-in-named-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!