简体   繁体   中英

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
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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