create list by extract amounts for the last row for each sheet name

Alaa mg

Active Member
Joined
May 29, 2021
Messages
378
Office Version
  1. 2019
hello
I need extract amount for each sheet based on last row for each name .
in column B should merge date for the last row with OPENING BALANCE word for each name , column C should populate name based on sheet name , column D should brings amount from the last row in column E
and will add new sheets before BALANCES sheet and will add new data for each sheet ,then should replace new data for old data by delete data have already copied in BALANCES sheet when every time add new data in others sheets.
if there is macro will be great.
thanks




asq
ABCDE
1DATEDescribeDEBITCREDITBALANCE
205/01/2022NOT PAID12331233
306/01/2022NOT PAID12332466
407/01/2022PAID2000466
508/01/2022PAID40066
ALA



asq
ABCDE
1DATEDescribeDEBITCREDITBALANCE
208/01/20220
309/01/2022NOT PAID20002000
411/01/2022NOT PAID200010003000
511/01/2022NOT PAID200040001000
ALN


asq
ABCDE
1DATEDescribeDEBITCREDITBALANCE
211/01/20220
312/01/2022NOT PAID15001500
413/01/2022NOT PAID5002000
514/01/2022PAID200010003000
615/01/2022PAID10101990
716/01/2022PAID1000990
817/01/2022PAID9900
ALM


result


before
asq
ABCD
1ITEMDETAILESNAMESAMOUNT
2
3
4
5
6
7
8
9
10
11
12
BALANCES


after
asq
ABCD
1ITEMDETAILESNAMESAMOUNT
21OPENING BALANCE 08/01/2022ALA66
32OPENING BALANCE 11/01/2022 ALN1000
43OPENING BALANCE 17/01/2022ALM0
5
6
7
8
9
10
11
12
BALANCES
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I hope that THIS WORKBOOK does what is needed.

VBA Code:
Option Explicit

' ----------------------------------------------------------------
' Procedure Name: TransferOpeningBalances
' Purpose: Fill current balances worksheet.
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jim
' Date: 6/25/2023
' ----------------------------------------------------------------

Sub TransferOpeningBalances()

'   -----------------------------------
'           Declare Variables
'   -----------------------------------

'   Used to iterate through all worksheets.
    Dim wsLoop As Worksheet
    
'   The summary worksheet including Opening Balances for all names.
    Dim wsBalances As Worksheet

'   Cell address of upperleftmost cell in names data. It contains the header label.
    Dim sNamesAnchorCellAddress As String

'   Cell address of upperleftmost cell in Balanaces worksheet data. It contains the header label.
    Dim sBalancesAnchorCellAddress As String
    
'   Range object variable for upperleftmost cell in names worksheet.
    Dim rNamesAnchorCell As Range
    
'   Range object variable for upperleftmost cell in Balances worksheet.
    Dim rBalancesAnchorCell As Range
    
'   Used to hold the value for the last row in the data.
    Dim iLastDataRow As Long
    
'   Used to hold the value for the next available row in the Balances data.
    Dim iNextBalanceRow As Long
    
'   The date to transfer from person data worksheet to Balances data worksheet.
    Dim dDate As Date
    
'   The balance to transfer from person data worksheet to Balances data worksheet.
    Dim dBalance As Double
    
'   The name to transfer from person data worksheet to Balances data worksheet.
    Dim sName As String
    
'   Keep track of how many names have been added to the Balances worksheet.
    Dim iNames As Long
    
'   -----------------------------------
'         Initialize Variables
'   -----------------------------------
    
    sNamesAnchorCellAddress = "A1" '<= Change if upperleftmost (header) cell for data in
'                                      the Name worksheets changes.
    
    sBalancesAnchorCellAddress = "A1" '<= Change if upperleftmost (header) cell for data in
'                                         the BALANCES worksheets changes.
    
    Set wsBalances = Worksheets("BALANCES") '<= Change if name of BALANCES worksheet changes.
    
    Set rBalancesAnchorCell = wsBalances.Range(sBalancesAnchorCellAddress)
    
'   -----------------------------------
'       Clear Exising Balances Data
'   -----------------------------------
    
    iLastDataRow = wsBalances.Cells(Rows.Count, 1).End(xlUp).Row
        
'   Remove alternating row shading from Balances worksheet.
'   If there are no rows to process this causes an error. If
'   so then skip over the command.
    On Error Resume Next
    With rBalancesAnchorCell.Offset(1).Resize(iLastDataRow - 1, 4)
        .Value = ""
        .FormatConditions.Delete
    End With
    On Error GoTo 0
'   -----------------------------------
'           Process Names Data
'   -----------------------------------
    
'   Iterate through all worksheets.
    For Each wsLoop In Worksheets

'       Only process worksheets with a person's data. This assumes that there are no other
'       worksheets except name-specific person's data worksheets and BALANCES worksheet!
        If UCase(wsLoop.Name) <> UCase(wsBalances.Name) _
         Then
            Set rNamesAnchorCell = wsLoop.Range(sNamesAnchorCellAddress)
            
            iLastDataRow = wsLoop.Cells(Rows.Count, 1).End(xlUp).Row
                  
            dDate = rNamesAnchorCell.Cells(iLastDataRow, 1).Value
            
            dBalance = rNamesAnchorCell.Cells(iLastDataRow, 5).Value
            
