简体   繁体   English

您如何在Excel VBA中引用范围?

[英]How do you reference Ranges in Excel VBA?

I have a named range in Excel that has some data I want to process in a subroutine or function, however I'm having a hard time figuring out how to reference individual parts of the range. 我在Excel中有一个命名范围,它有一些我希望在子程序或函数中处理的数据,但是我很难弄清楚如何引用该范围的各个部分。 I'm trying to use the Cells function, but it doesn't seem to be working. 我正在尝试使用Cells函数,但它似乎不起作用。

Here is a snippet of my code: 这是我的代码片段:

Sub sub1()
x = Range("Test")

For Each i In x
    v = x.Cells(i, 1)
    calculate(v)
Next i

End Sub

As of right now, the named range "Test" is named for A1:A18 and the calculate function hasn't been written. 截至目前,命名范围“Test”以A1:A18命名,并且尚未编写计算函数。 Using the debug tools in VBA, the program throws an error at the v = x.Cells(i,1) line: "Run-time error '424': Object required" 使用VBA中的调试工具,程序在v = x.Cells(i,1)行引发错误:“运行时错误'424':需要对象”

There seems to be something obvious I'm missing. 似乎有一些显而易见的东西我不见了。 Perhaps there's a way to convert a named range to an array? 也许有一种方法可以将命名范围转换为数组?

Everything is in Siddharth Rout answer ; 一切都在Siddharth Rout回答; I'll just go into more detailed explanation. 我将进一步详细解释。

The flaw is that x is undeclared, therefore VB uses a Variant . 缺点是x未声明,因此VB使用Variant You need x to be a Range type, so you can refer to its Cells property. 您需要x作为Range类型,因此您可以引用其Cells属性。 Therefore you should declare it so. 因此你应该这样宣布。

Insert Dim x as Range as first line of sub1 . Dim x as Range插入为sub1第一行。 You will then have to use the Set keyword to assign to x : Set x = Range("test") 然后,您必须使用Set关键字分配给x: Set x = Range("test")

Set is the syntax used to assign objects. Set是用于分配对象的语法。

Also, the Cells property is the default property of Range . 此外, Cells属性是Range的默认属性。 Therefore it is not required to write it explicity. 因此,不需要明确地写它。

For each i in x 

assigns to ia Range object which is the i'th cell of range x. 赋予ia Range对象,它是范围x的第i个单元格。 So you can apply calculate on i. 所以你可以在i上应用计算。

Now one last thing : Calculate can be applied to a single cell or to a Range. 现在最后一件事: Calculate可以应用于单个单元格或范围。 Therefore, x.Calculate would work (whithout the need for a loop). 因此,x.Calculate可以工作(不需要循环)。

This gives : 这给出了:

Sub sub1()
  Dim x as Range
  Dim i as Range
  Set x = Range("Test")

  For Each i In x
      i.Calculate
  Next i

End Sub

or simpler: 或者更简单:

Sub sub1()
  Dim x as Range
  Set x = Range("Test")

  x.Calculate

End Sub

if "Calculate" is a user-function : 如果“计算”是用户功能:

Sub sub1()
  Dim x as Range
  Dim i as Range
  Set x = Range("Test")

  For Each i In x
      Call CalculateMyPurpose(i)
  Next i

End Sub

Sub CalculateMyPurpose(myCell as Range)
  'Do something on myCelll
End Sub

NB : I would suggest you declare all variables. 注意:我建议您声明所有变量。 This will make sure they have the type you want them to. 这将确保它们具有您想要的类型。 To enforce variable declarations, write Option Explicit as 1st line of the module. 要强制执行变量声明,请将Option Explicit写为模块的第1行。

You do not need to use Cells :) 你不需要使用Cells :)

Is this what you are doing (UNTESTED)? 这是你在做什么(UNTESTED)?

Sub sub1()
    Dim cl As Range, nmRange As Range

    Set nmRange = Range("Test")

    For Each cl In nmRange
        calculate (cl)
    Next cl
End Sub

'~~> Some sub
Sub calculate(rng As Range)

End Sub

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

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