Copy specific columns after column contains BALANCES word in the header

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
Hello
I would macro to deal with 2000 names in CLS sheet and 5000 rows for duplicates names in VB sheet.
in CLS sheet should show result in G:L after match NAME column between two sheets .
in VB sheet will contains duplicates names . so should merge amount for DEBIT,CREDIT columns when show in J,K columns in CLS sheet.
I don't want power query I don't used to use it , so I don't need it.


HS ‫‬.xlsx
ABC
1DATENAMEBALANCES
211/08/2024AHHGGJ-15,100.00
312/09/2024AHHGGJ-22,500.00
411/08/2024AHHGGJ-328,200.00
511/09/2024AHHGGJ-439,368.00
612/09/2024AHHGGJ-52,370.00
7TOTAL77,538.00
CLS
Cell Formulas
RangeFormula
C7C7=SUM(C2:C6)


HS ‫‬.xlsx
ABCDEF
1DATENAMEVMNDEBITCREDITTOTAL
211/08/2024AHHGGJ-1REC2,000.002,000.00
312/09/2024AHHGGJ-2PPA1,200.00-1,200.00
413/09/2024AHHGGJ-2REC3,000.003,000.00
514/09/2024AHHGGJ-1REC1,200.001,200.00
615/09/2024AHHGGJ-2PPA100.00-100.00
716/09/2024AHHGGJ-3REC2,000.002,000.00
817/09/2024AHHGGJ-4REC1,500.001,500.00
918/09/2024AHHGGJ-5REC1,000.001,000.00
10TOTAL10,700.001,300.009,400.00
VB
Cell Formulas
RangeFormula
F2:F10F2=D2-E2
D10:E10D10=SUM(D2:D9)

I expect result like this

HS ‫‬.xlsx
ABCDEFGHIJKL
1DATENAMEBALANCESDATENAMEBALANCESDEBITCREDITTOTAL
211/08/2024AHHGGJ-15,100.0011/08/2024AHHGGJ-15,100.003,200.008,300.00
312/09/2024AHHGGJ-22,500.0012/09/2024AHHGGJ-22,500.003,000.001,300.004,200.00
411/08/2024AHHGGJ-328,200.0011/08/2024AHHGGJ-328,200.002,000.0030,200.00
511/09/2024AHHGGJ-439,368.0011/09/2024AHHGGJ-439,368.001,500.0040,868.00
612/09/2024AHHGGJ-52,370.0012/09/2024AHHGGJ-52,370.001,000.003,370.00
7TOTAL77,538.00TOTAL77,538.0010,700.001,300.0086,938.00
CLS
Cell Formulas
RangeFormula
L2:L6L2=I2+J2-K2
C7,I7:L7C7=SUM(C2:C6)

thanks in advanced
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:

VBA Code:
Sub Create_Balance()
  'Variable declaration area
  Dim a As Variant, b As Variant, c As Variant
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim dic As Object
  Dim fec As Double, bal As Double
  Dim i As Long, y As Long
  
  Set sh1 = Sheets("CLS")
  Set sh2 = Sheets("VB")
  Set dic = CreateObject("Scripting.Dictionary")
  
  'Loading arrays
  a = sh1.Range("A2:C" & sh1.Range("A" & Rows.Count).End(3).Row).Value
  b = sh2.Range("A2:F" & sh2.Range("A" & Rows.Count).End(3).Row).Value
  ReDim c(1 To UBound(a, 1), 1 To 6)
  
  'Create indexes
  For i = 1 To UBound(a)
    y = y + 1
    dic(a(i, 2)) = y
    c(y, 1) = a(i, 1)
    c(y, 2) = a(i, 2)
    c(y, 3) = a(i, 3)
  Next
  
  'Calculate balances
  For i = 1 To UBound(b)
    If dic.exists(b(i, 2)) Then
      y = dic(b(i, 2))
      c(y, 4) = c(y, 4) + b(i, 4)
      c(y, 5) = c(y, 5) + b(i, 5)
      c(y, 6) = c(y, 3) + c(y, 4) - c(y, 5)
    End If
  Next
  
  Application.ScreenUpdating = False
  'Pass values to the sheet
  sh1.Range("G2", sh1.Cells(Rows.Count, "L")).Clear
  With sh1.Range("G2").Resize(UBound(c, 1), UBound(c, 2))
    .Value = c
    .Borders.LineStyle = xlContinuous
  End With
  
  'Format cells
  sh1.Range("A" & Rows.Count).End(3).Copy
  sh1.Range("G" & Rows.Count).End(3).Resize(1, 6).PasteSpecial xlPasteFormats
  sh1.Range("G:L").HorizontalAlignment = xlCenter
  sh1.Range("I:L").NumberFormat = "#,##0.00;#,##0.00;"
  Application.ScreenUpdating = True
