VBA Line continuation

Kinseres

New Member
Joined
Dec 3, 2015
Messages
16
Hi All

I'm wondering if anyone can help me with the correct placement of line continuation in VBA. I have a piece of code that essentially just adds up the results of a number of sumifs, however, I cannot seem to find where in the individual sumifs formulas the line continuation needs to be placed to avoid syntax errors.

Any help at all would be appreciated!

Code:
WC = Sheets("Export").Range("A1").Cells(lrow + 1, 1).Value
Sheets("Export").Range("B1").Cells(lrow + 1, 1).Formula = "SUMIFS('Raw Data'!$T:$T,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 1"")+ _
SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 2"")+ _
SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 3"")+ _
SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 4"")+ _
SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 5"")+ _
SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 6"")+ _
SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 7"")"
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can't put it inside the formula string. You have to terminate the string and then continue it using & as well as the line continuation:

Code:
Sheets("Export").Range("B1").Cells(lrow + 1, 1).Formula = "SUMIFS('Raw Data'!$T:$T,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 1"")+" &  _
"SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 2"")+" &  _
"SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 3"")+" &  _
"SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 4"")+" &  _
"SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 5"")+" &  _
"SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 6"")+" &  _
"SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 7"")"
 
Upvote 0
FWIW, you could reduce the need for line continuations if you changed to a formula like this.
Code:
Sheets("Export").Range("B1").Cells(lrow + 1, 1).Formula = "=SUMIFS('Raw Data'!$T:$T,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,""Ward 1"")+" & _
"SUM(SUMIFS('Raw Data'!$J:$J,'Raw Data'!$D:$D,"">=""&" & WC & ",'Raw Data'!$D:$D,""<""&" & WC & "+7,'Raw Data'!$I:$I,{""Ward 2"",""Ward 3"",""Ward 4"",""Ward 5"",""Ward 6"",""Ward 7""}))"
 
Upvote 0
Hi Peter,

Thanks, I had considered using a similar formula to this, but occasionally I need to discount 1 or more of the wards from the sum, so I wanted each of them distinctly separate in my coding so I could 'deselect' them using a notation mark.

Thanks for the thought though, greatly appreciated :)
 
Upvote 0

Forum statistics

Threads
1,215,773
Messages
6,126,821
Members
449,340
Latest member
hpm23

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