[VBA HELP] Copy Data.

sasils

New Member
Joined
Jun 3, 2017
Messages
24
Hi everyone,

I need help on VBA code to copy and merge data from 2 pivot tables into a single data set to be display in another sheet.

Below is data Sheet 1 in excel.

Pivot A
ShiptoSoldtoLV1LV2LV3
001ALocalNEManufacturer
002BLocalNEManufacturer
003CGlobalCETrader
004DGlobalCEWhole Sale
005EOthersSMini Mart
006EOthersSMini Mart

<tbody>
</tbody>



Pivot B
Mat CodeDescriptionLV1LV2
S001AAAChemicalRM
S002BBBChemicalRM
S003CCCChemicalRM
S004DDDChemicalRM
S005EEEPaperPM
S006FFFPaperPM

<tbody>
</tbody>


Final Data to use data from Pivot A and B into a data set in Sheet 2. The result should look like below.

Final Table
ShiptoSoldtoLV1LV2LV3Mat CodeDescriptionLV1LV2
1ALocalNEManufacturerS001AAAChemicalRM
1ALocalNEManufacturerS002BBBChemicalRM
1ALocalNEManufacturerS003CCCChemicalRM
1ALocalNEManufacturerS004DDDChemicalRM
1ALocalNEManufacturerS005EEEPaperPM
1ALocalNEManufacturerS006FFFPaperPM
2BLocalNEManufacturerS001AAAChemicalRM
2BLocalNEManufacturerS002BBBChemicalRM
2BLocalNEManufacturerS003CCCChemicalRM
2BLocalNEManufacturerS004DDDChemicalRM
2BLocalNEManufacturerS005EEEPaperPM
2BLocalNEManufacturerS006FFFPaperPM
3CGlobalCETraderS001AAAChemicalRM
3CGlobalCETraderS002BBBChemicalRM
3CGlobalCETraderS003CCCChemicalRM
3CGlobalCETraderS004DDDChemicalRM
3CGlobalCETraderS005EEEPaperPM
3CGlobalCETraderS006FFFPaperPM
4DGlobalCEWhole SaleS001AAAChemicalRM
4DGlobalCEWhole SaleS002BBBChemicalRM
4DGlobalCEWhole SaleS003CCCChemicalRM
4DGlobalCEWhole SaleS004DDDChemicalRM
4DGlobalCEWhole SaleS005EEEPaperPM
4DGlobalCEWhole SaleS006FFFPaperPM
5EOthersSMini MartS001AAAChemicalRM
5EOthersSMini MartS002BBBChemicalRM
5EOthersSMini MartS003CCCChemicalRM
5EOthersSMini MartS004DDDChemicalRM
5EOthersSMini MartS005EEEPaperPM
5EOthersSMini MartS006FFFPaperPM
6EOthersSMini MartS001AAAChemicalRM
6EOthersSMini MartS002BBBChemicalRM
6EOthersSMini MartS003CCCChemicalRM
6EOthersSMini MartS004DDDChemicalRM
6EOthersSMini MartS005EEEPaperPM
6EOthersSMini MartS006FFFPaperPM

<tbody>
</tbody>


Thank you so much for your help.

Cheers.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I wrote a bit of VBA. I assume:
  • the two important pivot tables are on the same worksheet,
  • there is only the one header row in the pivot tables,
  • that the pivot tables can be reliably identified by the labels in their top left cells,
  • and any other pivot tables in that worksheet don't have the same labels in their top left cells.

Make the worksheet with these two pivot tables the active sheet, and run this code.

