[英]VBA: Copy-Paste from multiple worksheets multiple workbooks
我有一個主文件和源文件。
主文件:多個工作表,列A到J
源文件:多個工作表,列A到E
我設法與
1)在提示窗口中選擇源文件
2)通過匹配工作表中A列中的值,從源文件中復制C,D,E列並將其粘貼到C,D,E列
我現在嘗試在多個工作表中重復此復制粘貼操作。
對於主文件和源文件,工作表名稱的范圍從“ F.01,F.02到F.10”,“ T.01,T.02到T.10”,“ IS.01到IS.05”。
我是VBA的新手-通過檢查所有論壇和論壇,我設法完成了下面的代碼。
確實需要您的幫助,以幫助我根據上面所述的工作表范圍對代碼進行某種復制,使其重復復制。
將源“ F.01”發送到母版“ F.01”,然后繼續
Sub CommandButton2_Click()
Dim fileDialog As fileDialog
Dim strPathFile As String
Dim strFileName As String
Dim strPath As String
Dim dialogTitle As String
Dim wbSource As Workbook
Dim rngToCopy As Range
Dim rngRow As Range
Dim rngDestin As Range
Dim lngRowsCopied As Long
Dim dic As Object
Dim ky As Variant
Dim c As Variant
Dim cel As Range
Dim x As String
Dim tgt As Range
Dim FR As Long
Dim SSheetList As Variant
Dim MSheetList As Variant
dialogTitle = "Navigate to and select required file."
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = "C:\Users\User\Documents"
'.InitialFileName = ThisWorkbook.Path & "\" 'Alternative to previous line
.AllowMultiSelect = False
.Filters.Clear
.Title = dialogTitle
If .Show = False Then
MsgBox "File not selected to import. Process Terminated"
Exit Sub
End If
strPathFile = .SelectedItems(1)
End With
Set wbSource = Workbooks.Open(Filename:=strPathFile)
SSheetList = Array("F.01", "F.02")
MSheetList = Array("F.01", "F.02")
Set sh1 = ThisWorkbook.Sheets(MSheetList)
Set sh2 = wbSource.Sheets(SSheetList)
Application.ScreenUpdating = False
If sh1 Is sh2 Then
For Each c In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(xlUp))
FR = 0
On Error Resume Next
FR = Application.Match(c, sh1.Columns(1), 0)
On Error GoTo 0
If FR <> 0 Then sh1.Range("C" & FR).Value = c.Offset(, 2)
If FR <> 0 Then sh1.Range("D" & FR).Value = c.Offset(, 3)
If FR <> 0 Then sh1.Range("E" & FR).Value = c.Offset(, 4)
Next c
End If
wbSource.Close SaveChanges:=False
Set fileDialog = Nothing
Set rngRow = Nothing
Set rngToCopy = Nothing
Set wbSource = Nothing
Set rngDestin = Nothing
'MsgBox "The data is copied"
End Sub
我已經修改了您的代碼,請嘗試以下操作:
Sub CommandButton2_Click()
Dim fileDialog As fileDialog
Dim strPathFile As String
Dim strFileName As String
Dim strPath As String
Dim dialogTitle As String
Dim wbSource As Workbook
Dim wbMaster As Workbook
Dim wsSource, wsMaster As Worksheet
Dim rngToCopy As Range
Dim rngRow As Range
Dim rngDestin As Range
Dim lngRowsCopied As Long
Dim dic As Object
Dim ky As Variant
Dim c As Variant
Dim cel As Range
Dim x As String
Dim tgt As Range
Dim FR
Set wbMaster = ThisWorkbook.Name
dialogTitle = "Navigate to and select required file."
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = "C:\Users\User\Documents"
'.InitialFileName = ThisWorkbook.Path & "\" 'Alternative to previous line
.AllowMultiSelect = False
.Filters.Clear
.Title = dialogTitle
If .Show = False Then
MsgBox "File not selected to import. Process Terminated"
Exit Sub
End If
strPathFile = .SelectedItems(1)
End With
''''''''
'this is what I've modified:
Set wbSource = Workbooks.Open(Filename:=strPathFile)
For Each wsSource In wbSource.Sheets
For Each wsMaster In wbMaster.Sheets
If wsSource.Name = wsMaster.Name Then
t = 2
Do Until t = wbSource.wsSource.Cells(Rows.Count, 1).End(xlUp).Row
c = wbSource.wsSource.Cells(t, 1).Value
Set FR = wbMaster.wsMaster.Columns(1).Find(What:=c)
If Not FR Is Nothing Then
Do Until FR Is Nothing
wbMaster.wsMaster.Cells(FR.Row, 3).Value = wbSource.wsSource.Cells(t, 2).Value
'Or
'wbMaster.wsMaster.Cells(FR.Row, 3).Copy
'wbSource.wsSource.Cells(t, 2).PasteSpecial Paste:=xlPasteValues
wbMaster.wsMaster.Cells(FR.Row, 4).Value = wbSource.wsSource.Cells(t, 3).Value
wbMaster.wsMaster.Cells(FR.Row, 5).Value = wbSource.wsSource.Cells(t, 4).Value
Loop
End If
t = t + 1
Loop
End If
Next wsMaster
Next wsSource
''''''''
wbSource.Close SaveChanges:=False
Set fileDialog = Nothing
Set rngRow = Nothing
Set rngToCopy = Nothing
Set wbSource = Nothing
Set rngDestin = Nothing
'MsgBox "The data is copied"
End Sub
找到了解決方案
Sub CommandButton2_Click()
Dim fileDialog As fileDialog
Dim strPathFile As String
Dim dialogTitle As String
Dim wbSource As Workbook, Mwb As Workbook
Dim Ws As Worksheet, Mws As Worksheet
Dim Cl As Range
Dim FR As Long
Set Mwb = ThisWorkbook
dialogTitle = "Navigate to and select required file."
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName ="C:\Users\User\Documents"
.AllowMultiSelect = False
.Filters.Clear
.Title = dialogTitle
If .Show = False Then
MsgBox "File not selected to import. Process Terminated"
Exit Sub
End If
strPathFile = .SelectedItems(1)
End With
Application.ScreenUpdating = False
Set wbSource = Workbooks.Open(FileName:=strPathFile)
For Each Ws In wbSource.Worksheets
If ShtExists(Ws.name, Mwb) Then
Set Mws = Mwb.Sheets(Ws.name)
For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
FR = 0
On Error Resume Next
FR = Application.Match(Cl.Value, Ws.Columns(1), 0)
On Error GoTo 0
If FR <> 0 Then Mws.Range("C" & FR).Value = Cl.Offset(, 2)
If FR <> 0 Then Mws.Range("D" & FR).Value = Cl.Offset(, 3)
If FR <> 0 Then Mws.Range("E" & FR).Value = Cl.Offset(, 4)
Next Cl
End If
Set Mws = Nothing
Next Ws
wbSource.Close SaveChanges:=False
End Sub
Public Function ShtExists(ShtName As String, Optional Wbk As Workbook) As Boolean
If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
On Error Resume Next
ShtExists = (LCase(Wbk.Sheets(ShtName).name) = LCase(ShtName))
On Error GoTo 0
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.