# Using sumif with a variable in a macro

#### guardian4119

##### New Member
Hi

I am working on the code below. I recorded a macro and got my equations (like the last line sumif equation) in R1C1 but i want them in A1. I tried rewriting the commented out line to include some of the variables, but i keep getting an error. What is wrong with the formula. Thanks

Code:
``````Sub macro4()
'
'
'

LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row - 1
Set LR1 = Range("A4:A" & LR)
Set LR2 = Range("D4:D" & LR)

Range("L5").Select
ActiveCell.FormulaR1C1 = "Oakland"

Range("L7").Select
ActiveCell.FormulaR1C1 = "Concord"

Range("M5").Select
'ActiveCell.Formula = "=SUMIF(" & LR1 & "," * oakland * "," & LR2 & ")"

Range("M7").Select
ActiveCell.FormulaR1C1 = _
"=SUMIF(R4C[-12]:R11626C[-12],""*concord*"",R4C[-9]:R11626C[-9])"

End Sub``````

### Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi. See if this could help.
Code:
``````Sub macro4V2()
Dim LR As Long, LR1 As Range, LR2 As Range
LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row ' - 1 (?)
Set LR1 = Range("A4:A" & LR)
Set LR2 = Range("D4:D" & LR)
Range("L5") = "Oakland"
Range("L7") = "Concord"
End Sub``````

Awesome! Thanks Osvaldo, it's working great!

Replies
13
Views
700
Replies
4
Views
304
Replies
5
Views
189
Replies
2
Views
406
Replies
2
Views
1K

1,216,499
Messages
6,130,999
Members
449,612
Latest member
geniusufo007

### 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.

### Which adblocker are you using?

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

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