VBA Match Copy from 2 Sheets into another

Cervani

New Member
Joined
Jun 22, 2022
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi Guys.
I have a working code matching data from sheets Information and Data with sheet Header into OUTPUT

However few columns has been added into both Information and Data, so I changed ranges span and I can get data all apart from column A which should be copied to OUTPUT but it does not.
I bypass that with some other macro but I wonder what should be done to get this working with previous code?

Data start with this:
1668764113861.png

Information
1668764086588.png

OUTPUT
1668764142360.png


Header
1668764372230.png


Workgin code before adding new columns

VBA Code:
[/U]

Sub Get_GBSMDMData()



'Get the worksheets

Dim shGBSMDM As Worksheet, lastRowGBSMDM As Long, password As String

Set shGBSMDM = ThisWorkbook.Worksheets("OUTPUT")

password = "pass"



'Identify lastRowGBSMDM

lastRowGBSMDM = ThisWorkbook.Worksheets("OUTPUT").Cells(Rows.Count, 1).End(xlUp).row



shGBSMDM.Unprotect password:=password



Call TurnOffStuff



'Clear any existing output



Dim Cell As Range

For Each Cell In Range("GBSMDM")



If Cell.Value <> "" Then

shGBSMDM.Range("C25:C" & lastRowGBSMDM).EntireRow.Delete

End If



Next Cell



'Get SNData



lastrowHeaderAttributeSN = ThisWorkbook.Worksheets("Header").Range("A" & Rows.Count).End(xlUp).row

lastrowSNDataAttribute = ThisWorkbook.Worksheets("Information").Range("A" & Rows.Count).End(xlUp).row



For i = 2 To lastrowHeaderAttributeSN



Set rng1 = ThisWorkbook.Worksheets("Information").Range("A23:AT23")

Set rng2 = ThisWorkbook.Worksheets("OUTPUT").Range("A24:DY24")



m = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("A" & i).Value, rng1, 0)

ThisWorkbook.Worksheets("Information").Range("A24:A" & lastrowSNDataAttribute).Columns(m).Copy



h = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("A" & i).Value, rng2, 0)

ThisWorkbook.Worksheets("OUTPUT").Range("A25").Columns(h).PasteSpecial xlPasteValues



Next i



'Get SupplierData



lastrowHeaderAttributeSupp = ThisWorkbook.Worksheets("Header").Range("C" & Rows.Count).End(xlUp).row

lastrowSuppDataAttribute = ThisWorkbook.Worksheets("Data").Range("A" & Rows.Count).End(xlUp).row



For a = 2 To lastrowHeaderAttributeSupp



Set rng3 = ThisWorkbook.Worksheets("Data").Range("B25:CH25")

Set rng4 = ThisWorkbook.Worksheets("OUTPUT").Range("A24:DY24")



n = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("C" & a).Value, rng3, 0)

ThisWorkbook.Worksheets("Data").Range("B26:B" & lastrowSuppDataAttribute).Columns(n).Copy



o = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("C" & a).Value, rng4, 0)

ThisWorkbook.Worksheets("OUTPUT").Range("A25").Columns(o).PasteSpecial xlPasteValues



Next a



Application.CutCopyMode = False



Call Delete_Blank_Rows

Call TurnOnStuff



shGBSMDM.Protect password:=password, AllowSorting:=True, AllowFiltering:=True



End Sub



Sub TurnOffStuff()



Application.Calculation = xlCalculationManual

Application.ScreenUpdating = False

Application.EnableEvents = False



End Sub





Sub TurnOnStuff()

Application.Calculation = xlCalculationAutomatic

Application.ScreenUpdating = True

Application.EnableEvents = True

End Sub



Sub Delete_Blank_Rows()



Dim rRow As Range

Dim rSelect As Range

Dim rSelection As Range



Set rSelection = Range("GBSMDM")



'Loop through each row and add blank rows to rSelect range

