简体   繁体   中英

VBA: Range Variable in a SUMIF Formula

I'm attempting to use a range variable in a SUMIF function. The issue I have been running into with other answers I've seen is the variable being passed into the formula as 'N2':'N132' and resulting in a #NAME? error. I'm hoping to achieve not only the range being properly passed through but also having it locked $N$2:$N$132. the code is as follows:

Thank you for your help in advance.

Dim sh1 As Worksheet
Dim x As range
Dim y As range



Set sh1 = ThisWorkbook.Sheets("Open Orders 180-365")
sh1.Activate

Set x = range("N2", range("N2").End(xlDown))


range("K:K").Find(What:="Open Order Detail 90-179 Days", MatchCase:=True).Select

Selection.Offset(1, 3).Select

Set y = range(Selection, Selection.End(xlDown))

range("D3").FormulaR1C1 = "=SUMIF(R3C13:R132C13,RC[-2]," & x.Address(False, False) & ")"


x.Address(False, False)

Returns the address in A1 style and you cannot mix A1 and R1C1 styles:

range("D3").FormulaR1C1 = "=SUMIF(R3C13:R132C13,RC[-2]," & x.Address(False, False,xlR1C1) & ")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM