Sub combineDatasheets()
Dim sh As Worksheet
For Each sh In Sheets
If sh.Name <> "Combined datasheet" Then
a = sh.Cells(1, 1).End(xlDown).Row 'count rows untill blank
b = Sheets("Combined datasheet").Cells(1, 1).End(xlDown).Row 'last row with data
'find if there's any data already in "Combined datasheet" by looking at cell A1
If Sheets("Combined datasheet").Cells(1, 1).Value = "" Then
b = 0
End If
sh.Rows("1:" & a).Copy Destination:=Sheets("Combined datasheet").Range("A" & b + 1)
End If
Next sh
End Sub
这将为您提供所有包含数据的行,直到每个工作表中的第一个空白行(当然,忽略您正在合并数据的行)并将它们连续粘贴到“组合数据表”中。
如有必要,更改“组合数据表”工作表的名称。
注意:如果第一行为空白,则不会从该工作表中检索到任何数据。
希望这可以帮助!
编辑:
好的,如果我理解正确,您希望在任何其他数据表中的值发生变化时刷新合并表中的数据。因此,为此在您要从中检索数据的每个工作表中使用以下代码(我猜是您提到的 7 个工作表):
Private Sub Worksheet_Change(ByVal Target As Range)
Call combineDatasheets
End Sub
现在下面的代码进入一个模块(VBA->Insert->Module):
Sub combineDatasheets()
Dim sh As Worksheet
'Clear data in "Combined datasheet"
c = Sheets("Combined datasheet").Cells(1, 1).End(xlDown).Row
Sheets("Combined datasheet").Range("A1:A" & c).EntireRow.ClearContents
For Each sh In Sheets
If sh.Name <> "Combined datasheet" Then
a = sh.Cells(1, 1).End(xlDown).Row 'count rows untill blank
'fix error when there's only 1 row with data
If sh.Cells(2, 1).Value = "" Then
a = 1
End If
b = Sheets("Combined datasheet").Cells(1, 1).End(xlDown).Row 'last row with data
'find if there's any data already in "Combined datasheet" by looking at cell A1
If Sheets("Combined datasheet").Cells(1, 1).Value = "" Then
b = 0
Else
'fix error when "Combined datasheet" worksheet has only one row with data
If Sheets("Combined datasheet").Cells(2, 1).Value = "" Then
b = 1
End If
End If
sh.Rows("1:" & a).Copy Destination:=Sheets("Combined datasheet").Range("A" & b + 1)
End If
Next sh
End Sub
关于您遇到的错误,我认为这是因为您没有更改整合信息的工作表的名称。您需要将名称更改为“组合数据表”(不带引号),以便它可以与我编写的代码一起使用,或者您直接转到代码并将其中的名称更改为您自己的选择之一(每个当您看到“组合数据表”更改为引号内所需的名称时)。
我希望这一次对你能正常工作:)