我是新來的excel VBA,雖然我嘗試了努力但沒有運氣。Excel VBA查找問題
問題陳述:
我有源(白色)行和目的地(顏色黃色)行的片材,對於每個源存在下一行對應的目的地的行。我必須查找一個應用程序名稱,該名稱在開始時會被用戶輸入,並將在第6列中搜索整個工作表(超過10000行),並且如果在源目錄行中找到目標行,還必須提取源行在sheet2中。
而且一個單元格中可能有很多應用程序名稱,所以它應該修剪該單元格中的所有其他應用程序名稱並僅保留搜索到的應用程序名稱。
下面是部分代碼我想:
Sub GetInterfaceCounts()
Dim RANGEBOTTOM As String
Dim cell
Dim strAction As String
Dim intAdd As Integer
Dim strName As String
intAdd = 0
RANGEBOTTOM = "G700"
strName = InputBox(Prompt:="Please enter the application name.", _
Title:="Application Name", Default:="Application")
For Each cell In Range("G2:" & RANGEBOTTOM)
strAction = cell.Value
If InStr(1, strAction, strName) <> 0 Then
intAdd = intAdd + 1
End If
Next
MsgBox "Total number of " & strName & " counts are :" & CStr(intAdd)
GetMS4AppInventory (strName)
End Sub
Sub GetMS4AppInventory(strName As String)
Dim strAction
Dim intAdd As Integer
Dim RowIndex As Integer
RowIndex = 0
Sheets("Sheet1").Select
'For Each cell In Range("G2:G700")
With Worksheets("Sheet1").Range("G2:G700")
Set strAction = .Find(strName, LookIn:=xlValues)
'strAction = cell.Value
If Not strAction Is Nothing Then
Do
If InStr(1, strAction, strName) <> 0 Then
Rows(strAction.Row).Select
Selection.Copy
Sheets("MS4Inventory").Select
Rows(RowIndex + 1).Select
Selection.Insert Shift:=xlDown
Rows(RowIndex + 2).Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(RowIndex + 3, 1).Select
End If
Set strAction = .FindNext(strAction) //gets hanged here go to infinite loop
Loop While Not strAction Is Nothing
End If
End With
End Sub
如果有人可以幫助我,這將是偉大的其他人工手動操作的庫存seggregation會吸我。
問候,
維傑
輸入表的結構是什麼? – 2011-05-16 19:24:48
'Range.Offset'和'Range.Resize'是你的朋友,而不是用'G2:G700'來引用多個單元格。使用'Range.Value'將值獲取到變量數組中,然後逐個搜索數組而不是單元格。 – ja72 2011-05-16 19:49:37