Format VVBA Subtotals in a Dynamic Range

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
I have VBA to sort vendor names and amount in a spreadsheet. The code sorts alpha and subtotals each account. My question is how do I alter the code so only the subtotals are bold with comma format and a top border?

Private Sub cmdReport_Click()
Dim WSsrc As Worksheet
Dim WSReport As Worksheet
Dim LRSrc As Long
Dim LRrep As Long
Dim LastRow As Long
Dim LastCol As Long
Dim aCol As Long
Dim C As Range
Dim A As Long
Dim Cnt As Long
Dim firstAddress As String
Dim eTotal As Double
'Define worksheet source
Set WSsrc = Worksheets("POMEC")
'Test if there's a selection in the combobox.
If Me.cmbCat.ListIndex > -1 Then
'Add a new worksheet
Set WSReport = Worksheets.Add(before:=Worksheets(1))
With WSReport
'Check sheetname and keep adding 1 until not found.
If SheetExists("Report " & Me.cmbCat) Then
Cnt = 1
Do
If SheetExists("Report " & Me.cmbCat & " - " & Cnt) Then
Cnt = Cnt + 1
Else
.Name = "Report " & Me.cmbCat & " - " & Cnt
Exit Do
End If
Loop
Else
.Name = "Report " & Me.cmbCat
End If

'Headers
With .PageSetup
.Orientation = xlLandscape
.RightHeader = "&LPage &P"
.LeftHeader = "&L" & Date
.CenterHeader = "POMEC Account Transactions"
End With

'Format certain columns
.Columns(1).NumberFormat = "@"
.Columns(2).NumberFormat = "mm/dd/yy"
.Columns(5).NumberFormat = "$#.##"
'Headers
.Range("A1") = "Number"
.Range("B1") = "Date"
.Range("C1") = "Payee"
.Range("D1") = "Category"
.Range("E1") = "Amount"
'Borders
With .Range("A1:E1")
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
.Font.Bold = True
End With
End With
'Last column of source sheet.
LastCol = WSsrc.Cells(3, WSsrc.Columns.Count).End(xlToLeft).Column
If Me.cmbCat.ListIndex = 0 Then
aCol = LastCol - 1
Else
'The column stored in combobox.
aCol = Me.cmbCat.Column(1, Me.cmbCat.ListIndex)
End If
'Starting row number of destination sheet.
LRSrc = 4
For A = 6 To aCol Step 2
With WSsrc
'Last row of source sheet.
LastRow = .Cells(.Rows.Count, A).End(xlUp).Row
'Define search range.
With .Range(.Cells(15, A), .Cells(LastRow, A))
Set C = .Find("*", LookIn:=xlValues)
If Not C Is Nothing Then
'Record starting found address.
firstAddress = C.Address
'Replace the line break with a dash from Column header.
WSReport.Range("A" & LRSrc) = Replace(WSsrc.Cells(3, A), Chr(10), "-")
'Bold
WSReport.Range("A" & LRSrc).Font.Bold = True
LRSrc = LRSrc + 1
Do
'Found. Add to destination sheet.
'Number
WSReport.Cells(LRSrc, 1) = WSsrc.Cells(C.Row, 2)
'Date
WSReport.Cells(LRSrc, 2) = WSsrc.Cells(C.Row, 1)
'Payee
WSReport.Cells(LRSrc, 3) = WSsrc.Cells(C.Row, 3)
'Category
WSReport.Cells(LRSrc, 4) = Replace(WSsrc.Cells(3, A), Chr(10), "-")
'Amount
WSReport.Cells(LRSrc, 5) = Format(Val(C), " ($* #,##0.00 ); ($* (#,##0.00); ($* ""-""?? ); (@_)")
eTotal = eTotal + Val(C)
'Increment Destination row
LRSrc = LRSrc + 1
'Look for another amount in column.
Set C = .FindNext(C)
Loop While Not C Is Nothing And C.Address <> firstAddress
End If
End With
End With
WSReport.Cells(LRSrc, 5) = Format(eTotal, " ($* #,##0.00 ); ($* (#,##0.00); ($* ""-""?? ); (@_)")
eTotal = 0
'Increment Destination category row
LRSrc = LRSrc + 2
Next
'Autofit columns in destination sheet.
WSReport.Range("A4:E" & LRSrc).Columns.AutoFit
End If
WSReport.Range("A:A", "D:D").WrapText = False
Unload Me
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
if you could post a test or a sample file it would be great. try the following on a test file and see if it works for you.

