Variable grouping

jmthompson

Well-known Member
Joined
Mar 31, 2008
Messages
966
Good afternoon,
So I have spreadsheet data in columns A-L. Column A includes an account name and number. What I want to do is group the data by account type, which is determined by the 1st digit of the account number.

I am going to post my ugly, dirty-laundry, frankenstein code below. It's part of a much bigger macro, but at this point in the macro , a row has been inserted beneath each group of accounts and entered the name of the account type in column a.

This code is supposed to "group" the accounts by type. Here's the kicker, not all of the account types will be present each time this code is run. As it is written now, the code will work fine if every other account type is missing (accounts go from 200000 series to 400000 series to 600000 series and so on), but not if consecutive account types are missing (accounts go from 200000 series to the 500000 series).

How do I make this more flexible?

Code:
For Each c In Range("A5:A" & lastRow + 8)
    If c = "" Then
        Select Case Left(Right(c.Offset(-1, 0), 7), 1)
            Case Is = 1
                c.Value = "Balance Sheet Accounts"
            Case Is = 2
                c.Value = "Liability Accounts"
            Case Is = 3
                c.Value = "Equity Accounts"
            Case Is = 4
                c.Value = "Revenue Accounts"
            Case Is = 5
                c.Value = "Expense Accounts"
            Case Is = 6
                c.Value = "Other Operating Accounts"
            Case Is = 7
                c.Value = "Non-Operating Accounts"
            Case Is = 9
                c.Value = "Statistical Accounts"
        End Select
    End If
Next c

lastRow = Range("A" & Rows.Count).End(xlUp).Row
MyBreak1 = 0
MyBreak2 = 0
MyBreak3 = 0
MyBreak4 = 0
MyBreak5 = 0
MyBreak6 = 0
MyBreak7 = 0
MyBreak8 = 0
For Each c In Range("B5:B" & lastRow)
    If c = "" Then
        Select Case c.Offset(, -1)
            Case Is = "Balance Sheet Accounts"
                MyBreak1 = c.Row
            Case Is = "Liability Accounts"
                MyBreak2 = c.Row
            Case Is = "Equity Accounts"
                MyBreak3 = c.Row
            Case Is = "Revenue Accounts"
                MyBreak4 = c.Row
            Case Is = "Expense Accounts"
                MyBreak5 = c.Row
            Case Is = "Other Operating Accounts"
                MyBreak6 = c.Row
            Case Is = "Non-Operating Accounts"
                MyBreak7 = c.Row
            Case Is = "Statistical Accounts"
                MyBreak8 = c.Row
        End Select
    End If
Next c


If MyBreak1 = 0 Then
    MyBreak1a = 5
Else
    MyBreak1a = MyBreak1 + 1
End If

If MyBreak1 = 0 Then
    MyBreak1 = MyBreak2
End If


If MyBreak2 = 0 Then
    MyBreak2a = MyBreak1 + 1
Else
    MyBreak2a = MyBreak2 + 1
End If

If MyBreak2 = 0 Then
    MyBreak2 = MyBreak3
End If


If MyBreak3 = 0 Then
    MyBreak3a = MyBreak2 + 1
Else
    MyBreak3a = MyBreak3 + 1
End If

If MyBreak3 = 0 Then
    MyBreak3 = MyBreak4
End If


If MyBreak4 = 0 Then
    MyBreak4a = MyBreak3 + 1
Else
    MyBreak4a = MyBreak4 + 1
End If

If MyBreak4 = 0 Then
    MyBreak4 = MyBreak5
End If


If MyBreak5 = 0 Then
    MyBreak5a = MyBreak4 + 1
Else
    MyBreak5a = MyBreak5 + 1
End If

If MyBreak5 = 0 Then
    MyBreak5 = MyBreak6
End If


If MyBreak6 = 0 Then
    MyBreak6a = MyBreak5 + 1
Else
    MyBreak6a = MyBreak6 + 1
End If

If MyBreak6 = 0 Then
    MyBreak6 = MyBreak7
End If


If MyBreak7 = 0 Then
    MyBreak7a = MyBreak6 + 1
Else
    MyBreak7a = MyBreak7 + 1
End If

If MyBreak7 = 0 Then
    MyBreak7 = MyBreak8
End If