'           Name to transfer to the Balances worksheet is based on the worksheet name
'           for a specific person.
            sName = wsLoop.Name
            
'           Increment count of names transferred to the Balances worksheet.
            iNames = iNames + 1
            
            With rBalancesAnchorCell
            
'               Put item/name number into first column.
                .Cells(iNames + 1, 1) = iNames
                
'               Put OPENING BALANCE dd/mm/yy into second column.
                .Cells(iNames + 1, 2) = "OPENING BALANCE " & Format(dDate, "dd/mm/yy")
            
'               Put name into third column.
                .Cells(iNames + 1, 3) = sName
            
'               Put balance into fourth column.
                .Cells(iNames + 1, 4) = dBalance
            
            End With
                     
        End If
            
    Next wsLoop
    
'   ------------------------------------------------
'       Conditional Formatting of Balances Data
'   ------------------------------------------------

'   This code adds conditional formatting that creates alternating rows formatting.
    iLastDataRow = wsBalances.Cells(Rows.Count, 1).End(xlUp).Row

    With rBalancesAnchorCell.Offset(1).Resize(iLastDataRow - 1, 4)
    
        .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
        
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        
        With .FormatConditions(1).Interior
            .Pattern = xlGray16
            .PatternThemeColor = xlThemeColorDark1
            .ColorIndex = xlAutomatic
            .PatternTintAndShade = -0.349986266670736
        End With
        
        .FormatConditions(1).StopIfTrue = False
    
    End With

End Sub
 
Upvote 0
Book1
ABCD
1ITEMDETAILESNAMESAMOUNT
21OPENING BALANCE 1/8/2022ALA66
32OPENING BALANCE 1/11/2022ALN1000
43OPENING BALANCE 1/17/2022ALM0
BALANCES


After putting the names, dates and last row values into balance sheets, what's your next step? Can you elaborate further more? Thank you :)

Below code will loop through all worksheets (Except Balances sheet) , finding last row,date, sheet name values and put into balances sheet

VBA Code:
Option Explicit
Option Compare Text
Sub test()
Dim i%, lrow%, k%
Dim a()
Dim b()
ReDim b(1 To 10000, 1 To 4)

Sheets("BALANCES").[a2:d10000].Clear


For i = 1 To Worksheets.Count
      With Sheets(i)
            If Sheets(i).Name <> "BALANCES" Then 'Loop except balances sheet
             lrow = .Cells(Rows.Count, "e").End(xlUp).Row 'Find the last row of the sheets value
             a = .Range(.Cells(lrow, "A"), .Cells(lrow, "e")).Value 'Store last row into array
             k = k + 1
             b(k, 1) = k
             b(k, 2) = "OPENING BALANCE " & a(1, 1) 'Date value
             b(k, 3) = Sheets(i).Name
             b(k, 4) = a(1, 5) 'Balance Value
             End If
      End With
Next i
      Sheets("BALANCES").[a2].Resize(UBound(b, 1), UBound(b, 2)).Value = b 'Call out Array
End Sub
 
Upvote 0
@OaklandJim
thanks
it automates error invalid procedure call or argument in this line
VBA Code:
.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
 
Upvote 0
@RudRud
I no know what you ask for it !
your code works as I expect, but I forgot something ,sorry!🙏
should insert Total row like this

NamesBalances.xlsm
ABCD
1ITEMDETAILSNAMESAMOUNT
21OPENING BALANCE 08/01/22ALA66.00
32OPENING BALANCE 11/01/22ALN1,000.00
43OPENING BALANCE 17/01/22ALM0.00
5TOTAL1,066.00
BALANCES
Cell Formulas
RangeFormula
D5D5=SUM(D2:D4)

 
Upvote 0
Book4
ABCD
1ITEMDETAILESNAMESAMOUNT
21OPENING BALANCE 1/8/2022ALA66
32OPENING BALANCE 1/7/2022Sheet5466
43OPENING BALANCE 1/11/2022ALN1000
54OPENING BALANCE 1/17/2022ALM0
6TOTAL1532
BALANCES

VBA Code:
Option Explicit
Option Compare Text
Sub test()
Dim i%, lrow%, k%, ttl%
Dim a()
Dim b()
ReDim b(1 To 10000, 1 To 4)

Sheets("BALANCES").[a2:d10000].Clear


For i = 1 To Worksheets.Count
      With Sheets(i)
            If Sheets(i).Name <> "BALANCES" Then 'Loop except balances sheet
             lrow = .Cells(Rows.Count, "e").End(xlUp).Row 'Find the last row of the sheets value
             a = .Range(.Cells(lrow, "A"), .Cells(lrow, "e")).Value 'Store last row into array
             k = k + 1
             b(k, 1) = k
             b(k, 2) = "OPENING BALANCE " & a(1, 1) 'Date value
             b(k, 3) = Sheets(i).Name
             b(k, 4) = a(1, 5) 'Balance Value
             ttl = a(1, 5) + ttl
             End If
      End With
    
Next i
With Sheets("BALANCES")
    .[a2].Resize(UBound(b, 1), UBound(b, 2)).Value = b 'Call out Array
    lrow = .Cells(Rows.Count, "a").End(xlUp).Row + 1
    .Cells(lrow, "A").Value = "TOTAL"
    .Cells(lrow, "d").Value = ttl
End With
  
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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