早上好,
我正试图找到一种方法:
但是,我想不出一种循环遍历列并获取这些值的方法,将它们存储在数组中。我已经浏览了Stack Overflow和google但尚未找到成功的解决方案。
提前感谢您的帮助。
Sub collectNums()
Dim eNumStorage() As String ' initial storage array to take values
Dim i as Integer
Dim j as Integer
Dim lrow As Integer
lrow = Cells(Rows.Count, "B").End(xlUp).Row ' The amount of stuff in the column
For i = lrow To 2 Step -1
If (Not IsEmpty(Cells(i, 2).Value)) Then ' checks to make sure the value isn't empty
i = eNumStorage ' I know this isn't right
Next i
If (IsEmpty(eNumStorage)) Then
MsgBox ("You did not enter an employee number for which to query our database. Quitting")
Exit Sub
End If
End Sub
答案 0 :(得分:3)
这是将列添加到数组的最简单方法:
Public Sub TestMe()
Dim myArray As Variant
Dim cnt As Long
myArray = Application.Transpose(Range("B1:B10"))
For cnt = LBound(myArray) To UBound(myArray)
myArray(cnt) = myArray(cnt) & "something"
Next cnt
For cnt = LBound(myArray) To UBound(myArray)
Debug.Print myArray(cnt)
Next cnt
End Sub
它从数组中的B1
到B10
获取值,并且可以为此数组添加“某些东西”。
Transpose()
函数采用单列范围并将其存储为具有一个维度的数组。如果数组在一行上,那么你需要一个双转置,使它成为一个单维数组:
With Application
myArray = .Transpose(.Transpose(Range("A1:K1")))
End With
答案 1 :(得分:2)
只需在Vityata上添加变体,这是最简单的方法。此方法仅向数组添加非空值。使用方法时,必须使用Redim声明数组的大小。
DSL.selectCount()