Extraction last row based on specific words and put in account column(debit,credit)

Alaa mg

Active Member
Joined
May 29, 2021
Messages
378
Office Version
  1. 2019
Hello,
I have data about 8000 rows for each sheet .
every sheet should search for CASH, BANK words then should extract amount in TOTAL row for each word and put in column DEBIT or CREDIT and calculation in column BALANCE in RESULT sheet.
ALLAA.xlsm
ABCDEFG
1DATEBATCHNAMEACRRUING/CASHQTYPRICEBALANCE
201/01/2024GHTR-00ALAAACRRUING20.00220.004,400.00
301/01/2024GHTR-01ALAAACRRUING10.00214.002,140.00
401/01/2024GHTR-02ALAAACRRUING12.00215.002,580.00
5TOTAL9,120.00
601/01/2024GHTR-00ALAACASH IN CS20010.00200.002,000.00
7TOTAL2,000.00
801/01/2024GHTR-00ALA1BANK IN BN200010.00200.002,000.00
9TOTAL2,000.00
1001/01/2024GHTR-01ALAACASH IN CS20015.00200.001,000.00
1101/01/2024GHTR-02ALAACASH IN CS200110.00210.002,100.00
12TOTAL3,100.00
SDFR
Cell Formulas
RangeFormula
G2:G4G2=E2*F2


ALLAA.xlsm
ABCDEFG
1DATEBATCHNAMEACRRUING/CASHQTYPRICEBALANCE
201/01/2024GHTR-02LAALCASH IN CS2041.00230.00230.00
301/01/2024GHTR-00LAALCASH IN CS2042.00250.00500.00
4TOTAL730.00
501/01/2024GHTR-00AMINACASH IN CS2051.00200.00200.00
601/01/2024GHTR-01AMINACASH IN CS2052.00270.00540.00
701/01/2024GHTR-02AMINACASH IN CS2051.00300.00300.00
8TOTAL1,040.00
901/01/2024GHTR-02ALA1BANK IN BN20012.00310.00620.00
10TOTAL620.00
1101/01/2024GHTR-01ALAAACRRUING2.00200.00400.00
1201/01/2024GHTR-02ALAAACRRUING3.00210.00630.00
13TOTAL1,030.00
BGHT


ALLAA.xlsm
ABCDEFG
1DATEBATCHNAMEACRRUING/CASHQTYPRICEBALANCE
201/01/2024GHTR-02ALAAACRRUING1.00215.00215.00
3TOTAL215.00
401/01/2024GHTR-00ALAACASH IN CS2031.00200.00200.00
5TOTAL200.00
601/01/2024GHTR-00ALA1BANK IN BN20022.00200.00400.00
7TOTAL400.00
802/01/2024GHTR-01ALAACASH IN CS20032.00200.00400.00
902/01/2024GHTR-02ALAACASH IN CS20033.00210.00630.00
10TOTAL1,030.00
BTRT
Cell Formulas
RangeFormula
G2G2=E2*F2



ALLAA.xlsm
ABCDEFG
1DATEBATCHNAMEACRRUING/CASHQTYPRICEBALANCE
201/01/2024GHTR-02LAALCASH IN CS2061.00230.00230.00
301/01/2024GHTR-00LAALCASH IN CS2061.00250.00250.00
4TOTAL480.00
501/01/2024GHTR-00AMINACASH IN CS2071.00200.00200.00
601/01/2024GHTR-01AMINACASH IN CS2072.00270.00540.00
701/01/2024GHTR-02AMINACASH IN CS2071.00300.00300.00
8TOTAL1,040.00
901/01/2024GHTR-02ALA1BANK IN BN20042.00310.00620.00
10TOTAL620.00
1102/01/2024GHTR-00AMINAACRRUING1.00200.00200.00
1202/01/2024GHTR-01AMINAACRRUING1.00270.00270.00
1302/01/2024GHTR-02AMINAACRRUING1.00300.00300.00
14TOTAL770.00
BFGT



ALLAA.xlsm
ABCDEF
1DATES.NACRRUING/CASHDEBITCREDITBALANCE
2
3
4
5
6
7
8
9
10
11
12
13
14
RESULT



I want this
ALLAA.xlsm
ABCDEF
1DATES.NACRRUING/CASHDEBITCREDITBALANCE
201/01/20241CASH IN CS2002,000.002,000.00
301/01/20242BANK IN BN20002,000.004,000.00
401/01/20243CASH IN CS20013,100.007,100.00
501/01/20244CASH IN CS204730.006,370.00
601/01/20245CASH IN CS2051,040.005,330.00
701/01/20246BANK IN BN2001620.004,710.00
801/01/20247CASH IN CS203200.004,910.00
901/01/20248BANK IN BN2002400.005,310.00
1002/01/20249CASH IN CS20031,030.006,340.00
1101/01/202410CASH IN CS206480.005,860.00
1201/01/202411CASH IN CS2071,040.004,820.00
1301/01/202412BANK IN BN2004620.004,200.00
14TOTAL8,730.004,530.004,200.00
RESULT
Cell Formulas
RangeFormula
F2,F14F2=D2-E2
F3:F13F3=F2+D3-E3
D14:E14D14=SUM(D2:D13)


