2017-08-01 99 views
0

現在我的代碼複製&粘貼所有數據,沒有任何特定的選擇標準。我試圖從特定文件中將特定信息複製並粘貼到活動工作表中。我相信有一些功能可以幫助我解決這個問題。任何幫助都會很棒。謝謝。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 

pseudo code

回答

0

但是你已經有張貼紙張上的答案!

要檢查另一個字符串中的字符串,請使用InStr()功能:

If InStr(CStr(file), "PartOfFileName") <> 0 Then 
    'file found, copy specific parts to that file 
End If 

Here是關於如何使用功能更多一些很好的例子。

建議您「調暗」所有變量以更好地控制您的代碼。