Code:
Sub ProcessPivots()
  Dim ws1 As Worksheet
  Set ws1 = ActiveSheet
  
  ' identify pivot tables
  Dim pt1 As PivotTable, pt2 As PivotTable, pt As PivotTable
  For Each pt In ws1.PivotTables
    If pt.TableRange1.Cells(1, 1).Value = "Shipto" Then
      Set pt1 = pt
    ElseIf pt.TableRange1.Cells(1, 1).Value = "Mat Code" Then
      Set pt2 = pt
    End If
  Next
  If pt1 Is Nothing Or pt2 Is Nothing Then
    MsgBox "Couldn't identify both pivot tables.", vbCritical
    GoTo ExitSub
  End If
  
  ' insert new sheet
  Dim ws2 As Worksheet
  Set ws2 = Worksheets.Add(After:=ws1)
  
  'set up ranges
  Dim ptrange1 As Range, ptrange2 As Range
  Set ptrange1 = pt1.TableRange1
  Set ptrange2 = pt2.TableRange1
  Dim rows1 As Long, rows2 As Long
  rows1 = ptrange1.Rows.Count - 1
  rows2 = ptrange2.Rows.Count - 1
  Dim columns1 As Long, columns2 As Long
  columns1 = ptrange1.Columns.Count
  columns2 = ptrange2.Columns.Count
  
  ' headers
  ws2.Range("A1").Resize(, columns1).Value = ptrange1.Rows(1).Value
  ws2.Range("A1").Offset(, columns1).Resize(, columns2).Value = ptrange2.Rows(1).Value
  
  ' populate
  Dim ptrange2a As Range
  Set ptrange2a = ptrange2.Offset(1).Resize(rows2)
  Dim loop1 As Long
  For loop1 = 1 To rows1
    ws2.Range("A1").Offset(1 + (loop1 - 1) * rows2).Resize(rows2, columns1).Value = ptrange1.Rows(1 + loop1).Value
    ws2.Range("A1").Offset(1 + (loop1 - 1) * rows2, columns1).Resize(rows2, columns2).Value = ptrange2a.Value
  Next
  
ExitSub:
End Sub
 
Upvote 0
Hello,

Thank you for your help here, some clarification as I try it and it encounter error "MsgBox "Couldn't identify both pivot tables.", vbCritical"
- Do I have to change the name of pivot table to pt1 and pt2
- The 1st column in pt1 "Ship-to" is located in cell A9, it is impacted in above code is it to do with Range
- The 1st column in pt2 "Mat Code" is located in cell Q12, it is impacted in above code is it to do with Range
Thanks
 
Upvote 0
pt1 and pt2 are the variables used by VBA. I tried to make it easy, so you didn't have to rename pivot tables.

The two pivot tables must have "Shipto" and "Mat Code" in their respective top left cells, as your first post showed. If one has "Ship-to" instead, the code will not find it.

If you want to rename the pivot tables, it may make the code generally more reliable (though if the condition in the above sentence is followed exactly, my code is reliable), name them "Pivot A" and "Pivot B" the way you labeled them in your first post. Then this code should work:

Code:
Sub ProcessPivots()
  Dim ws1 As Worksheet
  Set ws1 = ActiveSheet
  
  ' identify pivot tables
  Dim pt1 As PivotTable, pt2 As PivotTable
  On Error Resume Next
  Set pt1 = ws1.PivotTables("Pivot A")
  Set pt2 = ws1.PivotTables("Pivot B")
  On Error Goto 0
  If pt1 Is Nothing Or pt2 Is Nothing Then
    MsgBox "Couldn't identify both pivot tables.", vbCritical
    GoTo ExitSub
  End If
  
  ' insert new sheet
  Dim ws2 As Worksheet
  Set ws2 = Worksheets.Add(After:=ws1)
  
  'set up ranges
  Dim ptrange1 As Range, ptrange2 As Range
  Set ptrange1 = pt1.TableRange1
  Set ptrange2 = pt2.TableRange1
  Dim rows1 As Long, rows2 As Long
  rows1 = ptrange1.Rows.Count - 1
  rows2 = ptrange2.Rows.Count - 1
  Dim columns1 As Long, columns2 As Long
  columns1 = ptrange1.Columns.Count
  columns2 = ptrange2.Columns.Count
  
  ' headers
  ws2.Range("A1").Resize(, columns1).Value = ptrange1.Rows(1).Value
  ws2.Range("A1").Offset(, columns1).Resize(, columns2).Value = ptrange2.Rows(1).Value
  
  ' populate
  Dim ptrange2a As Range
  Set ptrange2a = ptrange2.Offset(1).Resize(rows2)
  Dim loop1 As Long
  For loop1 = 1 To rows1
    ws2.Range("A1").Offset(1 + (loop1 - 1) * rows2).Resize(rows2, columns1).Value = ptrange1.Rows(1 + loop1).Value
    ws2.Range("A1").Offset(1 + (loop1 - 1) * rows2, columns1).Resize(rows2, columns2).Value = ptrange2a.Value
  Next
  
