[英]Excel VBA find all values in row and save different column values to variables
我已經做了很多搜索,找不到除我以外符合我的情況或可以修改的代碼。
查看下面的電子表格。 我希望用戶輸入OrderNumber
然后在Column A
搜索該編號的每個值。 因為它確實希望它將ItemNumber
和QtyOrdered
復制到兩個不同的變量中,以便稍后將它們放入文本框。
我希望它將信息“堆疊”到變量中,以便類似ItemNumValues = ItemNumValues + Cell.Value
我試圖修改其他人的代碼(“他們的代碼”),但出現不匹配類型錯誤。 其余代碼工作正常。 腳本中有一些以前功能中未使用的跟蹤元素,我只是還沒有刪除它們。
'***********************************************************
'********** Their Code Follows *****************
'***********************************************************
Dim numentries As Integer
Dim i As Integer
'***********************************************************
'Get number of entries
numentries = Worksheets(Sheet1).UsedRange.Rows.Count
'*************************************************************
'Run loop to cycle through all entries (rows) to copy
For i = 1 To numentries
If (Worksheets("Sheet1").Cells(i + 2, 1).Value = InStr(1, Cell, OrderNumber, vbTextCompare)) Then
MsgBox Test
End If
Next i
End If
'***********************************************************
'********** End Their Code *****************
'***********************************************************
我建議使用多維數組。 如果您以前從未使用過數組,我強烈建議您仔細閱讀它們。
Sub GatherData()
Dim c As Range
Dim aGetData() As Variant 'This is our array
Dim i As Integer
Dim a As Integer
Dim iRowCount As Integer
Dim sRange As String
'Gather data
iRowCount = Worksheets("Sheet1").UsedRange.Rows.Count
For Each c In Range("A2:A" & iRowCount)
If c.Value = 636779 Then
ReDim Preserve aGetData(2, i) 'An array must have a set size but as we
'do not know how many order numbers will be found we have to 'resize'
'the array to account for how many we do find. Using "ReDim Preserve"
'keeps any data we have placed into the array while at the same time
'changing it's size.
For a = 0 To 2 'Our first index will hold each col of data that is why
'it is set to 2 (arrays start at a base of zero, so
'0,1,2 will be each col(A,B,C)
aGetData(a, i) = c.Offset(0, a) 'This gets each value from col A,B and C
Next a
i = i + 1 'Increment for array in case we find another order number
'Our second index "aGetData(index1,index2) is being resized
'this represents each order number found on the sheet
End If
Next c
'How to read the array
For i = 0 To UBound(aGetData())
For a = 0 To 2
Debug.Print aGetData(a, i)
Next a
Next i
End Sub
似乎OrderNumber
(列A
)已排序。 很好的消息(如果不是,則對它們進行排序;))。 這個簡單的函數將把ItemNumber
和QtyOrdered
帶入一個二維數組,其中每一行都是一對。
Function ArrItemQty(ByVal OrderNumber As Long)
With Worksheets("Sheet1").UsedRange.Offset(1)
.AutoFilter 1, OrderNumber
ArrItemQty= .Resize(, 2).Offset(, 1).SpecialCells(xlCellTypeVisible).value
.Parent.AutoFilterMode = False
End With
End Function
這是一些測試:
Sub Test()
Dim i As Long, j As Long, ar
ar = ArrItemQty(636779)
For i = LBound(ar, 1) To UBound(ar, 1)
Debug.Print
For j = LBound(ar, 2) To UBound(ar, 2): Debug.Print ar(i, j),: Next
Next
End Sub
ps請注意,結果數組基於1。 按照指示使用LBound
和UBound
是最安全的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.