VBA使用filedialog来搜索,然后复制并粘贴
问题描述:
现在我的代码复制&粘贴所有数据,没有任何特定的选择标准。我试图从特定文件中将特定信息复制并粘贴到活动工作表中。我相信有一些功能可以帮助我解决这个问题。任何帮助都会很棒。谢谢。VBA使用filedialog来搜索,然后复制并粘贴
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set wbb = ThisWorkbook
Set sh = wbb.Worksheets("Sheet1")
With fd
.Title = "Please select Job Folder"
.AllowMultiSelect = True
Err.Clear
FileChosen = fd.Show
If MsgBox("Files selected, continue?", vbYesNo) = vbNo Then Exit Sub
For i = 1 To fd.SelectedItems.Count
file = fd.SelectedItems(i)
Workbooks.Open Filename:=file, ReadOnly:=True
If file = "" Then Exit Sub
filesheet = "Sheet1"
ActiveWorkbook.Sheets(filesheet).Range("A1:A3").Copy
LastRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
sh.Cells(sh.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Close savechanges:=False
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub