Excel VBA 中的范围数组

发布于 2024-08-07 23:24:19 字数 337 浏览 9 评论 0原文

好吧,我一直在努力处理一小段代码,似乎无法绕过它...... 我试图从一系列单元格中获取一个数组,但该数组显示为 1 个元素宽。
这是代码:

Dim item As Variant
MsgBox Range("D19:H19").Count    
item = Range("D19:H19").Value
MsgBox LBound(item) & " " & UBound(item)   

根据我的理解,项目应该包含一个二维数组......但是我得到以下结果 第一个 MsgBox 打印 5 第二个 MsgBox 打印 1 1

出了什么问题?

Well I've been struggling with the little bit of code and can't seem to get around it ...
I'm trying to get an array from a range of cells, the array however is showing up to be 1 element wide.
Well here's the code:

Dim item As Variant
MsgBox Range("D19:H19").Count    
item = Range("D19:H19").Value
MsgBox LBound(item) & " " & UBound(item)   

as per my understanding item should contain a 2D array... however I'm getting the following result
1st MsgBox prints 5
2nd MsgBox prints 1 1

What's going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云裳 2024-08-14 23:24:19

问题出在 LBound 和 UBound

jtolle 关于 LBound 和 UBound 的说法是正确的。

LBound(item, 2)

UBound(item, 2)

但是,项目不得作为数组变暗(您将收到错误)。

我想这就是你想要的

Dim item As Variant
MsgBox Range("D19:H19").Count
item = Range("D19:H19").Value

MsgBox LBound(item, 2) & " " & UBound(item, 2)

For i = LBound(item, 2) To UBound(item, 2)
  MsgBox item(1, i)
Next

The problem is in LBound and UBound

jtolle was correct about the LBound and UBound.

LBound(item, 2)

UBound(item, 2)

However, item must not be dimmed as an array (you'll get an error).

I think this is what you want

Dim item As Variant
MsgBox Range("D19:H19").Count
item = Range("D19:H19").Value

MsgBox LBound(item, 2) & " " & UBound(item, 2)

For i = LBound(item, 2) To UBound(item, 2)
  MsgBox item(1, i)
Next
呢古 2024-08-14 23:24:19

您的项目应按预期包含一个二维数组。如果您在代码中设置断点并查看 VBA 编辑器中的小“Locals”窗口,您应该会看到这一点。您对 LBound 和 UBound 的调用将获得第一维的边界。如果您调用 Lbound(item,2) 和 UBound(item,2),您应该得到您期望的 1 和 5。

编辑:
也就是说,一旦完成分配,item 看起来就像您可以这样声明的东西:

Dim item(1 to 1, 1 to 5)

VBA 编程的祸根之一是数组可以具有任意下限。所以你的所有代码都需要意识到这一点。

Your item should contain a 2-D array as expected. If you stick a breakpoint in your code and look at the little "Locals" window in the VBA editor, you should see that. Your calls to LBound and UBound are getting the bounds in the first dimension. If you call Lbound(item,2) and UBound(item,2), you should get 1 and 5 as you expect.

EDIT:
That is, once you've made the assignment, item would look like something you could have declared as such:

Dim item(1 to 1, 1 to 5)

One of the banes of VBA programming is that arrays can have arbitrary lower bounds. So all of your code needs to be aware of that.

顾忌 2024-08-14 23:24:19

这是正确的。即使您选择一组单元格,您仍然可以选择从数组中选择一个单元格(例如使用 Tab 键浏览该数组的项目)

.Value

只能获取当前单个选定单元格的内容。

如果您想要枚举数组,您可以调用 Range 对象的 .Cells() 方法

假设 D19 到 H19 包含“a”到“e” " 分别调用

Range("D19:H19").Cells(2)

会返回 "b"。请注意,这是一个基于一的数组,并且可以是二维的。 Cells() 最多采用 2 个参数来指定距选区原点的内部偏移量。

希望能澄清...问候

That's correct as is. Even if you select an array of cells, you still have the option to select one single cell out of the array (and step for example with tab through the items of this array)

.Value

only gets you the content of the currently single-selected cell.

if you want the enumeration of the array, you may call the .Cells()-method of the Range-object

Assuming that D19 until H19 contain "a" through "e" respectively, calling

Range("D19:H19").Cells(2)

returns you "b". Note that this is a one-based array and can be 2-dimensional. Cells() takes at most 2 parameters to specify the inner offset from the selection's origin.

hope that clarifies... regards

木落 2024-08-14 23:24:19

试试这个:

Dim item As Variant
MsgBox Range("D19:H19").Count
item = Application.Transpose(Range("D19:H19").Value)
MsgBox LBound(item) & " " & UBound(item)

Try this:

Dim item As Variant
MsgBox Range("D19:H19").Count
item = Application.Transpose(Range("D19:H19").Value)
MsgBox LBound(item) & " " & UBound(item)
愿与i 2024-08-14 23:24:19

例如,如果您想要一个一维数组,将其连接到 IN 子句,您应该转置您的范围。
我发现你必须为一行转置两次,为一列数据转置一次,如下所示:

Dim rngRow As Range, rngColumn As Range

Set rngRow = Sheets(1).Range("A1", "Z1")
Set rngColumn = Sheets(1).Range("A1", "A20")

Dim arrRowValues, arrColValues
arrRowValues = WorksheetFunction.Transpose(WorksheetFunction.Transpose(rngRow))
arrColValues = WorksheetFunction.Transpose(rngColumn)

Dim numList As String, stringList As String
numList = Join(arrRowValues, ",")
stringList = "'" & Join(arrColValues, "','") & "'"

值得一试。

if you want a 1D array, to join it for an IN clause, for example, you should transpose your range.
I've found you have to transpose twice for a row, once for a column of data like this:

Dim rngRow As Range, rngColumn As Range

Set rngRow = Sheets(1).Range("A1", "Z1")
Set rngColumn = Sheets(1).Range("A1", "A20")

Dim arrRowValues, arrColValues
arrRowValues = WorksheetFunction.Transpose(WorksheetFunction.Transpose(rngRow))
arrColValues = WorksheetFunction.Transpose(rngColumn)

Dim numList As String, stringList As String
numList = Join(arrRowValues, ",")
stringList = "'" & Join(arrColValues, "','") & "'"

worth a play.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文