Code:
[COLOR=#333333]Private Sub cmdReport_Click()[/COLOR]
[COLOR=#333333]Dim WSsrc As Worksheet[/COLOR]
[COLOR=#333333]Dim WSReport As Worksheet[/COLOR]
[COLOR=#333333]Dim LRSrc As Long[/COLOR]
[COLOR=#333333]Dim LRrep As Long[/COLOR]
[COLOR=#333333]Dim LastRow As Long[/COLOR]
[COLOR=#333333]Dim LastCol As Long[/COLOR]
[COLOR=#333333]Dim aCol As Long[/COLOR]
[COLOR=#333333]Dim C As Range[/COLOR]
[COLOR=#333333]Dim A As Long[/COLOR]
[COLOR=#333333]Dim Cnt As Long[/COLOR]
[COLOR=#333333]Dim firstAddress As String[/COLOR]
[COLOR=#333333]Dim eTotal As Double[/COLOR]
[COLOR=#333333]'Define worksheet source[/COLOR]
[COLOR=#333333]Set WSsrc = Worksheets("POMEC")[/COLOR]
[COLOR=#333333]'Test if there's a selection in the combobox.[/COLOR]
[COLOR=#333333]If Me.cmbCat.ListIndex > -1 Then[/COLOR]
[COLOR=#333333]'Add a new worksheet[/COLOR]
[COLOR=#333333]Set WSReport = Worksheets.Add(before:=Worksheets(1))[/COLOR]
[COLOR=#333333]With WSReport[/COLOR]
[COLOR=#333333]'Check sheetname and keep adding 1 until not found.[/COLOR]
[COLOR=#333333]If SheetExists("Report " & Me.cmbCat) Then[/COLOR]
[COLOR=#333333]Cnt = 1[/COLOR]
[COLOR=#333333]Do[/COLOR]
[COLOR=#333333]If SheetExists("Report " & Me.cmbCat & " - " & Cnt) Then[/COLOR]
[COLOR=#333333]Cnt = Cnt + 1[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333].Name = "Report " & Me.cmbCat & " - " & Cnt[/COLOR]
[COLOR=#333333]Exit Do[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Loop[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333].Name = "Report " & Me.cmbCat[/COLOR]
[COLOR=#333333]End If[/COLOR]

[COLOR=#333333]'Headers[/COLOR]
[COLOR=#333333]With .PageSetup[/COLOR]
[COLOR=#333333].Orientation = xlLandscape[/COLOR]
[COLOR=#333333].RightHeader = "&LPage &P"[/COLOR]
[COLOR=#333333].LeftHeader = "&L" & Date[/COLOR]
[COLOR=#333333].CenterHeader = "POMEC Account Transactions"[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]'Format certain columns[/COLOR]
[COLOR=#333333].Columns(1).NumberFormat = "@"[/COLOR]
[COLOR=#333333].Columns(2).NumberFormat = "mm/dd/yy"[/COLOR]
[COLOR=#333333].Columns(5).NumberFormat = "$#.##"[/COLOR]
[COLOR=#333333]'Headers[/COLOR]
[COLOR=#333333].Range("A1") = "Number"[/COLOR]
[COLOR=#333333].Range("B1") = "Date"[/COLOR]
[COLOR=#333333].Range("C1") = "Payee"[/COLOR]
[COLOR=#333333].Range("D1") = "Category"[/COLOR]
[COLOR=#333333].Range("E1") = "Amount"[/COLOR]
[COLOR=#333333]'Borders[/COLOR]
[COLOR=#333333]With .Range("A1:E1")[/COLOR]
[COLOR=#333333]With .Borders(xlEdgeBottom)[/COLOR]
[COLOR=#333333].LineStyle = xlContinuous[/COLOR]
[COLOR=#333333].Weight = xlThin[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333].Font.Bold = True[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]'Last column of source sheet.[/COLOR]
[COLOR=#333333]LastCol = WSsrc.Cells(3, WSsrc.Columns.Count).End(xlToLeft).Column[/COLOR]
[COLOR=#333333]If Me.cmbCat.ListIndex = 0 Then[/COLOR]
[COLOR=#333333]aCol = LastCol - 1[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]'The column stored in combobox.[/COLOR]
[COLOR=#333333]aCol = Me.cmbCat.Column(1, Me.cmbCat.ListIndex)[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]'Starting row number of destination sheet.[/COLOR]
[COLOR=#333333]LRSrc = 4[/COLOR]
[COLOR=#333333]For A = 6 To aCol Step 2[/COLOR]
[COLOR=#333333]With WSsrc[/COLOR]
[COLOR=#333333]'Last row of source sheet.[/COLOR]
[COLOR=#333333]LastRow = .Cells(.Rows.Count, A).End(xlUp).Row[/COLOR]
[COLOR=#333333]'Define search range.[/COLOR]
[COLOR=#333333]With .Range(.Cells(15, A), .Cells(LastRow, A))[/COLOR]
[COLOR=#333333]Set C = .Find("*", LookIn:=xlValues)[/COLOR]
[COLOR=#333333]If Not C Is Nothing Then[/COLOR]
[COLOR=#333333]'Record starting found address.[/COLOR]
[COLOR=#333333]firstAddress = C.Address[/COLOR]
[COLOR=#333333]'Replace the line break with a dash from Column header.[/COLOR]
[COLOR=#333333]WSReport.Range("A" & LRSrc) = Replace(WSsrc.Cells(3, A), Chr(10), "-")[/COLOR]
[COLOR=#333333]'Bold[/COLOR]
[COLOR=#333333]WSReport.Range("A" & LRSrc).Font.Bold = True[/COLOR]
[COLOR=#333333]LRSrc = LRSrc + 1[/COLOR]
[COLOR=#333333]Do[/COLOR]
[COLOR=#333333]'Found. Add to destination sheet.[/COLOR]
[COLOR=#333333]'Number[/COLOR]
[COLOR=#333333]WSReport.Cells(LRSrc, 1) = WSsrc.Cells(C.Row, 2)[/COLOR]
[COLOR=#333333]'Date[/COLOR]
[COLOR=#333333]WSReport.Cells(LRSrc, 2) = WSsrc.Cells(C.Row, 1)[/COLOR]
[COLOR=#333333]'Payee[/COLOR]
[COLOR=#333333]WSReport.Cells(LRSrc, 3) = WSsrc.Cells(C.Row, 3)[/COLOR]
[COLOR=#333333]'Category[/COLOR]
[COLOR=#333333]WSReport.Cells(LRSrc, 4) = Replace(WSsrc.Cells(3, A), Chr(10), "-")[/COLOR]
[COLOR=#333333]'Amount[/COLOR]
[COLOR=#333333]WSReport.Cells(LRSrc, 5) = Format(Val(C), " ($* #,##0.00 ); ($* (#,##0.00); ($* ""-""?? ); (@_)")[/COLOR]
[COLOR=#333333]eTotal = eTotal + Val(C)[/COLOR]
[COLOR=#333333]'Increment Destination row[/COLOR]
[COLOR=#333333]LRSrc = LRSrc + 1[/COLOR]
[COLOR=#333333]'Look for another amount in column.[/COLOR]
[COLOR=#333333]Set C = .FindNext(C)[/COLOR]
[COLOR=#333333]Loop While Not C Is Nothing And C.Address <> firstAddress[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]WSReport.Cells(LRSrc, 5) = Format(eTotal, " ($* #,##0.00 ); ($* (#,##0.00); ($* ""-""?? ); (@_)")
[/COLOR]WSReport.Range(WSReport.Cells(LRSrc, 1), WSReport.Cells(LRSrc, 5)).Font.Bold = False
[COLOR=#333333]eTotal = 0[/COLOR]
[COLOR=#333333]'Increment Destination category row[/COLOR]
[COLOR=#333333]LRSrc = LRSrc + 2[/COLOR]
[COLOR=#333333]Next[/COLOR]
[COLOR=#333333]'Autofit columns in destination sheet.[/COLOR]
[COLOR=#333333]WSReport.Range("A4:E" & LRSrc).Columns.AutoFit[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]WSReport.Range("A:A", "D:D").WrapText = False[/COLOR]
[COLOR=#333333]Unload Me[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
That did not work for me. It ran but there are no formatting changes.

How do I post a file copy? Can I e-mail it you?
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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