If MyBreak8 = 0 Then
    MyBreak8 = lastRow
End If


Rows("5:" & MyBreak1 - 1).Rows.Group
Rows(MyBreak1a & ":" & MyBreak2 - 1).Rows.Group
Rows(MyBreak2a & ":" & MyBreak3 - 1).Rows.Group
Rows(MyBreak3a & ":" & MyBreak4 - 1).Rows.Group
Rows(MyBreak4a & ":" & MyBreak5 - 1).Rows.Group
Rows(MyBreak5a & ":" & MyBreak6 - 1).Rows.Group
Rows(MyBreak6a & ":" & MyBreak7 - 1).Rows.Group
Rows(MyBreak7a & ":" & MyBreak8 - 1).Rows.Group
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
First you need to assign a value to the LastRow before you use it.

And you will need to perform a check to see if you need to update the account number. Here is a walkthrough on what I think you need.

Code:
   [COLOR=green]'loop throug each cell in range[/COLOR]
      [COLOR=green]'get the account number[/COLOR]
 
      [COLOR=green]'if the cell is empty[/COLOR]
         [COLOR=green]'if the account number is not numeric[/COLOR]
            [COLOR=green]'set the cell value to the cell above[/COLOR]
         [COLOR=green]'else[/COLOR]
           [COLOR=green]'select case for the account number[/COLOR]

Translated into code:
Code:
   [COLOR=darkblue]Dim[/COLOR] ws [COLOR=darkblue]As[/COLOR] Worksheet
   [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] c [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] num [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] ws = Worksheets("[COLOR=red]Sheet1[/COLOR]")
   [COLOR=red]LastRow[/COLOR] = ws.Range("A" & Rows.Count).End(xlUp).Row
   num = 0
 
   [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] c [COLOR=darkblue]In[/COLOR] ws.Range("A5:A" & [COLOR=red]LastRow[/COLOR] + 8)
      num = Left(Right(c.Offset(-1, 0), 7), 1)
 
      [COLOR=darkblue]If[/COLOR] c = "" [COLOR=darkblue]Then[/COLOR]
         [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] IsNumeric(num) [COLOR=darkblue]Then[/COLOR]
            c.Value = c.Offset(-1, 0).Value
         [COLOR=darkblue]Else[/COLOR]
            [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] num
[COLOR=#008000]'rest of the code goes here[/COLOR]

This is the only part of your code I have looked at. Hope it helps.
 
Upvote 0
Thanks for taking a look, Bertie. I only posted a snippet of my code, so you couldn't see where I had lastRow defined.

I ended up adding a helper column to use the Subtotal function and then backing out the unnecessary junk. Here is my, complete, final code. Still not pretty, but it gets the job done
Code:
Sub Refresh()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim lastRow As Long
Dim c As Range

Sheets("Report").Select
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A5:L" & lastRow).ClearContents

Sheets("Hyperion").Select
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Set wsDest = Sheets("Report")
i = wsDest.Cells(Rows.Count, 6).End(xlUp).Row
If (i = 6) And (wsDest.Cells(6, 1) = "") Then i = 0
Set SiteCol = Range("A5")
Set SiteCol = Range(SiteCol, Cells(Rows.Count, SiteCol.Column).End(xlUp))
For Each cell In SiteCol.Cells
If cell <> "" Then
i = i + 1
wsDest.Cells(i, 1).Resize(1, 1) = cell
End If
Next

Range("B2:F" & lastRow).Select
Selection.Replace What:="#Missing", Replacement:="0", LookAt:=xlPart _
        , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
For Each c In Range("B2:F" & lastRow)
    If c = "0" Then
    c.Value = 1 * 0
    End If
Next c

    

Sheets("Report").Select
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Columns("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B5:B" & lastRow).FormulaR1C1 = "=COUNTIF(R[-4]C[-1]:R1C1,R[-4]C[-1])"
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 5 Step -1
If (Cells(i, "B").Value) > 1 Then
Cells(i, "B").EntireRow.Delete
End If
Next i
Columns("B:B").Delete Shift:=xlToLeft

lastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("B5:B" & lastRow).FormulaR1C1 = "=IF(ISERR(VLOOKUP(RC[-1],Hyperion!C[-1]:C,2,FALSE)),"""",VLOOKUP(RC[-1],Hyperion!C[-1]:C,2,FALSE))"
Range("C5:C" & lastRow).FormulaR1C1 = "=IF(ISERR(VLOOKUP(RC[-2],Hyperion!C[-2]:C,3,FALSE)),"""",VLOOKUP(RC[-2],Hyperion!C[-2]:C,3,FALSE))"
Range("D5:D" & lastRow).FormulaR1C1 = "=RC[-1]-RC[-2]"
Range("E5:E" & lastRow).FormulaR1C1 = "=IF(ISERROR(RC[-1]/RC[-3]),"""",(RC[-2]-RC[-3])/RC[-3])"
Range("F5:F" & lastRow).FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-5],Hyperion!C[-5]:C[-2],4,FALSE)),"""",VLOOKUP(RC[-5],Hyperion!C[-5]:C[-2],4,FALSE))"
Range("G5:G" & lastRow).FormulaR1C1 = "=RC[-1]-RC[-5]"
Range("H5:H" & lastRow).FormulaR1C1 = "=IF(ISERROR(RC[-1]/RC[-6]),"""",RC[-1]/RC[-6])"
Range("I5:I" & lastRow).FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-8],Hyperion!C[-8]:C[-4],5,FALSE)),"""",VLOOKUP(RC[-8],Hyperion!C[-8]:C[-4],5,FALSE))"
Range("J5:J" & lastRow).FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-9],Hyperion!C[-9]:C[-4],6,FALSE)),"""",VLOOKUP(RC[-9],Hyperion!C[-9]:C[-4],6,FALSE))"
Range("K5:K" & lastRow).FormulaR1C1 = "=RC[-1]-RC[-2]"
Range("L5:L" & lastRow).FormulaR1C1 = "=IF(ISERROR(RC[-1]/RC[-3]),"""",RC[-1]/RC[-3])"
With Range("E:E,H:H,L:L")
    .Style = "Percent"
    .NumberFormat = "0.00%"
