在Excel中合并来自不同工作表的表
我有5个不同的子声明,它们几乎都做相同的事情。唯一不同的部分是工作表变体(每个子项使用2个特定的工作表) 我想做的是把5个不同的潜艇压缩成一个整体 下面的代码是我的一个SUB的示例(注意,下面的代码只是整个SUB中的更改) 仅在以下分组1子项中更改的代码。在Excel中合并来自不同工作表的表,excel,vba,Excel,Vba,我有5个不同的子声明,它们几乎都做相同的事情。唯一不同的部分是工作表变体(每个子项使用2个特定的工作表) 我想做的是把5个不同的潜艇压缩成一个整体 下面的代码是我的一个SUB的示例(注意,下面的代码只是整个SUB中的更改) 仅在以下分组1子项中更改的代码。 Set ws = wb.Worksheets("A") Set addWS = wb.Worksheets("A add") 其中一个潜艇的代码 ' -- Combines table1 and table2 -- ' Sub
Set ws = wb.Worksheets("A")
Set addWS = wb.Worksheets("A add")
其中一个潜艇的代码
' -- Combines table1 and table2 -- '
Sub group1()
Dim wb As Workbook
Dim ws As Worksheet
Dim addWS As Worksheet
Dim counter As Long
Dim counterAdd As Long 'counter for additional trades
Set wb = Workbooks("MASTER.xlsm")
Set ws = wb.Worksheets("A")
Set addWS = wb.Worksheets("A add")
ws.Activate 'activate sheet
' Checks to see if there is only 1 row or is empty
If IsEmpty(ws.Range("A11").Value) = True Then
Exit Sub
End If
If IsEmpty(ws.Range("A12").Value) = True And IsEmpty(ws.Range("A11").Value) = False Then
counter = 1
Else
counter = ws.Range("A11", Range("A11").End(xlDown)).Rows.count
End If
addWS.Activate 'activate additional sheet
' Checks to see if there is only 1 row or is empty
If IsEmpty(addWS.Range("A11").Value) = True Then
Exit Sub
End If
If IsEmpty(addWS.Range("A12").Value) = True And IsEmpty(addWS.Range("A11").Value) = False Then
counterAdd = 1
Else
counterAdd = addWS.Range("A11", Range("A11").End(xlDown)).Rows.count
End If
' Copy / paste additional trades
addWS.Range("A11:AB" & counterAdd + 10).Copy
ws.Activate
ws.Range("A" & counter + 11).PasteSpecial xlPasteAll
End Sub
在下面的代码中,我试图将它变成一个带有2个For循环的子循环,但是它会卡在第二个循环中。有没有一种方法可以让我同时完成两件事?
' -- Combines table1 and table2 -- '
Sub group()
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAdd As Worksheet
Dim counter As Long
Dim counterAdd As Long 'counter for additional trades
Dim WSArray As Variant
Dim WSArrayAdd As Variant
Set wb = Workbooks("MASTER.xlsm")
WSArray = Array("A", "B", "C", "D", "E")
WSArrayAdd = Array("A add", "B add", "C add", "D add", "E add")
'Loop through WSArray sheets
For Each currentWS In WSArray
Set ws = wb.Worksheets(currentWS)
ws.Activate
' Checks to see if there is only 1 row or is empty
If IsEmpty(ws.Range("A11").Value) = True Then
' do nothing
End If
If IsEmpty(ws.Range("A12").Value) = True And IsEmpty(ws.Range("A11").Value) = False Then
counter = 1
Else
counter = ws.Range("A11", Range("A11").End(xlDown)).Rows.count
End If
For Each currentAddWS In WSArrayAdd
Set wsAdd = wb.Worksheets(currentAddWS)
wsAdd.Activate 'activate additional sheet
' Checks to see if there is only 1 row or is empty
If IsEmpty(wsAdd.Range("A11").Value) = True Then
' do nothing
End If
If IsEmpty(wsAdd.Range("A12").Value) = True And IsEmpty(wsAdd.Range("A11").Value) = False Then
counterAdd = 1
Else
counterAdd = wsAdd.Range("A11", Range("A11").End(xlDown)).Rows.count
End If
' Copy / paste additional trades
wsAdd.Range("A11:AB" & counterAdd + 10).Copy
ws.Activate
ws.Range("A" & counter + 11).PasteSpecial xlPasteAll
Next currentAddWS
Next currentWS
End Sub
我能够在下面的代码中解决这个问题。此代码循环遍历一系列10张表(每张表都有相应的表),并将表合并到一个主表中
' -- Combines additional trades table with main table -- '
Sub groupTables()
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAdd As Worksheet
Dim counter As Long
Dim counterAdd As Long 'counter for additional trades
Dim WSArray As Variant
Dim WSArrayAdd As Variant
Dim i As Long
Set wb = Workbooks("MASTER.xlsm")
WSArray = Array("1", "2", "3", "4", "5")
'Loop through WSArray sheets
For Each currentWS In WSArray
Set ws = wb.Worksheets(currentWS)
Set wsAdd = wb.Worksheets(WSArray(i) & " add")
ws.Activate
' COUNTS ROWS IN TRADE SHEET
' Checks to see if there is only 1 row or is empty
If IsEmpty(ws.Range("A11").Value) = True Then
counter = 0 'no trades
ElseIf IsEmpty(ws.Range("A12").Value) = True And IsEmpty(ws.Range("A11").Value) = False Then
counter = 1
Else
counter = ws.Range("A11", Range("A11").End(xlDown)).Rows.count
End If
wsAdd.Activate
' COUNTS ROWS IN ADDITIONAL TRADE SHEET
' Checks to see if there is only 1 row or is empty
If IsEmpty(wsAdd.Range("A11").Value) = True Then
counterAdd = 0 'no trades
ElseIf IsEmpty(wsAdd.Range("A12").Value) = True And IsEmpty(wsAdd.Range("A11").Value) = False Then
counterAdd = 1
Else
counterAdd = wsAdd.Range("A11", Range("A11").End(xlDown)).Rows.count
End If
'Copy Additional trades
If counterAdd > 0 Then
wsAdd.Range("A11:AB" & counterAdd + 10).Copy 'Copy additional trades table
ws.Activate
ws.Range("A" & counter + 11).PasteSpecial xlPasteAll ' Paste additional trades to main table
End If
i = i + 1 'iterate
Next currentWS
End Sub
您只需循环浏览
WSArray
,当命名第二张工作表时,您可以使用Set addWS=wb.Worksheets(WSArray(i)和“add”)
(其中i
用于循环浏览数组的计数器)@controlnetic.nomad感谢您的帮助!我可以在下面的代码中解决这个问题!