as you see for first,third sheets will put amount in DEBIT column as to second,fourth sheets will put in CREDIT column and insert TOTAL row to sum columns for debit,credit.
will bring date and ACRRUING/CASH columns and auto numbering in column B
every time should replace data when run the code.
I don't want solution by POWER QUERY ,PIVOT TABLE , FORMULAS . just I want code
thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try:

VBA Code:
Sub Extraction_based_specific_words()
  Dim arr As Variant, sht As Variant
  Dim sh As Worksheet
  Dim ar As Range
  Dim i As Long, k As Long, nRows As Long, lr As Long
  Dim bce As Double, dbt As Double, cdt As Double
  
  Application.ScreenUpdating = False
  arr = Array("SDFR", "BGHT", "BTRT", "BFGT")   '1,2 and 3,4
  
  For i = 0 To UBound(arr)
    Set sh = Sheets(arr(i))
    nRows = nRows + sh.Range("A" & Rows.Count).End(3).Row
  Next
  ReDim b(1 To nRows, 1 To 6)
  
  For i = 0 To UBound(arr)
    Set sh = Sheets(arr(i))
  
    For Each ar In sh.Range("D2:D" & sh.Range("A" & Rows.Count).End(3).Row).SpecialCells(xlCellTypeBlanks).Areas
      If InStr(1, ar.Offset(-1).Value, "CASH", vbTextCompare) > 0 Or _
         InStr(1, ar.Offset(-1).Value, "BANK", vbTextCompare) > 0 Then
        k = k + 1
        b(k, 1) = ar.Offset(-1, -3).Value
        b(k, 2) = k
        b(k, 3) = ar.Offset(-1, 0).Value
        If i = 0 Or i = 2 Then
          b(k, 4) = ar.Offset(0, 3).Value
          dbt = dbt + b(k, 4)
        Else
          b(k, 5) = ar.Offset(0, 3).Value
          cdt = cdt + b(k, 5)
        End If
        bce = bce + b(k, 4) - b(k, 5)
        b(k, 6) = b(k, 6) + bce
        Sheets("RESULT").Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
      End If
    Next
  Next
  
  With Sheets("RESULT")
    .Range("A2:F" & Rows.Count).Clear
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    lr = .Range("A" & Rows.Count).End(3).Row + 1
    .Range("A" & lr).Resize(1, 6).Value = Array("TOTAL", , , dbt, cdt, dbt - cdt)
    .Range("A:A").NumberFormat = "dd/mm/yyyy"
    .Range("D:F").NumberFormat = "#,##0.00"
    .Range("A:F").HorizontalAlignment = xlCenter
    Range("A2:F" & lr).Borders.LineStyle = xlContinuous
  End With
  
  Application.ScreenUpdating = True
End Sub

🤗
 
Upvote 0
the code works perfect with small data , but I'm afraid will be too slow with data for about 6000 rows at least for each sheet !
did you test it?
do you have another efficient way?
 
Upvote 0
the code works perfect
(y)

do you have another efficient way?
Of course, with matrices:

VBA Code:
Sub Extraction_based_specific_words()
  Dim arr As Variant, sht As Variant
  Dim sh As Worksheet
  Dim ar As Range
  Dim i As Long, j As Long, k As Long, nRows As Long, lr As Long
  Dim bce As Double, dbt As Double, cdt As Double
  Dim a() As Variant, b As Variant
  
  Application.ScreenUpdating = False
  arr = Array("SDFR", "BGHT", "BTRT", "BFGT")   '1,2 and 3,4
  
  For i = 0 To UBound(arr)
    Set sh = Sheets(arr(i))
    nRows = nRows + sh.Range("A" & Rows.Count).End(3).Row
  Next
  ReDim b(1 To nRows, 1 To 6)
  
  For j = 0 To UBound(arr)
    Set sh = Sheets(arr(j))
    Erase a
    a = sh.Range("A1", sh.Range("G" & Rows.Count).End(3)).Value2
    For i = 2 To UBound(a, 1)
      If a(i, 1) = "TOTAL" Then
        If InStr(1, a(i - 1, 4), "CASH", vbTextCompare) > 0 Or _
           InStr(1, a(i - 1, 4), "BANK", vbTextCompare) > 0 Then
          k = k + 1
          b(k, 1) = a(i - 1, 1)
          b(k, 2) = k
          b(k, 3) = a(i - 1, 4)
          If j = 0 Or j = 2 Then
            b(k, 4) = a(i, 7)
            dbt = dbt + b(k, 4)
          Else
            b(k, 5) = a(i, 7)
            cdt = cdt + b(k, 5)
          End If
          bce = bce + b(k, 4) - b(k, 5)
          b(k, 6) = b(k, 6) + bce
        End If
      End If
    Next
  Next
  
  With Sheets("RESULT")
    .Range("A2:F" & Rows.Count).Clear
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    lr = .Range("A" & Rows.Count).End(3).Row + 1
    .Range("A" & lr).Resize(1, 6).Value = Array("TOTAL", , , dbt, cdt, dbt - cdt)
    .Range("A:A").NumberFormat = "dd/mm/yyyy"
    .Range("D:F").NumberFormat = "#,##0.00"
    .Range("A:F").HorizontalAlignment = xlCenter
    Range("A2:F" & lr).Borders.LineStyle = xlContinuous
  End With
  
  Application.ScreenUpdating = True
End Sub


🫡
 
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,107
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