【发布时间】:2019-08-01 15:21:08
【问题描述】:
我正在寻找一些复杂的东西。我有一个主工作簿(名称:Verificari CE),其他工作簿位于桌面上的同一文件夹中(文件夹名称 Verificari)。如果我可以从位于桌面上名为“Verificari”的文件夹中循环遍历整个 .xls 工作簿,并将每个工作簿中的数据复制到这个主工作簿 (Verificari CE) 中。
假设我有这些工作簿:
- Verificari CE(主工作簿)
- 测试 A
- 测试 B
- 测试 C
注意:这些工作簿的名称和编号(测试 A;测试 B;测试 C……)会有所不同!
这是我需要它的功能:
- 将测试 A 的 Sheet1 中包含数据的所有行复制到 Verificari CE。
- 然后 检查测试 B 的 Sheet1 并复制所有包含 A2 数据的行,在 Verificari CE 上粘贴以下活动 A 的数据
- 然后 检查测试 C 的 Sheet1 并复制所有包含数据的行,在 Verificari CE 上粘贴以下活动 B 的数据
很抱歉,我无法上传示例(我在一家对数据敏感的公司工作)。任何帮助将不胜感激!
Sub Copymultiple()
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.DisplayAlerts = False
.EnableEvents = False
End With
Dim VerificariCE As Workbook
Dim TestA As Workbook
Dim TestB As Workbook
Dim TestC As Workbook
Dim maxRow As Long
Dim maxCol As Integer
Dim nextRow As Long
Set VerificariCE = Workbooks("Verificari CE.xlsm")
With VerificariCE.Sheets(2)
Workbooks.Open .Cells(1, 1).Value
Set TestA = ActiveWorkbook
Workbooks.Open .Cells(2, 1).Value
Set TestB = ActiveWorkbook
Workbooks.Open .Cells(2, 1).Value
Set TestC = ActiveWorkbook
End With
'Comment this out if you don't want to clear existing values
VerificariCE.Sheets(1).UsedRange.Clear
'Comment this out if you don't want to clear existing values
nextRow = VerificariCE.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row + 1
With TestA.Sheets(1)
.Activate
maxRow = .Cells(Rows.Count, "A").End(xlUp).Row
maxCol = .Cells(3, Columns.Count).End(xlToLeft).Column
.Range(.Cells(3, 1), .Cells(maxRow, maxCol)).Copy
End With
VerificariCE.Activate
VerificariCE.Sheets(1).Cells(nextRow, 1).Select
ActiveSheet.Paste
nextRow = VerificariCE.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row + 1
TestA.Close
With TestB.Sheets(1)
.Activate
maxRow = .Cells(Rows.Count, "A").End(xlUp).Row
maxCol = .Cells(3, Columns.Count).End(xlToLeft).Column
.Range(.Cells(3, 1), .Cells(maxRow, maxCol)).Copy
End With
VerificariCE.Activate
VerificariCE.Sheets(1).Cells(nextRow, 1).Select
ActiveSheet.Paste
nextRow = VerificariCE.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row + 1
TestB.Close
With TestC.Sheets(1)
.Activate
maxRow = .Cells(Rows.Count, "A").End(xlUp).Row
maxCol = .Cells(3, Columns.Count).End(xlToLeft).Column
.Range(.Cells(3, 1), .Cells(maxRow, maxCol)).Copy
End With
VerificariCE.Activate
VerificariCE.Sheets(1).Cells(nextRow, 1).Select
ActiveSheet.Paste
nextRow = VerificariCE.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row + 1
TestC.Close
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
.EnableEvents = True
End With
With VerificariCE.Sheets(1).UsedRange
.Value = .Value
.Activate
End With
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
End Sub
【问题讨论】: