Update the table so negative values at the top

amrita17170909

Board Regular
Joined
Dec 11, 2019
Messages
74
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hi All,

I am generating a report which looks like below at the moment :

1579834698967.png


I am using the below program to generate it:

VBA Code:
Sub generate_report_v_4_test() ' step 7

    Dim LastRow As Long, ctr As Long, fSumRow As Long, lSumRow As Long
    Dim SwapAry As Variant
    Dim SwapAry1 As Variant
    Dim cCel As Range
 
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
 
    If SheetExists("Attachment A") Then
        Sheets("Attachment A").Delete
    End If
 
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Attachment A"
 
    With Sheets("Table 4")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        
      
        .Range("A2:AB" & LastRow).copy _
            Destination:=Sheets("Attachment A").Range("A6")
    End With
    
    'To delete Grand Total
    With Sheets("Attachment A")
       LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
       If .Range("A" & LastRow) = "Grand Total" Then .Rows(LastRow).Delete
      
    End With
 
    With Sheets("Attachment A")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
          
        .Columns("C").EntireColumn.Insert _
            Shift:=xlShiftToRight
 
        .Range("G" & LastRow).Offset(1).Value = UCase("total")
        
       ' This loop finds all the BRN and adds a blank between other BRN
        For ctr = LastRow To 5 Step -1
            If .Range("A" & ctr).Value <> .Range("A" & ctr).Offset(-1).Value Then
            
              If .Range("A" & ctr).Offset(-1).Value <> "" Then
              
            ' blank condition if offset -1 is blank non numeric then go to end if
                .Range(ctr & ":" & ctr + 1).EntireRow.Insert _
                    Shift:=xlShiftDown
                    
                .Range("G" & ctr).Value = UCase("total")
            
                
              ' Else Goto Next ctr
               End If
            
            End If
        Next ctr
      
      
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
       ' MsgBox (LastRow)
      
        fSumRow = 6
      
        While lSumRow < LastRow
            lSumRow = .Range("A" & fSumRow).End(xlDown).Row
           ' MsgBox (lSumRow)
          
            .Range("H" & lSumRow + 1 & ":AE" & lSumRow + 1).Formula = "=SUM(H" & fSumRow & ":H" & lSumRow & ")"
            .Range("G" & lSumRow + 1 & ":AE" & lSumRow + 1).Interior.Color = RGB(240, 240, 240)
            .Range("G" & lSumRow + 1 & ":AE" & lSumRow + 1).Borders.LineStyle = xlContinuous
            .Range("G" & lSumRow + 1 & ":AE" & lSumRow + 1).HorizontalAlignment = xlCenter
            .Range("G" & lSumRow + 1 & ":AE" & lSumRow + 1).WrapText = True
          
            For ctr = fSumRow To lSumRow
                .Range("AD" & ctr).Formula = "=SUM($H" & ctr & ":$AB" & ctr & ")"
                .Range("AE" & ctr).Formula = "=SUM($H" & ctr & ":$AC" & ctr & ")"
                
             ' If .Range("AE" & ctr).Value > Range("A" & ctr).Offset(-1).Value Then
              
              'SwapAry=
              
              
            
            Next ctr

            fSumRow = lSumRow + 3 'condition to bring out of the loop
           ' MsgBox (fSumRow)
        Wend
    End With
 
    Call report_aesthetics_1_test
 
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub


I would like an additional step :
1. Checks the values in column AE for all rows having the same ID in column A
2. The rows from D to AE will be in ascending order.

so e.g if values in column AE are as per below

-1
-2
5
6
3

Then the program is going to make all rows as per ascending order :

-2
-1
3
5
6


I hope this makes sense.

Amrita
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Well, a quick query is ... for this:
1. Checks the values in column AE for all rows having the same ID in column A

... you seem to have merged parts of column A already, so how is this supposed to work?
 
Upvote 0
I am able to apply the logic before the merge happens in column A .

I am unsure what logic can be used.
 
Upvote 0
Hard to test without having the data/formulas etc but I think you should be able to add something like this at the end & it shouldn't matter that column A is already merged.
I am assuming ..
- Column AE contains formulas
- Column AE has nothing in the 'TOTAL' rows & the rows below (eg rows 11 & 12, 20 & 21, ..)

Make sure you test in a copy of the worksheet. :)

VBA Code:
Dim rA As Range