ExitSub:
End Sub
 
Upvote 0
Hello Jon,

Thank you for helping me, it work great now. Few small things. when it create a new sheet. It actually copy the header of Pivot 1 and value of Pivot 2 columns like below. I dont need header to be there, just need values from the pivot table to be copy. HOw to change the code on this. Also I would like the data to be inserted into existing sheet name "Data" starting row 59.

Thank you for your help.

CustomerName 1Sales GroupMatcodeDescription EN
CustomerName 1Sales GroupS100003500น้ำตาลทรายดิบ A (เทกอง)Bonsucro
CustomerName 1Sales GroupS100009800Raw A Sugar (Bulk) OFF SPEC
CustomerName 1Sales GroupS100009900น้ำตาลทรายดิบ A (เทกอง)
CustomerName 1Sales GroupS100109900น้ำตาลทรายดิบ (1000KG)NoMark
CustomerName 1Sales GroupS100120000น้ำตาลทรายดิบมิตรผล (1000KG)
CustomerName 1Sales GroupS100220000น้ำตาลทรายดิบ A (500KG)
CustomerName 1Sales GroupS100310000น้ำตาลทรายดิบ A มิตรผล (50KG.)
CustomerName 1Sales GroupS110009900น้ำตาลทรายดิบ B (เทกอง)
CustomerName 1Sales GroupS110220000น้ำตาลทรายดิบ B (500KG)
CustomerName 1Sales GroupS13000990น้ำตาลทรายดิบ (เทกอง)
CustomerName 1Sales GroupS130009900น้ำตาลทรายดิบไฮโพล์มิตรผล (เทกอง)
CustomerName 1Sales GroupS130110000น้ำตาลทรายดิบไฮโพล์มิตรผล (1000KG.)
CustomerName 1Sales GroupS130129900น้ำตาลทรายดิบไฮโพล์ NoMark (1000KG)
CustomerName 1Sales GroupS130310000น้ำตาลทรายดิบไฮโพล์มิตรผล (50KG.)
CustomerName 1Sales GroupS130510070น้ำตาลทรายดิบไฮโพล์มิตรผล(25KG)REG-KOREA

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
These are minor modifications. As you become familiar with how VBA works, you'll be able to figure them out for yourself.

In the code below I've commented out the old lines and inserted the new lines.

