简体   繁体   中英

Error 1004: Application-defined or Object-defined Error - VBA

ActiveCell.FormulaR1C1 = "=sumifs('Adjusted Forecasts'!r1c[""&(adjcol-snopadj)&""]:r""&(lrow-1)&""c[""&(adjcol-snopadj)&""]|'Adjusted Forecasts'!r1c-7:r""&(lrow-1)&""c[-7]|r[1]c[-7])"

Hi,

The above line is throwing me an error 1004.There is no copying issue here, as this is an isolated line of code. The sheet 'Adjusted Forecasts' is another worksheet in the same workbook.

Any help is appreciated. Thanks.

There are two problems with the formula.

  1. the double quotes should only be single quotes. double quotes are used when a quote is needed in the formula. A single quote is used to differentiate between the formula test and vba code.

2 You have placed a "|" where a comma is required.

Try this:

ActiveCell.FormulaR1C1 = "=sumifs('Adjusted Forecasts'!r[1]c[" & (adjcol - snopadj) & "]:r[" & (lrow - 1) & "]c[" & (adjcol - snopadj) & "],'Adjusted Forecasts'!r[1]c[-7]:r" & (lrow - 1) & "c[-7],r[1]c[-7])"

It look like you're trying to use string concatenation with some variables, but you've escaped your quotation marks so it doesn't work.

Try:

ActiveCell.FormulaR1C1 = "=sumifs('Adjusted Forecasts'!r1c[" & (adjcol-snopadj) & _
    "]:r" & (lrow-1) & "c[" & (adjcol-snopadj) & "]|'Adjusted Forecasts'!r1c-7:r" & _
    (lrow-1) & "c[-7]|r[1]c[-7])"

(added line breaks for readability)


In VBA, a quotation mark ( " ) marks the start or end of a string. If you want to use a quotation mark within a string, then you have to escape it by doubling it up:

Debug.Print "Testing some ""quotes"" in this string"
'// Prints ~~ Testing some "quotes" in this string ~~

Because you have doubled up the quotes, the variable is read as a string instead of a variable, because the existing string hasn't been ended:

Dim a As Integer
a = 5

Debug.Print "a = " & a & " (test)"   '// Prints ~~ a = 5 test~~
Debug.Print "a = "" & a "" & (test)" '// Prints ~~ a = " & a " & (test) ~~

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