嗨,我尝试为我的问题寻找可能的解决方案,但我找不到我需要的确切代码。
我需要从具有不同工作表名称和不同列的两个不同工作簿中复制数据。我在从单个工作簿复制数据时使用了我的代码,但出现错误提示
“自动化错误”。
所以我需要做的是将数据从工作表名称复制Raw Data
到Arm Checklist
我的主工作表中,也命名为Raw Data
.
我需要从中复制的列Raw Data
是 fromA7:Q
和 toArm Checklist
是 from C3:D,G,E,H:J,K,M:Q
。此列中的数据需要合并到我的 MainWorkfileRaw Data
Sub SAMPLE()
Dim MainWorkfile As Workbook
Dim OtherWorkfile As Workbook
Dim OtherWorkfile2 As Workbook
Dim TrackerSht As Worksheet
Dim FilterSht As Worksheet
Dim FilterSht2 As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' set workbook object
Set MainWorkfile = ActiveWorkbook
' set the worksheet object
Set TrackerSht = MainWorkfile.Sheets("Raw Data")
With TrackerSht
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row with data in column "C"
.Range("A7:S7" & lRow).ClearContents
End With
Application.AskToUpdateLinks = False
' set the 2nd workbook object
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
' set the 2nd worksheet object
Set FilterSht = OtherWorkfile.Sheets("Raw Data")
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row with data in column "C"
.Range("A7:Q" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("A7:Q" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
OtherWorkfile.Close
Set OtherWorkfile2 = Workbooks.Open(Filename:=Application.GetOpenFilename)
' set the 2nd worksheet object
Set FilterSht2 = OtherWorkfile.Sheets("Arm Checklist")
With FilterSht2
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row with data in column "C"
.Range("C3:D" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("A:B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' implement it for the rest of your columns...
With FilterSht2
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row with data in column "C"
.Range("G3:G" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("C7:C" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht2
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row with data in column "C"
.Range("E3:E" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("E7:E" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht2
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row with data in column "C"
.Range("H3:J" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("F7:H" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
OtherWorkfile2.Close
End Sub