VBA to add If statement

mbesspiata

Board Regular
Joined
Aug 28, 2009
Messages
100
I need to know how and where to insert an IF statement to this part of my VBA to check if cell b5 is blank. If it's blank it should be cell c5.
How would I do that?


Dim lLastrow As Long
Dim vResult As Variant

With ActiveSheet
lLastCol = .Cells(5, Columns.Count).End(xlToLeft).Column
If lLastCol < 3 Then Exit Sub
vResult = Application.WorksheetFunction.Sum(Range("b5", .Cells(5, lLastCol - 1)))
.Range("b15").FormulaR1C1 = vResult
End With
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Why do you need an If statement?
if B5 is blank the Sum will simply not add anything to the other values.
 
Upvote 0
I need this because row 5 in my pivot table is for headers indicating plan numbers. My basic data I'm pulling from for totals having a plan number can have lines of information that do not have a plan number associated with them. Therefore the first column of my pivot table (row 5) could be blank and I would want to start my count of those items in c5 instead of b5. Hope that makes sense.

[TABLE="width: 420"]
<colgroup><col width="155" style="width: 116pt; mso-width-source: userset; mso-width-alt: 5668;"> <col width="113" style="width: 85pt; mso-width-source: userset; mso-width-alt: 4132;"> <col width="136" style="width: 102pt; mso-width-source: userset; mso-width-alt: 4973;"> <col width="156" style="width: 117pt; mso-width-source: userset; mso-width-alt: 5705;"> <tbody>[TR]
[TD="width: 155, bgcolor: transparent"]STAT[/TD]
[TD="width: 113, bgcolor: transparent"](Multiple Items)[/TD]
[TD="width: 136, bgcolor: transparent"][/TD]
[TD="width: 156, bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]DYSOLD[/TD]
[TD="bgcolor: transparent"](Multiple Items)[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]Count of Seizure Number[/TD]
[TD="bgcolor: transparent"]DESTPLN#[/TD]
[TD="bgcolor: transparent"] [/TD]
[TD="bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]Region[/TD]
[TD="bgcolor: transparent"] [/TD]
[TD="bgcolor: transparent"] CA-17-0072(HI-UWR)[/TD]
[TD="bgcolor: transparent"] CA-17-0092(RIV-Butane)[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]California - West[/TD]
[TD="bgcolor: transparent, align: right"]259[/TD]
[TD="bgcolor: transparent, align: right"]14[/TD]
[TD="bgcolor: transparent, align: right"]6[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]Florida - Southeast[/TD]
[TD="bgcolor: transparent, align: right"]534[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]New Jersey - Northeast[/TD]
[TD="bgcolor: transparent, align: right"]289[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]Texas - Central[/TD]
[TD="bgcolor: transparent, align: right"]63[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]Grand Total[/TD]
[TD="bgcolor: transparent, align: right"]1145[/TD]
[TD="bgcolor: transparent, align: right"]14[/TD]
[TD="bgcolor: transparent, align: right"]6[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Not really, if B5 is blank it won't make any difference to the Sum.
That said I know nothing about Pivots as I've never used them.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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