我有一张超过50张的单个工作簿。每个工作表的行数和列数完全相同(每个月的数据是工作表)。我正在使用For Each循环从每个工作表复制一系列单元格并粘贴到摘要表中。然后我删除空白行。如何在For Each循环期间在单独的列中添加工作表名称。我想知道一张纸上的数据的开始和结束位置。
Sub CopyRangeToAnotherSheet()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Summary" Then
wks.Range("C1:J100" & wks.Cells(Rows.Count, "C").End(xlUp).Row).Copy_
Destination:=Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(0)
End If
Next
Application.CutCopyMode = False
End Sub
Sub DeleteEmptyRows()
Application.ScreenUpdating = False
Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
End Sub
数据
Col A B C D
Value1, Value2, Value3, Sheet Name Here
Value1, Value2, Value3, Sheet Name Here
Value1, Value2, Value3, Sheet Name Here
答案 0 :(得分:0)
尝试这样的事情,我做了一些修改,使代码更加清晰:
Sub CopyRangeToAnotherSheet()
Dim wks As Worksheet
Dim copyRange as Range
Dim destRange as Range
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Summary" Then
'## Get the range which will be copied:
Set copyRange = wks.Range("C1:J100" & wks.Cells(Rows.Count, "C").End(xlUp).Row)
'## Get the range where you will paste:
Set destRange = Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(0)
'## Copy/Paste:
copyRange.Copy destRange
'## Fill in the source worksheet (copyRange.Parent) sheet name
destRange.Offset(0,3).Resize(copyRange.Rows.Count).Value = wks.Name
End If
Next
Application.CutCopyMode = False
End Sub