Macro to change way of showing data by transporting for each account name

Omar M

New Member
Joined
Jan 11, 2024
Messages
40
Office Version
  1. 2019
Platform
  1. Windows
Hello
in RESULT sheet there is no any data, but what I want making report based on sheet1
it should populate accounts names in column B by brings from row1 from column G to last column( every time add new account name so will insert new columns)
as to column C,D for DEBIT,CREDIT will merge for each account name in SHEET1 and the BALANCE column will subtract DEBIT from CREDIT.
I would macro to deal about 7000 rows for SHEET1 when brings data to make macro really fast when brings data.


ACCOUNTS DAILY.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1DATECONSTRAIN NODESCRIPTIONBALANCING CASETOTALBANKSAFEPURCHASESELLINGCAPITALPURCHASE RETURNINGSELLING RETURNINGRECEIVABLECREDITOR
2DEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDIT
311/08/20241DEPOSIT CAPITALBALANCED2,000.002,000.002,000.002,000.00
411/08/20242BUYING GOODSBALANCED3,500.003,500.001,500.002,000.003,500.00
511/08/20243SELLING GOODSNOT BALANCED10,000.0015,000.0010,000.0015,000.00
611/08/20244SELLING GOODSBALANCED3,000.003,000.003,000.003,000.00
711/08/20245INCREASE CAPITALBALANCED15,000.0015,000.0015,000.0015000
812/08/20246DECREASE CAPITALBALANCED3,000.003,000.003,000.003,000.00
sheet1



should be like this
ACCOUNTS DAILY.xlsm
ABCDE
1ITEMDEBITDEBITCREDITBALANCE
21BANK10,000.001,500.008,500.00
32SAFE20,000.005,000.0015,000.00
43PURCHASE3,500.000.003,500.00
54SELLING0.0018,000.00-18,000.00
65CAPITAL3,000.0017,000.00-14,000.00
76PURCHASE RETURNING0.000.000.00
87SELLING RETURNING0.000.000.00
98RECEIVABLE0.000.000.00
109CREDITOR0.000.000.00
RESULT
Cell Formulas
RangeFormula
E2:E10E2=C2-D2

I hope finding assistance as DanteAmore did it in earlier .
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Do you need a macro? Could you do it with formulas something like this?

Cell Formulas
RangeFormula
A2:A12A2=IF(B2="","",COUNTA(B$2:B2))
B2:B12B2=INDEX(Sheet1!G$1:AZ$1,ROWS(B$2:B2)*2-1)&""
C2:C12C2=IF(A2="","",SUMPRODUCT((Sheet1!G$1:AZ$1=$B2)*(Sheet1!G$2:AZ$2=C$1)*Sheet1!G$3:AZ$10000))
D2:D12D2=IF(A2="","",SUMPRODUCT((Sheet1!G$1:AY$1=$B2)*(Sheet1!H$2:AZ$2=D$1)*Sheet1!H$3:AZ$10000))
E2:E12E2=IF(A2="","",C2-D2)
 
Upvote 0
If you decide you do need a macro see if this works for you.
It assumes that the Sheet RESULT already exists and already has the heading.

VBA Code:
Sub ReformatData()

    Dim wsSrc As Worksheet, wsResult As Worksheet
    Dim rngSrc As Range, arrSrc As Variant
    Dim arrResult As Variant, itemResult As Long
    Dim rngResult As Range
    Dim lrResult As Long
    Dim i As Long, j As Long
    
    Set wsSrc = Worksheets("Sheet1")
    Set rngSrc = wsSrc.Range("A1").CurrentRegion
    arrSrc = rngSrc.Value
    
    Set wsResult = Worksheets("Result")
    Set rngResult = wsResult.Range("A1").CurrentRegion
    rngResult.Offset(1).ClearContents
    
    Dim dictSrc As Object, dictKey As String
    
    Set dictSrc = CreateObject("Scripting.dictionary")
    dictSrc.CompareMode = vbTextCompare
    
    ' Load details range into Dictionary
    For j = Columns("G").Column To UBound(arrSrc, 2) Step 2
        If arrSrc(1, j) <> "" Then
            dictKey = arrSrc(1, j)
            If Not dictSrc.exists(dictKey) Then
                itemResult = itemResult + 1
                dictSrc(dictKey) = itemResult
                arrSrc(1, j + 1) = arrSrc(1, j)
            End If
        End If
    Next j
    
    ReDim arrResult(1 To dictSrc.Count, 1 To 4)
    
    For i = 3 To UBound(arrSrc)
        For j = Columns("G").Column To UBound(arrSrc, 2) Step 2
            itemResult = dictSrc(arrSrc(1, j))
            arrResult(itemResult, 1) = itemResult
            arrResult(itemResult, 2) = arrSrc(1, j)
            arrResult(itemResult, 3) = arrResult(itemResult, 3) + arrSrc(i, j)
            arrResult(itemResult, 4) = arrResult(itemResult, 4) + arrSrc(i, j + 1)
        Next j
    Next i
    
    rngResult.Offset(1).Resize(UBound(arrResult), UBound(arrResult, 2)).Value = arrResult
    rngResult.Offset(1, 4).Resize(UBound(arrResult), 1).FormulaR1C1 = "=RC[-2]-RC[-1]"

End Sub
 
Upvote 0
Thanks Alex.
not really sure why shows error subscript out of range in this line !
VBA Code:
arrResult(itemResult, 4) = arrResult(itemResult, 4) + arrSrc(i, j + 1)
by the way I made sure sheets names in the workbook with your code.
 
Upvote 0
What is the value of i & j & itemResult when it errors out ?

If it helps copy this into the immediate window and hit enter.
VBA Code:
? i,j,itemResult
 
Upvote 0
1) The code needs to have errored out and you hit the debug button.
2) Copy what I had including the "?" into the immediate window (Ctrl+G if its not visible) then hit enter.

I am loging off for the night and it is likely to be a data issue, so if the above doesn't help you find the issue please share a copy of your workbook via a sharing platform such as dropbox, google drive etc and make sure the workbook (if you've trimmed it down) still has the issue and that it is made available to anyone with the link.
 
Upvote 0
do you mean like this
as.PNG



and running the code?
if it's so doesn't show anything.
 
Upvote 0
My guess is that you have an additional column that is detected by CurrentRegion.

Try replacing:
Rich (BB code):
    For i = 3 To UBound(arrSrc)
        For j = Columns("G").Column To UBound(arrSrc, 2) Step 2
            itemResult = dictSrc(arrSrc(1, j))
            arrResult(itemResult, 1) = itemResult
            arrResult(itemResult, 2) = arrSrc(1, j)
            arrResult(itemResult, 3) = arrResult(itemResult, 3) + arrSrc(i, j)
            arrResult(itemResult, 4) = arrResult(itemResult, 4) + arrSrc(i, j + 1)
        Next j
    Next i

With this:
VBA Code:
    For i = 3 To UBound(arrSrc)
        For j = Columns("G").Column To UBound(arrSrc, 2)
        
        If UCase(arrSrc(2, j)) = "DEBIT" Or UCase(arrSrc(2, j)) = "CREDIT" Then
            itemResult = dictSrc(arrSrc(1, j))
            arrResult(itemResult, 1) = itemResult
            arrResult(itemResult, 2) = arrSrc(1, j)
            
            If UCase(arrSrc(2, j)) = "DEBIT" Then
                arrResult(itemResult, 3) = arrResult(itemResult, 3) + arrSrc(i, j)
            Else
                 arrResult(itemResult, 4) = arrResult(itemResult, 4) + arrSrc(i, j)
            End If
        End If
        Next j
    Next i
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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