split & delete characters into multiple sheets based on helper column

Abdo

Board Regular
Joined
May 16, 2022
Messages
216
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I want to split SH1 into multiple sheets based on column B using helper column(G) to delete some characters .
after split data should make header name BALANCE and if the amount is credit or debit then should under BALANCE header
the the unique ID whether in helper column (G) or in column (B) will be 20 items and could be 4000 rows for SH1 sheet
and should update every sheet has already divided based on SH1 sheet and should rename sheets based on column G with considering I will add more items based on column B.
example:
sp.xlsm
ABCDEFG
1DATEACCOUNT REFDEBITCREDIT
201/04/2023PUR PURCHASE INV 00001120,000.00PUR PURCHASE
302/04/2023PUR PURCHASE INV 00001220,000.00CASH PP
403/04/2023PUR PURCHASE INV 00001320,000.00CASH SS
504/04/2023PUR PURCHASE INV 00001420,000.00PAID PUR PURCHASE
605/04/2023PUR PURCHASE INV 00001520,000.00
706/04/2023CASH PP 90001,000.00
807/04/2023CASH SS 90012,000.00
908/04/2023CASH PP 90021,200.00
1009/04/2023CASH SS 90031,200.00
1110/04/2023CASH SS 90041,300.00
1211/04/2023CASH SS 90051,400.00
1312/04/2023PAID PUR PURCHASE INV 0000201,000.00
1413/04/2023PAID PUR PURCHASE INV 0000212,000.00
1514/04/2023PAID PUR PURCHASE INV 0000221,200.00
1615/04/2023PUR PURCHASE INV 0000231,200.00
1716/04/2023PUR PURCHASE INV 0000241,300.00
1817/04/2023PUR PURCHASE INV 0000252,000.00
sh1



results
sp.xlsm
ABC
1DATEACCOUNT REFBALANCE
212/04/2023PAID PUR PURCHASE1,000.00
313/04/2023PAID PUR PURCHASE2,000.00
414/04/2023PAID PUR PURCHASE1,200.00
PAID PUR PURCHASE


sp.xlsm
ABC
1DATEACCOUNT REFBALANCE
206/04/2023CASH PP1,000.00
308/04/2023CASH PP1,200.00
CASH PP


sp.xlsm
ABC
1DATEACCOUNT REFBALANCE
207/04/2023CASH SS2,000.00
309/04/2023CASH SS1,200.00
410/04/2023CASH SS1,300.00
511/04/2023CASH SS1,400.00
CASH SS


sp.xlsm
ABC
1DATEACCOUNT REFBALANCE
201/04/2023PUR PURCHASE20,000.00
302/04/2023PUR PURCHASE20,000.00
403/04/2023PUR PURCHASE20,000.00
504/04/2023PUR PURCHASE20,000.00
605/04/2023PUR PURCHASE20,000.00
715/04/2023PUR PURCHASE1,200.00
816/04/2023PUR PURCHASE1,300.00
917/04/2023PUR PURCHASE2,000.00
PUR PURCHASE
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi @Abdo

I have a couple of doubts:
1. If the destination sheet already exists, you must put the data after the last record with data, or you must delete the data and put only the new one starting in cell A2.
2. Are all ACCOUNT REFs unique? If there are duplicates, they must be accumulated, how?

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0
Hi Dante,
r you must delete the data and put only the new one starting in cell A2.
yes ,
I don't want to repeat the same data under last record has already copied.
2. Are all ACCOUNT REFs unique? If there are duplicates, they must be accumulated, how?
I'm not sure if I understand correctly ,
do you mean should merge like this "?
example picture 2
If you mean to merge this
sp
ABC
1ITEMACCOUNT REFBALANCE
21PAID PUR PURCHASE4,200.00
PAID PUR PURCHASE

I don't mind , if you mean another thing, please just comment what do you think .
 
Upvote 0
I get it.

Try this:

VBA Code:
Sub Split_ACCOUNT_REF()
  Dim sh1 As Worksheet
  Dim a As Variant, b As Variant, c As Variant
  Dim i As Long, k As Long, m As Long
  Dim newSh As String
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Set sh1 = Sheets("sh1")
  a = sh1.Range("A2:D" & sh1.Range("B" & Rows.Count).End(3).Row).Value
  c = sh1.Range("G2:G" & sh1.Range("G" & Rows.Count).End(3).Row).Value
 
  For i = 1 To UBound(c, 1)
    newSh = c(i, 1)
    On Error Resume Next: Sheets(newSh).Delete: On Error GoTo 0
    Sheets.Add(, Sheets(Sheets.Count)).Name = newSh
    ReDim b(1 To UBound(a, 1), 1 To 3)
    m = 0
   
    For k = 1 To UBound(a, 1)
      If Left(a(k, 2), Len(newSh)) = newSh Then
        m = m + 1
        b(m, 1) = a(k, 1)
        b(m, 2) = newSh
        b(m, 3) = IIf(a(k, 3) <> "", a(k, 3), a(k, 4))
      End If
    Next
   
    With Sheets(newSh)
      sh1.Range("A1:C1").Copy .Range("A1")
      .Range("C1").Value = "BALANCE"
      .Range("A2").Resize(UBound(b, 1), 3).Value = b
    End With
   
  Next
 
  sh1.Select
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

😇
 
Upvote 0
great!
I need fixing some things :
1- I want creating borders into cells for divided sheets as are existed in SH1 when split data .
2- should also create formatting of numbers in column C,D like this #,##0.00 for divided sheets
3- it's your choice as you like , may you merge as in picture in post#2,please?
thanks
 
Upvote 0
1- I want creating borders into cells for divided sheets as are existed in SH1 when split data .
2- should also create formatting of numbers in column C,D like this #,##0.00 for divided sheets
Try:

VBA Code:
Sub Split_ACCOUNT_REF()
  Dim sh1 As Worksheet
  Dim a As Variant, b As Variant, c As Variant
  Dim i As Long, k As Long, m As Long
  Dim newSh As String
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set sh1 = Sheets("sh1")
  a = sh1.Range("A2:D" & sh1.Range("B" & Rows.Count).End(3).Row).Value
  c = sh1.Range("G2:G" & sh1.Range("G" & Rows.Count).End(3).Row).Value
  
  For i = 1 To UBound(c, 1)
    newSh = c(i, 1)
    ReDim b(1 To UBound(a, 1), 1 To 3)
    m = 0
    
    For k = 1 To UBound(a, 1)
      If Left(a(k, 2), Len(newSh)) = newSh Then
        m = m + 1
        b(m, 1) = a(k, 1)
        b(m, 2) = newSh
        b(m, 3) = IIf(a(k, 3) <> "", a(k, 3), a(k, 4))
      End If
    Next
    
    If m > 0 Then
      On Error Resume Next: Sheets(newSh).Delete: On Error GoTo 0
      Sheets.Add(, Sheets(Sheets.Count)).Name = newSh
      
      With Sheets(newSh)
        sh1.Range("A1:C1").Copy .Range("A1")
        .Range("C1").Value = "BALANCE"
        .Range("A2").Resize(m, 3).Value = b
        .Range("A1").Resize(m + 1, 3).Borders.LineStyle = xlContinuous
        .Range("A:C").EntireColumn.AutoFit
        .Range("C:C").NumberFormat = "#,##0.00"
      End With
    End If
    
  Next
  
  sh1.Select
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

🫡
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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