End Sub

😇
 
Upvote 0
thanks, but the code doesn't show new name if there is new name in VB sheet and it's not existed in CLS .
 
Upvote 0
but the code doesn't show new name if there is new name in VB sheet and it's not existed in CLS .
In your example you did not put a case like that.
You also didn't explain it in your OP.


If you want that to happen, you will have to explain what those records that exist in VB and not CLS would be like and how you want the result.
 
Upvote 0
I will try to assume:

VBA Code:
Sub Create_Balance()
  'Variable declaration area
  Dim a As Variant, b As Variant, c As Variant
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim dic As Object
  Dim fec As Double, bal As Double
  Dim i As Long, y As Long
  
  Set sh1 = Sheets("CLS")
  Set sh2 = Sheets("VB")
  Set dic = CreateObject("Scripting.Dictionary")
  
  'Loading arrays
  a = sh1.Range("A2:C" & sh1.Range("A" & Rows.Count).End(3).Row).Value
  b = sh2.Range("A2:F" & sh2.Range("A" & Rows.Count).End(3).Row).Value
  ReDim c(1 To UBound(a, 1) + UBound(a, 1), 1 To 6)
  
  'Create indexes
  For i = 1 To UBound(a) - 1
    y = y + 1
    dic(a(i, 2)) = y
    c(y, 1) = a(i, 1)
    c(y, 2) = a(i, 2)
    c(y, 3) = a(i, 3)
  Next
  For i = 1 To UBound(b) - 1
    If Not dic.exists(b(i, 2)) Then
      y = y + 1
      dic(b(i, 2)) = y
      c(y, 1) = b(i, 1)
      c(y, 2) = b(i, 2)
      c(y, 3) = 0
    End If
  Next
  i = UBound(a)
  y = dic.Count + 1
  dic(a(i, 2)) = y
  c(y, 1) = a(i, 1)
  c(y, 2) = a(i, 2)
  c(y, 3) = a(i, 3)
 
  'Calculate balances
  For i = 1 To UBound(b)
    If dic.exists(b(i, 2)) Then
      y = dic(b(i, 2))
      c(y, 4) = c(y, 4) + b(i, 4)
      c(y, 5) = c(y, 5) + b(i, 5)
      c(y, 6) = c(y, 3) + c(y, 4) - c(y, 5)
    End If
  Next
  
  Application.ScreenUpdating = False
  'Pass values to the sheet
  sh1.Range("G2", sh1.Cells(Rows.Count, "L")).Clear
  With sh1.Range("G2").Resize(dic.Count, UBound(c, 2))
    .Value = c
    .Borders.LineStyle = xlContinuous
  End With
  
  'Format cells
  sh1.Range("A" & Rows.Count).End(3).Copy
  sh1.Range("G" & Rows.Count).End(3).Resize(1, 6).PasteSpecial xlPasteFormats
  sh1.Range("G:L").HorizontalAlignment = xlCenter
  sh1.Range("I:L").NumberFormat = "#,##0.00;#,##0.00;"
  Application.ScreenUpdating = True
End Sub

🧙‍♂️
 
Upvote 0
Solution

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,657
Latest member
giadungthienduyen

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