VBA-填充一个来自许多单独单元的值的数组

发布于 2025-02-04 06:54:53 字数 291 浏览 2 评论 0原文

我需要一个简单的解决方案,以将一个单元格中指定的单元格的数字输入到一个数组中。

例如,A1充满了特定文本:a3b4d10f1:f45

我需要这些值VBA数组中的单元格 - 我该如何进行代码中的所有内容?我尝试了类似的事情:

Dim x() As Array
x = Range(Range("A1").Value).Value

但是它显然不起作用:)

I need a simple solution for inputing numbers from the cells specified in one cell into an array.

E.g. A1 is filled with particular text: A3, B4, D10, F1:F45

I need values from these cells in the array in VBA - how can I do this withput specifing everything in code? I've tried something like:

Dim x() As Array
x = Range(Range("A1").Value).Value

However it doesn't work apparently :)

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

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

发布评论

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

评论(2

乱了心跳 2025-02-11 06:54:53

您希望array()才能捕获多个值,例如:

Sub generateArray()
    Dim testArray As Variant
    testArray = Array(Cells(1, 1).Value, Cells(2, 2).Value)
    Debug.Print testArray(0)
    Debug.Print testArray(1)
End Sub

关于在数组中添加多个数组的使用,您将需要创建一个复合数组尺寸,该维度尺寸为两个以前的ubound(),或简单地redim参数,并继续添加到新数组中。请参阅有关后者的更多信息。

You want the Array() to capture multiple values, e.g.:

Sub generateArray()
    Dim testArray As Variant
    testArray = Array(Cells(1, 1).Value, Cells(2, 2).Value)
    Debug.Print testArray(0)
    Debug.Print testArray(1)
End Sub

Regarding the use of adding multiple arrays to your array, you will want to create a composite array dimensioned to your two previous ubound(), or simply redim parameters and keep adding to the new array. See this post for more information on the latter.

enter image description here

野侃 2025-02-11 06:54:53

由于a3b4d10f1:f45是不合格的,大概是您想要一个1D数组,您可以使用一个循环:

Dim rng As Range
Set rng = Range(Range("A1").Value)
    
Dim arr As Variant
ReDim arr(1 To rng.Count)
    
Dim counter As Long
counter = 1
    
Dim cell As Range
For Each cell In rng
    arr(counter) = cell.Value
    counter = counter + 1
Next

Since A3, B4, D10, F1:F45 are non-contiguous, and presumably you want a 1D array, you can use a loop:

Dim rng As Range
Set rng = Range(Range("A1").Value)
    
Dim arr As Variant
ReDim arr(1 To rng.Count)
    
Dim counter As Long
counter = 1
    
Dim cell As Range
For Each cell In rng
    arr(counter) = cell.Value
    counter = counter + 1
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文