VBA Codes On Combine Data

Mel0620

New Member
Joined
Jun 24, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi Excel gurus:

I am trying to create a VBA codes in excel using below codes but I ran into error:

This is what I intended to do:

1. Copy data from Sheet A (specific to 3 columns only) to Summary sheet
2. Copy data from Sheet B (specific to 3 columns only) to Summary sheet
3. Arrange the copied data in summary tab and delete duplicate data

Sub CombineData()

'CopyPasteSage

'Sheets("Sage_Pivot").Select
Range("A5:C5").Select
Range("A5:C5").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Sheets("Top Sheet").Select
Range("A24:C24").Select
Range("A24:C24").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

'CopyPasteAccrual

'Sheets("PCard Accrual_Pivot").Select
Range("K5").Select
Range("K5").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Sheets("Top Sheet").Select
Range("A24").End(xlDown).Offset(1, 0).Select
Range("A24").Offset(-1, 6).Copy Destination:=Range("A24").Offset(-1, 1)

'Sheets("PCard Accrual_Pivot").Select
Range("M5:N5").Select
Range("M5:N5").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Sheets("Top Sheet").Select
Range("B4:C24").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A24").End(xlDown).Offset(1, 0).Select


'Sheets("Top Sheet").Select
Range("A24", Range("A24").End(xlDown)).Rows.RemoveDuplicates Columns:=1, Header:=xlYes

Dim Table As PivotCache
For Each Table In ThisWorkbook.PivotCaches

Table.Refresh

Next Table

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
According to your macro, you copy the data from row 5 and paste it into row 24. But I really don't know where the headers are at the source and at the destination.
Maybe those rows need to be adjusted in the macro.

Try this:

VBA Code:
Sub CombineData()
  Dim sh3 As Worksheet
  Dim lr2 As Long, lr3 As Long
  Dim Table As PivotCache
  
  Application.ScreenUpdating = False
  Set sh3 = Sheets("Top Sheet")
  sh3.Range("A24:C" & Rows.Count).ClearContents
  
  With Sheets("Sage_Pivot")
    .Range("A5:C" & .Range("A:C").Find("*", , xlValues, , xlByRows, xlPrevious).Row).Copy
    sh3.Range("A24").PasteSpecial xlPasteValues
  End With
  
  With Sheets("PCard Accrual_Pivot")
    lr2 = .Range("K:N").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    .Range("K5:K" & lr2 & ",M5:N" & lr2).Copy
    sh3.Range("A" & sh3.Range("A:C").Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1).PasteSpecial xlPasteValues
  End With
  
  lr3 = sh3.Range("A:C").Find("*", , xlValues, , xlByRows, xlPrevious).Row
  sh3.Range("A24:C" & lr3).Rows.RemoveDuplicates Columns:=1, Header:=xlYes
  
  For Each Table In ThisWorkbook.PivotCaches
    Table.Refresh
  Next Table
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
I have a concern on removing duplicates, my goal is to remove duplicates but keeping the first one.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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