【问题标题】:VBA use filedialog to search, then copy and pasteVBA使用filedialog搜索,然后复制粘贴
【发布时间】:2017-08-01 15:13:17
【问题描述】:

现在我的代码在没有任何特定选择标准的情况下复制和粘贴所有数据。我正在尝试根据名称将某些文件中的特定信息复制并粘贴到活动工作表中。我相信有一些我需要的功能可以帮助我解决这个问题。任何帮助都会很棒。谢谢。

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

【问题讨论】:

    标签: vba search filedialog


    【解决方案1】:

    但是你已经在张贴的表格上找到了答案!

    要检查另一个字符串中的字符串,请使用InStr() 函数:

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

    Here 是有关如何使用该功能的一些更好的示例。

    建议将所有变量“调暗”以更好地控制代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 2021-07-02
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多