For Each rRow In rSelection.Rows

If WorksheetFunction.CountA(rRow) = 0 Then

If rSelect Is Nothing Then

Set rSelect = rRow

Else

Set rSelect = Union(rSelect, rRow)

End If

End If

Next rRow



'Delete blank rows

If rSelect Is Nothing Then

MsgBox "No blank rows were found.", vbOKOnly, "Select Blank Rows Macro"

Exit Sub

Else

rSelect.Rows.Delete Shift:=xlShiftUp

End If



End Sub

[U]
This is new one with extended range ( rng 1 ,2 3 and 4) and
with macro so copy that A column

VBA Code:
[/U]
Sub Get_GBSMDMData()



'Get the worksheets

Dim shGBSMDM As Worksheet, lastRowGBSMDM As Long, password As String

Set shGBSMDM = ThisWorkbook.Worksheets("OUTPUT")

password = "pass"



'Identify lastRowGBSMDM

lastRowGBSMDM = ThisWorkbook.Worksheets("OUTPUT").Cells(Rows.Count, 1).End(xlUp).row



shGBSMDM.Unprotect password:=password



Call TurnOffStuff



'Clear any existing output



Dim Cell As Range

For Each Cell In Range("GBSMDM")



If Cell.Value <> "" Then

shGBSMDM.Range("C25:C" & lastRowGBSMDM).EntireRow.Delete

End If



Next Cell



'Get SNData



lastrowHeaderAttributeSN = ThisWorkbook.Worksheets("Header").Range("A" & Rows.Count).End(xlUp).row

lastrowSNDataAttribute = ThisWorkbook.Worksheets("Information").Range("A" & Rows.Count).End(xlUp).row



For i = 2 To lastrowHeaderAttributeSN



Set rng1 = ThisWorkbook.Worksheets("Information").Range("A23:AX23")

Set rng2 = ThisWorkbook.Worksheets("OUTPUT").Range("A24:ES24")



m = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("A" & i).Value, rng1, 0)

ThisWorkbook.Worksheets("Information").Range("A24:A" & lastrowSNDataAttribute).Columns(m).Copy



h = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("A" & i).Value, rng2, 0)

ThisWorkbook.Worksheets("OUTPUT").Range("A25").Columns(h).PasteSpecial xlPasteValues



Next i



'Get SupplierData

lastrowHeaderAttributeSupp = ThisWorkbook.Worksheets("Header").Range("C" & Rows.Count).End(xlUp).row

lastrowSuppDataAttribute = ThisWorkbook.Worksheets("Data").Range("A" & Rows.Count).End(xlUp).row

For a = 2 To lastrowHeaderAttributeSupp



Set rng3 = ThisWorkbook.Worksheets("Data").Range("B25:CW25")

Set rng4 = ThisWorkbook.Worksheets("OUTPUT").Range("A24:ES24")



n = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("C" & a).Value, rng3, 0)

ThisWorkbook.Worksheets("Data").Range("B26:B" & lastrowSuppDataAttribute).Columns(n).Copy



o = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Header").Range("C" & a).Value, rng4, 0)

ThisWorkbook.Worksheets("OUTPUT").Range("A25").Columns(o).PasteSpecial xlPasteValues



Next a



Application.CutCopyMode = False



Call Delete_Blank_Rows

‘HERE I added macro to copy/paste first column  from Information tab

Sheets("Information").Select

Range("A24").Select

Range(Selection, Selection.End(xlDown)).Select

Selection.Copy

Sheets("OUTPUT").Select

Range("A25").Select

Selection.PasteSpecial xlPasteValues



Call TurnOnStuff



shGBSMDM.Protect password:=password, AllowSorting:=True, AllowFiltering:=True



End Sub
[U]

Thanks in advance
 

Attachments

  • 1668763983169.png
    1668763983169.png
    3 KB · Views: 6

Excel Facts

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

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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