End With


Sheets("Report").Select
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B5:B" & lastRow).FormulaR1C1 = "=LEFT(RIGHT(RC[-1],7),1)"
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

For Each c In Range("C5:C" & lastRow)
    If c <> "" Then
        Select Case c
            Case Is = 1
                c.Offset(, -2).Value = "Balance Sheet Accounts"
            Case Is = 2
                c.Offset(, -2).Value = "Liability Accounts"
            Case Is = 3
                c.Offset(, -2).Value = "Equity Accounts"
            Case Is = 4
                c.Offset(, -2).Value = "Revenue Accounts"
            Case Is = 5
                c.Offset(, -2).Value = "Expense Accounts"
            Case Is = 6
                c.Offset(, -2).Value = "Other Operating Accounts"
            Case Is = 7
                c.Offset(, -2).Value = "Non-Operating Accounts"
            Case Is = 9
                c.Offset(, -2).Value = "Statistical Accounts"
        End Select
    End If
Next c
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A4:M" & lastRow).Select
    Selection.Subtotal GroupBy:=1, Function:=xlCount, TotalList:=Array(13), _
        Replace:=True, PageBreaks:=True, SummaryBelowData:=True

lastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each c In Range("B5:B" & lastRow)
    If c = "" Then
        With c
            .FormulaR1C1 = "=LEFT(RC[-1],(LEN(RC[-1])-6))"
            .Font.Bold = True
            .Offset(, 11).Value = ""
        End With
    End If
c.Value = c.Value
Next c

Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Range("A" & lastRow).Value = ""

Cells.EntireColumn.AutoFit

lastRow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.PageSetup.PrintArea = "$A$1:$L$" & lastRow
    With Sheets("Report").PageSetup
        .PrintTitleRows = "$1:$4"
        .PrintTitleColumns = ""
    End With
    With Sheets("Report").PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .CenterFooter = "&P & of &N"
        .RightFooter = "&D"
        .LeftFooter = ""
        .LeftMargin = Application.InchesToPoints(0.25)
        .RightMargin = Application.InchesToPoints(0.25)
        .TopMargin = Application.InchesToPoints(0.75)
        .BottomMargin = Application.InchesToPoints(0.75)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.3)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 20
        .PrintErrors = xlPrintErrorsDisplayed
    End With

Range("A2").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,319
Members
452,905
Latest member
deadwings

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