简体   繁体   English

Excel VBA:在范围名称的For循环中使用索引号

[英]Excel VBA: using the index number in a For loop in a range name

I would like to use the index number in a For loop in a range name. 我想在范围名称的For循环中使用索引号。 Please see below. 请看下面。 I am receiving an "Object required" error. 我收到“需要对象”错误。 Thank you for your help. 谢谢您的帮助。

Sincerely, 真诚的

Dante 但丁

Dim i As Integer
For i = 2 To 25
    If Not Intersect(range_1, range_ & i) Is Nothing Then
        MsgBox ("Error!  range_1 and range_" & i & " overlap!")
    End If
Next i

You cannot append numbers to the variable names. 您不能在变量名称后附加数字。 The Intersect function takes ranges objects, so you might want to write: Intersect函数采用range对象,因此您可能需要编写:

Dim i As Integer
For i = 2 To 25
    If Not Intersect(Range("range_1"), Range("range_" & i)) Is Nothing Then
        MsgBox ("Error!  range_1 and range_" & i & " overlap!")
    End If
Next i

Of course, I'm assuming that you have properly named your ranges with "range_1", "range_2" etc. 当然,我假设您已使用“ range_1”,“ range_2”等正确命名了您的范围。

EDIT (by Doug): Here's a version that ensures you are checking in the correct sheet. 编辑(作者道格):这是确保您签入正确图纸的版本。 In this case it's set to ActiveSheet, but you can adjust that. 在这种情况下,它设置为ActiveSheet,但是您可以进行调整。 Note that this checks for named ranges actually defined in the worksheet, not ranges set as variables in your code: 请注意,这将检查工作表中实际定义的命名范围,而不是在代码中设置为变量的范围:

Sub CheckIntersect()
Dim ws As Worksheet
Dim i As Long

Set ws = ActiveSheet 'adjust if necessary
For i = 2 To 25
    If Not Intersect(ws.Range("range_1"), ws.Range("range_" & i)) Is Nothing Then
        MsgBox ("Error!  range_1 and range_" & i & " overlap!")
    End If
Next i
End Sub

I leave the previous answer since, in case of named ranges, it's correct and has also been improved by someone else, so users that look for problems with named ranges might find it useful. 我留下了先前的答案,因为在命名范围的情况下,它是正确的,并且已经得到其他人的改进,因此,查找命名范围问题的用户可能会发现它很有用。

On the other hand, what you probably meant is that you assigned the range to a variable , such as (of course this is just an example, i cannot know how you made your assignment) 另一方面,您可能的意思是您将范围分配给了变量 ,例如(当然,这只是一个例子,我不知道您是如何进行分配的)

Set range_1 = Range("A1")
Set range_2 = Range("A2")
'...
Set range_25 = Range("A25)

Unfortunately you cannot loop like that through variable names. 不幸的是,您不能通过变量名那样循环。 But what you can do is creating a collection of ranges, so that you will be able to access them lately by using an index to iterate through them: 但是您可以做的是创建一个范围集合,以便您以后可以通过使用索引来遍历它们来访问它们:

Dim ranges As Collection: Set ranges = New Collection

ranges.Add range_1
ranges.Add range_2
'...
ranges.Add range_25

More in general, add the range to the collection every time you define it. 通常,每次定义范围时,将范围添加到集合中。 Please note that even if this range will change at a later stage, the range inside your list will change as well because the Add method creates a shallow copy of the object (in other words, it is still pointing to the original object). 请注意,即使此范围稍后会更改,列表中的范围也会更改,因为Add方法会创建对象的浅表副本 (换句话说,它仍指向原始对象)。

Now, the collection of ranges is storing your range objects and you will be able to access them by index. 现在,范围的集合正在存储您的范围对象,您将能够通过索引访问它们。 For example, in your specific case: 例如,在您的特定情况下:

For j = 2 To 25
    If Not Intersect(ranges(1), ranges(j)) Is Nothing Then

As a good practice, remember that every time you will want to iterate through a "humanly indexable" series of objects/variables as in your case, you will often might want to cast them into a collection because the name of the variables cannot be create/changed from the code itself. 作为一种好习惯,请记住,每次像情况一样要遍历“可人工索引”的对象/变量系列时,通常都可能希望将它们强制转换为集合,因为无法创建变量的名称。 /已从代码本身更改。

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

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