Single line causing issues

amrita17170909

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

The below program is used to build a report and it works well except if it encounters a single line.

I am thinking of incorporating an If statement to counter that eventuality.
VBA Code:
Sub generate_report_v_4_test() ' Generates Attachment A

    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
    Dim EndofBlock As Long
    
    
  
 
    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("Attachment A Raw")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        
      
        .Range("A2:AC" & LastRow).copy _
            Destination:=Sheets("Attachment A").Range("A6")
    End With
    
    'To delete Grand Total line incase it comes through
    With Sheets("Attachment A")
      
       LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
         If .Range("A" & LastRow) = "Grand Total" Then
          .Rows(LastRow).Delete
         End If
              
 
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
          
        .Columns("C").EntireColumn.Insert _
            Shift:=xlShiftToRight
 
          .Range("F" & LastRow).Offset(1).Value = UCase("total")
        
                      
        ' code to populate the positives at the top and the negatives at the bottom
        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( _
                        "AD" & ctr & ":AD" & EndofBlock), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
                        xlSortNormal
                    With .Sort
                        .SetRange Range("A" & ctr & ":AD" & 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("F" & ctr).Value = UCase("total")
          
              ' Else Goto Next ctr
               End If
          
            End If
        
        Next ctr
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range( _
            "AD6:AD" & EndofBlock), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With .Sort
            .SetRange Range("A6:AD" & EndofBlock)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
              
    'The below code add the line "Total" and does formatting
     LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
      
        fSumRow = 6
         While lSumRow < LastRow
            
            lSumRow = .Range("A" & fSumRow).End(xlDown).Row
          
                                
            .Range("G" & lSumRow + 1 & ":AD" & lSumRow + 1).Formula = "=SUM(G" & fSumRow & ":G" & lSumRow & ")"
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).Interior.Color = RGB(240, 240, 240)
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).Borders.LineStyle = xlContinuous
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).HorizontalAlignment = xlCenter
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).WrapText = True
          
            For ctr = fSumRow To lSumRow
                .Range("AC" & ctr).Formula = "=SUM($G" & ctr & ":$AA" & ctr & ")"
                .Range("AD" & ctr).Formula = "=SUM($G" & ctr & ":$AB" & ctr & ")"
              
            Next ctr

            fSumRow = lSumRow + 3 'condition to bring out of the loop
            
        Wend
    
    End With
    
    
  

'change -values to ()
    
    
 
    

End Sub

Can anyone help me as to what that If statement can look like?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Where does the error occur, if you step through line by line using F8 ?
 
Upvote 0
Thanks so much Michael.
VBA Code:
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
      
        fSumRow = 6
         While lSumRow < LastRow
            
            lSumRow = .Range("A" & fSumRow).End(xlDown).Row
          
                                
            .Range("G" & lSumRow + 1 & ":AD" & lSumRow + 1).Formula = "=SUM(G" & fSumRow & ":G" & lSumRow & ")"
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).Interior.Color = RGB(240, 240, 240)
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).Borders.LineStyle = xlContinuous
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).HorizontalAlignment = xlCenter
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).WrapText = True
          
            For ctr = fSumRow To lSumRow
                .Range("AC" & ctr).Formula = "=SUM($G" & ctr & ":$AA" & ctr & ")"
                .Range("AD" & ctr).Formula = "=SUM($G" & ctr & ":$AB" & ctr & ")"
              
            Next ctr

            fSumRow = lSumRow + 3 'condition to bring out of the loop
            
        Wend
    
    End With
 
Upvote 0
I was thinking if I can introduce a If loop that might help.

By that I mean the If loop will check for the word "total" in E column and would know that that is the end of the loop
 
Upvote 0
It should look like this :
1581044017381.png



but looks like this in certain sections :

1581044131244.png
 
Upvote 0
But you still haven't told us where the problem occurs in the code ??
 
Upvote 0
Hi Michael,

On further analysis I realise it is not one single line but the following loop which causes an issue :

VBA Code:
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
      
        fSumRow = 6
         While lSumRow < LastRow
            
            lSumRow = .Range("A" & fSumRow).End(xlDown).Row
          
                                
            .Range("G" & lSumRow + 1 & ":AD" & lSumRow + 1).Formula = "=SUM(G" & fSumRow & ":G" & lSumRow & ")"
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).Interior.Color = RGB(240, 240, 240)
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).Borders.LineStyle = xlContinuous
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).HorizontalAlignment = xlCenter
            .Range("F" & lSumRow + 1 & ":AD" & lSumRow + 1).WrapText = True
          
            For ctr = fSumRow To lSumRow
                .Range("AC" & ctr).Formula = "=SUM($G" & ctr & ":$AA" & ctr & ")"
                .Range("AD" & ctr).Formula = "=SUM($G" & ctr & ":$AB" & ctr & ")"
              
            Next ctr

            fSumRow = lSumRow + 3 'condition to bring out of the loop
            
        Wend
    
    End With

Hope it makes sense

Any help is appreciated
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

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