For Each rA In Range("AE7", Range("AE" & Rows.Count).End(xlUp)).SpecialCells(xlFormulas).Areas
  Intersect(rA.EntireRow, Columns("F:AE")).Sort Key1:=rA.Cells(1), Order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
Next rA

Edit: Reading again, you may need to change that 'Intersect' part to start at column D, not F? (I started at F since that seemed to be the first column with any data after the merged cells columns)
 
Last edited:
Upvote 0
Replace your ctr loop with this:

Code:
        ' start
        EndofBlock = LastRow
       ' This loop finds all the BRN and adds a blank between other BRN
        For ctr = LastRow To 5 Step -1
            If .Range("A" & ctr).Value <> .Range("A" & ctr).Offset(-1).Value Then
           
              If .Range("A" & ctr).Offset(-1).Value <> "" Then
                   
                    .Sort.SortFields.Clear
                    .Sort.SortFields.Add Key:=Range( _
                        "AE" & ctr & ":AE" & EndofBlock), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                        xlSortNormal
                    With .Sort
                        .SetRange Range("A" & ctr & ":AE" & EndofBlock)
                        .Header = xlGuess
                        .MatchCase = False
                        .Orientation = xlTopToBottom
                        .SortMethod = xlPinYin
                        .Apply
                    End With
                    EndofBlock = ctr - 1
                   
            ' blank condition if offset -1 is blank non numeric then go to end if
                .Range(ctr & ":" & ctr + 1).EntireRow.Insert _
                    Shift:=xlShiftDown
                   
                .Range("G" & ctr).Value = UCase("total")
           
              ' Else Goto Next ctr
               End If
           
            End If
        Next ctr
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range( _
            "AE6:AE" & EndofBlock), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With .Sort
            .SetRange Range("A6:AE" & EndofBlock)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
 
Upvote 0
Thanks Glen and Peter for your help previously.

I had to amend my previous code as it was not populating correctly. The new code is as per below are you able to advise how I can sort the positive values to the top and negatives at the bottom
VBA Code:
Sub generate_reportA_1() ' Generates report with all entries

Dim IDtag As String
Dim IDdesc As String
Dim LastRowNo As Long
Dim StartIDRow As Long
Dim LastIDRow As Long
Dim IDloop As Long
Dim CurrentTag As String
Dim ID20Years
Dim ID20YearsTot
Dim LbID20Years As Long
Dim UbID20Years As Long
Dim ReportRow As Long

IDtag = ""
IDdesc = ""
StartIDRow = 2
LastIDRow = 0
 
'Calculate the number of rows in Table 2
LastRowNo = Worksheets("All Entries Raw").Cells(Rows.Count, 1).End(xlUp).Row

If SheetExists("Attachment A") Then
        Sheets("Attachment A").Delete
    End If
 
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Attachment A"
    
'Check if there is data in table 2
If LastRowNo < 2 Then
    MsgBox "Sorry, could not find data", vbCritical, ThisWorkbook.Name
    Exit Sub
Else
    If Worksheets("All Entries Raw").Range("A2").Value <> "" Then
        IDtag = Worksheets("All Entries Raw").Range("A2").Value
        CurrentTag = IDtag
        IDdesc = Worksheets("All Entries Raw").Range("B2").Value
    Else
        MsgBox "Sorry, no data", vbCritical, ThisWorkbook.Name
        Exit Sub
    End If
End If



ReportRow = 6

'write first line
IDtag = Worksheets("All Entries Raw").Cells(StartIDRow, 1).Value
'MsgBox (IDtag)
Worksheets("Attachment A").Cells(ReportRow, 1).Value = IDtag
Worksheets("Attachment A").Cells(ReportRow, 1).Font.Bold = True
Worksheets("Attachment A").Cells(ReportRow, 2).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 2).Value
Worksheets("Attachment A").Cells(ReportRow, 2).Font.Bold = True
Worksheets("Attachment A").Cells(ReportRow, 4).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 4).Value
Worksheets("Attachment A").Cells(ReportRow, 5).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 3).Value
Worksheets("Attachment A").Cells(ReportRow, 6).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 5).Value
ID20Years = Worksheets("All Entries Raw").Range(Cells(StartIDRow, 6).Address, Cells(StartIDRow, 29).Address).Value
Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Value = ID20Years
ID20YearsTot = ID20Years
LbID20Years = LBound(ID20Years, 2)
UbID20Years = UBound(ID20Years, 2)

