Using sumif with a variable in a macro

guardian4119

New Member
Joined
May 1, 2014
Messages
25
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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Osvaldo Palmeiro

Well-known Member
Joined
Feb 24, 2009
Messages
635
Office Version
  1. 365
Platform
  1. Windows
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"
    Range("M5") = "=SUMIF(" & LR1.Address & ",""*oakland*""," & LR2.Address & ")"
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,129,754
Messages
5,638,170
Members
417,011
Latest member
Amaden95

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
Top