我正在尝试将多个工作簿加载到同一个工作表中,这些工作簿将包含一个公共列标题。加载多个工作簿后,我想搜索我想要的行并将其粘贴到新工作表上。
到目前为止,我已经完成了搜索和粘贴部分,但是需要加载多个工作簿的部分非常难,有人可以帮我解决这个问题吗?感谢。
Sub SearchRowAndCopy()
Dim strSearch
strSearch = Application.InputBox("Please enter the search string")
x = 2
Do While Cells(x, 1) <> ""
If Cells(x, 2) Like "*" & strSearch & "*" Then
Worksheets("Sheet1").Rows(x).Copy
Worksheets("Sheet2").Activate
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet2").Rows(erow)
End If
Worksheets("Sheet1").Activate
x = x + 1
Loop
End Sub
答案 0 :(得分:1)
Sub GetFiles()
Dim fPath As String
Dim sFile As String
Dim wb As Workbook
fPath = "D:\Analysis\"
sFile = Dir(fPath & "*.xls*")
Do While Len(sFile) > 0
Debug.Print fPath & sFile
Set wb = Workbooks.Open(Filename:=sFile, ReadOnly:=True)
'...
'do your copying
'...
wb.Close False
sFile = Dir
Loop
End Sub