'MsgBox (LbID20Years)
'MsgBox (UbID20Years)
'Set for next read
StartIDRow = StartIDRow + 1
CurrentTag = Worksheets("All Entries Raw").Cells(StartIDRow, 1).Value
ReportRow = ReportRow + 1

Do While StartIDRow <= LastRowNo

    Do While IDtag = CurrentTag
        
        Worksheets("Attachment A").Cells(ReportRow, 5).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 3).Value
        Worksheets("Attachment A").Cells(ReportRow, 4).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 4).Value
        Worksheets("Attachment A").Cells(ReportRow, 6).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 5).Value
        ID20Years = Worksheets("All Entries Raw").Range(Cells(StartIDRow, 6).Address, Cells(StartIDRow, 29).Address).Value
        
        'add to total
        For IDloop = LbID20Years To UbID20Years
            ID20YearsTot(1, IDloop) = ID20YearsTot(1, IDloop) + ID20Years(1, IDloop)
        Next IDloop
        
        Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Value = ID20Years
        StartIDRow = StartIDRow + 1
        CurrentTag = Worksheets("All Entries Raw").Cells(StartIDRow, 1).Value
        ReportRow = ReportRow + 1
    Loop
    
    'Finish putting BRN rows and add the total line
    Worksheets("Attachment A").Range(Cells(ReportRow, 6).Address).Value = "TOTAL"
     Worksheets("Attachment A").Range(Cells(ReportRow, 6).Address).Font.Bold = True ' Font should be bold for BRN and BRN description
    Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Value = ID20YearsTot
    Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Font.Bold = True ' Font should be bold for BRN and BRN description
      
        
    ReportRow = ReportRow + 1
    
    'Next BRN
    'write first line
'    StartIDRow = StartIDRow + 1
    ReportRow = ReportRow + 1
    IDtag = Worksheets("All Entries Raw").Cells(StartIDRow, 1).Value
    Worksheets("Attachment A").Cells(ReportRow, 1).Value = IDtag
    Worksheets("Attachment A").Cells(ReportRow, 1).Font.Bold = True ' Font should be bold for BRN and BRN description
    'MsgBox (IDtag)
    Worksheets("Attachment A").Cells(ReportRow, 2).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 2).Value
    Worksheets("Attachment A").Cells(ReportRow, 2).Font.Bold = True ' Font should be bold for BRN and BRN description
    Worksheets("Attachment A").Cells(ReportRow, 5).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 3).Value
    Worksheets("Attachment A").Cells(ReportRow, 4).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 4).Value
    Worksheets("Attachment A").Cells(ReportRow, 6).Value = Worksheets("All Entries Raw").Cells(StartIDRow, 5).Value
    ID20Years = Worksheets("All Entries Raw").Range(Cells(StartIDRow, 6).Address, Cells(StartIDRow, 29).Address).Value
    Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Value = ID20Years
    ID20YearsTot = ID20Years
    LbID20Years = LBound(ID20Years, 2)
    UbID20Years = UBound(ID20Years, 2)
    
    'Set for next read
    StartIDRow = StartIDRow + 1
    CurrentTag = Worksheets("All Entries Raw").Cells(StartIDRow, 1).Value
    ReportRow = ReportRow + 1
    
Loop
''Simon added as workaround ++++++++++++++++++++++++++++++++++++++++++++
 Worksheets("Attachment A").Range(Cells(ReportRow, 6).Address).Value = "TOTAL"
     Worksheets("Attachment A").Range(Cells(ReportRow, 6).Address).Font.Bold = True ' Font should be bold for BRN and BRN description
    Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Value = ID20YearsTot
    Worksheets("Attachment A").Range(Cells(ReportRow, 7).Address, Cells(ReportRow, 30).Address).Font.Bold = True ' Font should be bold for BRN and BRN description
'+++++++++++++++++++++++++++++++++++++++++++

Call sum_two_columns
'change -values to ()

Call report_aesthetics_1_test

LastRowNo1 = Worksheets("Attachment A").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Attachment A").Range(Cells(2, 7).Address, Cells(LastRowNo1, 30).Address).NumberFormat = "#,##0 ;(#,##0); -"


End Sub
 
Upvote 0
Does your sheet still look like that image in post #1?
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,797
Members
449,337
Latest member
BBV123

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