我有多个数据工作表中的数据要合并到摘要工作表中。 当用户更新任何数据工作表中的数据时,它会自动将信息复制到摘要工作表。
例如
(输入)数据工作表1
type Foo with
member m.clone() = {x=m.x;y=m.y}
(输入)数据工作表2
| A | B | C |
Cat Red Male
Dog Green Female
(输出)摘要工作表
| A | B | C |
Monkey Brown Male
Ant Blue Male
Bird White Female
现在我的代码运行正常,有一个数据工作表。但是,当我尝试切换到另一个数据工作表上的工作时,摘要工作表上的数据将无法更正。当我删除所有信息或更改代码时(我需要重新打开它),它也有一些错误。
这是我的代码
的ThisWorkbook
| A | B | C |
Cat Red Male
Dog Green Female
Monkey Brown Male
Ant Blue Male
Bird White Female
模块
Public Sub Workbook_Open()
Set WB = ThisWorkbook
Set ActWS = WB.ActiveSheet
Set MainWS = WB.Worksheets("Main")
ActWSPreLastRow = ActWS.Cells(ActWS.Rows.Count, "A").End(xlUp).Row
MainWSPreLastRow = MainWS.Cells(MainWS.Rows.Count, "A").End(xlUp).Row
End Sub
其他工作表
Public WB As Workbook
Public ActWS As Worksheet
Public MainWS As Worksheet
Public ActWSPreLastRow As Long
Public ActWSStoredLastRow As Long
Public MainWSPreLastRow As Long
Public MainWSStoredLastRow As Long
Public MainWSEndLastRow As Long
Public I As Long
Public Sub DoCopy()
Set WB = ThisWorkbook
Set ActWS = WB.ActiveSheet
Set MainWS = WB.Worksheets("Main")
ActWSPreLastRow = ActWS.Cells(ActWS.Rows.Count, "A").End(xlUp).Row
MainWSPreLastRow = MainWS.Cells(MainWS.Rows.Count, "A").End(xlUp).Row
I = MainWSStoredLastRow + (ActWSPreLastRow - ActWSStoredLastRow)
MainWS.Range("A" & MainWSStoredLastRow + 1, "AQ" & I).Value = _
ActWS.Range("A" & ActWSStoredLastRow + 1, "AQ" & ActWSPreLastRow).Value
MainWSPreLastRow = MainWS.Cells(MainWS.Rows.Count, "A").End(xlUp).Row
End Sub
Public Sub StoreOld()
ActWSStoredLastRow = ActWSPreLastRow
MainWSStoredLastRow = MainWSPreLastRow
End Sub
答案 0 :(得分:2)
Microsoft查询:救援:
assets
SELECT A,B,C FROM [Sheet1$]
UNION ALL
SELECT A,B,C FROM [Sheet2$]
或随意使用my Add-In。
然后只需在需要时刷新查询(通过1行VBA或右键单击表格并单击刷新)。
假设Data->From Other Sources->Microsoft Query
&的结构Sheet1
是:
(输入)数据工作表1(带行号)
Sheet2
(输入)数据工作表2(带行号)
1 | A | B | C |
2 Cat Red Male
3 Dog Green Female
,然后强>
创建新工作表(Sheet3)
使用上面的SQL创建查询。
如果要添加条件,请向 1 | A | B | C |
2 Monkey Brown Male
3 Ant Blue Male
4 Bird White Female
WHERE
子句