我有很多不同的工作簿,我需要多个选项卡来提取数据以进行汇总。不幸的是,它们都在列中,所以每个数据点都在它自己的列中,行中的类别低至50.
我需要能够将每个工作表中所有已使用的列栏A列复制到名为" Samples"的输出表中。因此,对于每个新选项卡,我需要将数据粘贴到输出表中的下一个可用列。
以下是我编写的代码,但在第二个循环中,我得到了一个对象定义错误。
有人能指出我正确的方向吗?我对这一切都很陌生!
Sub ExtractSamples()
Set wsOutput = ActiveWorkbook.Sheets("Samples")
For Each wsInput In ActiveWorkbook.Worksheets
If wsInput.Name <> wsOutput.Name Then
With wsInput
LColI = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rng = .Range(.Cells(1, 2) & .Cells(50, LColI))
rng.Copy
With wsOutput
LColO = .Range("A" & .Columns.Count).End(xlToLeft).Column + 1
.Range("A" & LColO).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
End With
End If
Next wsInput
End Sub
非常感谢!
答案 0 :(得分:0)
试试这个。我已经纠正了上面的语法(你的LColO的分配没有意义,因为A是列,范围想要一行和一列)。我还声明了你所有的变量,即使在这种情况下它可能没有引起错误,这也是很好的做法。
Sub ExtractSamples()
Dim wsOutput As Worksheet, wsInput As Worksheet
Dim LColI As Long, LColO As Long
Set wsOutput = ActiveWorkbook.Sheets("Samples")
For Each wsInput In ActiveWorkbook.Worksheets
If wsInput.Name <> wsOutput.Name Then
With wsInput
LColI = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set Rng = .Range(.Cells(1, 2), .Cells(50, LColI))
Rng.Copy
With wsOutput
LColO = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
.Cells(1, LColO).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
End With
End If
Next wsInput
End Sub