Trying to get VBA to sum total of columns with specific text and place combined totals in Message Box

BBobbyD

New Member
Joined
Jun 3, 2015
Messages
3
Using Windows 7, Microsoft Excel 2010 - VBA, I'm trying to create a macro that will format a report & then give me a message box with the totals (or sum) of certain columns that contain specific text. So far I have the macro formatting the report the way I need, & then I have the message box working correctly (only if I manually enter the sum of the needed columns in a separate cell & define the cell as whatever the columns contain), but I would like the macro to sum all values within a column that contains specific text & input the sum in a empty cell, then defining that cell so that it may be used for the message box.
The reports I'm working with contain headers (typically ranging between B1-AZ1 after the initial formatting has been done) such as "expense 1" "expense 2" "expense 3" "7% tax of expense 2" "4% tax of expense 3" "4% tax of expense 1" etc., and within each column are the specific amounts for various clients. An example of these amounts would be "Client 1" "Expense 2" $525.15. Column A has the clients all numbered, ranging anywhere from 1 to 1000+ (depending on the number of clients in a given report.

Given this information (assuming it all makes sense), this is the current code I am using for the formatting & then for the message box as stated above:
'
ActiveCell.Offset(2, 0).Rows("1:1000").EntireRow.Select
Selection.Delete Shift:=xlUp
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Rows("2:3").Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Rows("1:1").RowHeight = 38.25
Rows("1:1").Select
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
MsgBox "# of Clients: " & Range("FinalNumClients").Value & vbCrLf & "Total 7% Tax: " & Range("TaxCityOne").Value & vbCrLf & "Total 4% Tax: " & Range("TaxCityTwo").Value, vbOKOnly, "Check Total"

'
End Sub

Unfortunately I have not been able to figure out if VBA can sum all values within multiple columns, depending on if those columns contain specific text such as "Tax" or "Expense," & then enter the sum of all applicable columns (based on if they have the specified text) into an empty cell that could then be defined as needed. If anyone knows a way that this could be done that could work with the current code above I would greatly appreciate it! TIA!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I have made a slight change to the initial code.
Now it automatically formats without having to select the last cell of the given report (which is why I had it originally start with "ActiveCell.Offset(2,0).Rows("1:1000").EntireRow.Select")
With this given change, I was wondering if anyone knew how to select all cells within one or two specific rows (such as .Rows("1:2")) that contain certain text so that I can work on defining the selected range which will then be used to populate data in the message box at the end of my previous code?
 
Upvote 0
...or does anyone know of a way to have VBA sum the total value of a column that contains specific text?
For example, A1 is titled "Dog", & there are random numbers in the cells below A1 (A2:A50). I want to see if VBA can sum the numbers in the entire column(s) that contains "Dog" & either place it in a far away cell (such as A1000) so that it can be defined later on, or put it in another way that can be used in a message box. If anyone know's how to do the latter, my main goal is to try to get the message box to present the totals of columns containing specific text. Such as: MsgBox "Total Dogs: " & Range("SumDog").Value
 
Upvote 0

Forum statistics

Threads
1,206,830
Messages
6,075,109
Members
446,122
Latest member
sambee66

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