VBA copy column from two sheets to master in sequence

msarfaraz

New Member
Joined
Oct 18, 2012
Messages
46
Sheet1
NameDescUnitAmount
ABCTechnical51000
DEFElectronic102000
GHKMattel153000
LMNGlass254000
this sheet has many records

Sheet2
NameDescUnitAmount
TTGWire45678
EEFClone26789
HJKAcid256789
this sheet has many records



MASTER Sheet where data need to copy paste
MASTER
NameDescUnitAmount
ABCTechnical51000
DEFElectronic102000
GHKMattel153000
LMNGlass254000
TTGWire45678
EEFClone26789
HJKAcid256789

So i need a VBA which give me copy and paste data from sheet1 and sheet2 to Master sheet
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi msarfaraz,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long
    Dim lngPasteRow As Long
    Dim wsSource As Worksheet
    Dim wsDestin As Worksheet
    Dim varMySheet As Variant
    
    Application.ScreenUpdating = False
    
    Set wsDestin = ThisWorkbook.Sheets("Master")
    
    For Each varMySheet In Array("Sheet1", "Sheet2")
        Set wsSource = ThisWorkbook.Sheets(CStr(varMySheet))
        If WorksheetFunction.CountA(wsSource.Cells) > 0 Then
            lngLastRow = wsSource.Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        End If
        If WorksheetFunction.CountA(wsDestin.Cells) > 0 Then
            lngPasteRow = wsDestin.Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        End If
        If lngLastRow >= 2 And lngPasteRow >= 2 Then
            wsSource.Range("A2:D" & lngLastRow).Copy Destination:=wsDestin.Range("A" & lngPasteRow & ":D" & lngPasteRow)
        End If
    Next varMySheet
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi msarfaraz,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long
    Dim lngPasteRow As Long
    Dim wsSource As Worksheet
    Dim wsDestin As Worksheet
    Dim varMySheet As Variant
   
    Application.ScreenUpdating = False
   
    Set wsDestin = ThisWorkbook.Sheets("Master")
   
    For Each varMySheet In Array("Sheet1", "Sheet2")
        Set wsSource = ThisWorkbook.Sheets(CStr(varMySheet))
        If WorksheetFunction.CountA(wsSource.Cells) > 0 Then
            lngLastRow = wsSource.Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        End If
        If WorksheetFunction.CountA(wsDestin.Cells) > 0 Then
            lngPasteRow = wsDestin.Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        End If
        If lngLastRow >= 2 And lngPasteRow >= 2 Then
            wsSource.Range("A2:D" & lngLastRow).Copy Destination:=wsDestin.Range("A" & lngPasteRow & ":D" & lngPasteRow)
        End If
    Next varMySheet
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Thanks Robert it work perfectly
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top