提取多列

时间:2015-03-23 13:39:41

标签: excel vba excel-vba

下面是我尝试过的代码,但它只适用于A列,我想与其他列做同样的工作:

Sub ColumnAMaster()
Dim lastRow As Long, lastRowMaster As Long
Dim ws As Worksheet
Dim Master As Worksheet

  Application.ScreenUpdating = False
  Set Master = Sheets.Add
  Master.Name = "Master"
  lastRowMaster = 1
  For Each ws In ThisWorkbook.Sheets
    If ws.Name <> "Master" Then
      lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
      ws.Range("A1:A" & lastRow).Copy Destination:=Master.Range("A" & lastRowMaster)
      lastRowMaster = Master.Range("A" & Rows.Count).End(xlUp).Row + 1
    End If
  Next
  Application.ScreenUpdating = True
  MsgBox "Done!"
End Sub

1 个答案:

答案 0 :(得分:0)

sub Master_data()
 'declarations
Dim ws1 as worksheet
Dim ws2 as worksheet 
Dim ws3 as worksheet
Dim ws4 as worksheet
Dim i as long
Dim lastrow_sh1 as long
Dim lastrow_sh4 as long
Dim wb as workbook
set wb= application.activeworkbook
set ws1=wb.sheets("sheet1")
set ws2=wb.sheets("sheet2")
set ws3=wb.sheets("sheet3")
set ws4=wb.sheets("sheet4")

lastrow_sh1= ws1.range("A1").end(xlup).row
ws4.cells(1,1)="name"



for i =2 to lastrow_sh1 
        if ws1.cells(i,1)=ws2.cells(i,1) & ws3.cell(i,1)then
        lastrow_sh4=ws4.range(A1).end(xlup).row

      ws4.cells(lastrow_sh4+1,1)=ws1.cells(i,1)
      ws4.cells(lastrow_sh4+1,2)=ws1.cells(i,2)
      ws4.cells(lastrow_sh4+1,3)=ws1.cells(i,3)
      ws4.cells(lastrow_sh4+1,4)=ws3.cells(i,4)

                 endif

next i

end sub