简体   繁体   English

输入/输出值到数组中

[英]Input/output values into an array

EDIT: Updated question using some of the suggestions below. 编辑:使用以下一些建议更新了问题。 This produces weird output though. 但这会产生奇怪的输出。

Dim ProviderArray() As Variant

Sub GetProviderNumbers()
Dim InputRange As Range
Dim WorkRange As Range


Set InputRange = Range("ProviderList")

Set WorkRange = Application.Intersect(InputRange, ActiveSheet.UsedRange)

SizeOfArray = Application.WorksheetFunction.CountA(WorkRange)

ReDim ProviderArray(0 To SizeOfArray)

ProviderArray = WorkRange.Value

For r = 1 To UBound(ProviderArray, 1)
For C = 1 To UBound(ProviderArray, 2)
    Debug.Print r, C, ProviderArray(r, C)
Next C
Next r

End Sub

1 1 5555 2 1 4444654 3 1 654654 4 1 654654654 5 1 1 1 5555 2 1 4444654 3 1 654654 4 1 654654654 5 1
6 1 6 1
7 1 7 1
8 1 8 1
9 1 9 1
10 1 10 1
11 1 11 1
12 1 12 1
13 1 13 1
14 1 14 1
15 1 15 1
16 1 16 1
17 1 17 1
18 1 18 1
19 1 19 1

Could someone explain why this output? 有人可以解释为什么这个输出吗?

You can only use the one-line approach if you put the range into a 2-D array: you only have a 1-D array. 如果将范围放入二维数组中,则只能使用单行方法:只有一维数组。

You could do this: 您可以这样做:

Dim ProviderArray()
Set WorkRange = .Intersect(InputRange, ActiveSheet.UsedRange)

'This makes ProviderArray a 2-D array, dimension 1 = # rows,
'   dimension2 = #cols.  Both dimensions are 1-based.  
ProviderArray = WorkRange.value 

for r=1 to ubound(ProviderArray,1)
for c=1 to ubound(ProviderArray,2)
    debug.print r,c,ProviderArray(r,c)
next c
next r

Maybe something a bit simpler like: 也许有点简单:

Private Sub GetProviderNumbers()

    Dim InputRange() As Variant
    InputRange = Range("ProviderList")

    For Each i In InputRange
        Debug.Print i
    Next

End Sub

This captures a two-dimensional range and stores the values in a global two-dimensional array: 这将捕获一个二维范围,并将值存储在全局二维数组中:

Dim ProviderArray() As String
Sub MAIN()
Range("B2:C11").Name = "ProviderList"
Call GetProviderNumbers
End Sub
Sub GetProviderNumbers()
    ary = Range("Providerlist")
    ll = LBound(ary, 1)
    lm = LBound(ary, 2)
    ul = UBound(ary, 1)
    um = UBound(ary, 2)
    ReDim ProviderArray(ll To ul, lm To um)
    For i = ll To ul
        For j = lm To um
            ProviderArray(i, j) = ary(i, j)
        Next
    Next
End Sub

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

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