简体   繁体   English

VBA:为什么变量在命名范围内不起作用?

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

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. 当我使用.Range(“ v1_copy”)时,代码运行,但是当我命名该范围并将其放置为变量(myCopyRange)时,代码无法运行,并且出现错误:编译错误:方法或数据找不到成员。 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

You're missing the Set keyword for your Range object reference assignments to myCopyRange and myPasteRange . 您没有为myCopyRangemyPasteRange Range对象引用分配分配Set关键字。

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. 但是,对于检索命名范围,如果您想要执行显示内容并声明其功能的完全显式代码, 则最好的方法是从适当的Names集合中取消引用Name

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: 如果名称是工作簿范围的名称,请使用一个Workbook对象(这里是一个book对象变量)进行限定,但根据需要, ActiveWorkbookThisWorkbook正常工作:

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: 如果名称是工作表范围的,请使用Worksheet对象(这里是工作sheet对象变量)进行限定,但ActiveSheet效果也一样:

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. 只要名称存在于查询的Names集合中,它就不会中断。


 '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 . myWS1.myCopyRange是非法的: myWS1是一个Worksheet对象: Worksheet接口没有myCopyRange成员,因此未找到方法或数据成员

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: 由于myCopyRange是一个Range对象,它知道它的Parent是在Worksheet属于:有没有必要限定它...而且也没有必要再任它取消引用-这就够了:

myPasteRange.Value = myCopyRange.Value

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 如果在没有对象限定符的情况下使用它(句点左边的一个对象),则Range属性将在活动工作表上返回一个范围...在不使用显式对象限定符的情况下使用Range属性之前,请使用Activate方法激活工作表。

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. 如果尝试引用NamedRanges而不是VBA变量中包含的名称,则需要更改访问范围的方式。

Workbook-scope NamedRanges do not use worksheet reference - since they don't apply to a worksheet, they apply at the workbook level. 工作簿范围的NamedRanges不使用工作表引用-由于它们不适用于工作表,因此它们适用于工作簿级别。 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: 如果您引用的是Worksheet-scope NamedRange,则需要一个限定符,但是它位于引号内:

Range("Sheet1!Sales")

Properly create ranges by using Set and don't refer to worksheets before them. 通过使用“ Set正确创建范围,并且不要在它们之前引用工作表。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM