简体   繁体   中英

Excel Error:1004 - Trying to write cell formula

I'll try to make this as short as possible. I need to rewrite the formulas of a number of cells depending on what worksheet they're located in.

Dim sFrmla As String
Dim rSomerange As Range

Let sFrmla = "=OFFSET(INDIRECT(INDIRECT(" & Chr(34) & "E" & Chr(34) & "&ROW()));0;"

Let rSomerange.Formula = sFrmla & wsSheet.Name & "_ZERO_SCALE)"
Let rSomerange.Formula = sFrmla & wsSheet.Name & "_FULL_SCALE)"
Let rSomerange.Formula = sFrmla & wsSheet.Name & "_PRCSUNIT)"

This crashes at the second line (Zero_scale) and gives me a runtime error 1004. I have already made sure that the range itself exists and is writable.

Funny part is that the same code but without the formula string works just fine.

Let rSomerange.Formula = wsSheet.Name & "_ZERO_SCALE)"

Any ideas?

  1. there is no need to use Let keyword. It's odd in this case.
  2. no matter what is the default separator for your regional settings (comma , or semicolon ; ), you should always use comma , with Range.Formula . If you'd like to use your regional separator (semicolon ; in your case), use Range.FormulaLocal instead.

So, you should use:

sFrmla = "=OFFSET(INDIRECT(INDIRECT(" & Chr(34) & "E" & Chr(34) & "&ROW())),0,"
rSomerange.Formula = sFrmla & wsSheet.Name & "_ZERO_SCALE)"

or

sFrmla = "=OFFSET(INDIRECT(INDIRECT(" & Chr(34) & "E" & Chr(34) & "&ROW()));0;"
rSomerange.FormulaLocal = sFrmla & wsSheet.Name & "_ZERO_SCALE)"

or shorter version:

sFrmla = "=OFFSET(INDIRECT(INDIRECT(""E""&ROW())),0,"

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