Code:
Sub ProcessPivots()
  Dim ws1 As Worksheet
  Set ws1 = ActiveSheet
  
  ' identify pivot tables
  Dim pt1 As PivotTable, pt2 As PivotTable
  On Error Resume Next
  Set pt1 = ws1.PivotTables("Pivot A")
  Set pt2 = ws1.PivotTables("Pivot B")
  On Error Goto 0
  If pt1 Is Nothing Or pt2 Is Nothing Then
    MsgBox "Couldn't identify both pivot tables.", vbCritical
    GoTo ExitSub
  End If
  
  ' insert new sheet
  Dim ws2 As Worksheet
  ''Set ws2 = Worksheets.Add(After:=ws1) '' OLD
  Set ws2 = Worksheets("Data") '' NEW
  
  'set up ranges
  Dim ptrange1 As Range, ptrange2 As Range
  Set ptrange1 = pt1.TableRange1
  Set ptrange2 = pt2.TableRange1
  Dim rows1 As Long, rows2 As Long
  rows1 = ptrange1.Rows.Count - 1
  rows2 = ptrange2.Rows.Count - 1
  Dim columns1 As Long, columns2 As Long
  columns1 = ptrange1.Columns.Count
  columns2 = ptrange2.Columns.Count
  
  '' headers '' NO LONGER USED
  ''ws2.Range("A1").Resize(, columns1).Value = ptrange1.Rows(1).Value '' OLD
  ''ws2.Range("A1").Offset(, columns1).Resize(, columns2).Value = ptrange2.Rows(1).Value '' OLD
  
  ' populate
  Dim ptrange2a As Range
  Set ptrange2a = ptrange2.Offset(1).Resize(rows2)
  Dim loop1 As Long
  For loop1 = 1 To rows1
    ''ws2.Range("A1").Offset(1 + (loop1 - 1) * rows2).Resize(rows2, columns1).Value = ptrange1.Rows(1 + loop1).Value '' OLD
    ''ws2.Range("A1").Offset(1 + (loop1 - 1) * rows2, columns1).Resize(rows2, columns2).Value = ptrange2a.Value '' OLD
    ws2.Range("A59").Offset((loop1 - 1) * rows2).Resize(rows2, columns1).Value = ptrange1.Rows(1 + loop1).Value '' NEW
    ws2.Range("A59").Offset((loop1 - 1) * rows2, columns1).Resize(rows2, columns2).Value = ptrange2a.Value '' NEW
  Next
  
ExitSub:
End Sub
 
Upvote 0
Hi,

The head values still there.... As Customer, Sale group, Matcode Description EN, i think it is to do with populate part of the code....can you help thank you so much.

CustomerName 1Sales GroupMatcodeDescription EN
CustomerName 1Sales GroupS133071400น้ำตาลซองแดงเอราวัณ SACHET(7G.*100*20)
CustomerName 1Sales GroupS133071500น้ำตาลซองแดงSPASSO SACHET (7G.*100*20)
CustomerName 1Sales GroupS303070400น้ำตาลบริสุทธิ์การบินไทย (7G.*100*20)
CustomerName 1Sales GroupS303072300น้ำตาลบริสุทธิ์ Tables T1(7G*100*20)
CustomerName 1Sales GroupS403070400น้ำตาลบริสุทธิ์ซองการบินไทย (7G.*100*20)
CustomerName 1Sales GroupS403071400น้ำตาลบริสุทธิ์เอราวัณSACHET(7G.*100*20)
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11MatcodeDescription EN
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11S133071400น้ำตาลซองแดงเอราวัณ SACHET(7G.*100*20)
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11S133071500น้ำตาลซองแดงSPASSO SACHET (7G.*100*20)
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11S303070400น้ำตาลบริสุทธิ์การบินไทย (7G.*100*20)
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11S303072300น้ำตาลบริสุทธิ์ Tables T1(7G*100*20)
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11S403070400น้ำตาลบริสุทธิ์ซองการบินไทย (7G.*100*20)
100013บริษัท เนสท์เล่ (ไทย) จำกัดD11S403071400น้ำตาลบริสุทธิ์เอราวัณSACHET(7G.*100*20)
100015บริษัท ควอลิตี้ คอฟฟี่ โปรดักท์สD11MatcodeDescription EN

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Do your pivot tables have more than one row of headers at the top? This was one of the assumptions, based on your initial post. I built these two pivot tables based on that post:

NvKXwZC.png
 
Last edited:
Upvote 0
Hi,

I think it has 2 header rows, one is empty and below it is header name...and I remove it in Pivot table and it worked now thank you.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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