Got the following code which DOES WORK as an Excel VBScript Macro:
Sub replace()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Sheets(ws.Name).Activate
Dim I
For I = 5 To 20
ActiveSheet.Range("T" & I).Value = "=AVERAGE(3)"
Next I
Next ws
End Sub
Obviously =AVERAGE(3)
is not meant to be useful in any way, but to show that this code above works, and the following chunk does not:
Sub replace()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Sheets(ws.Name).Activate
Dim I
For I = 5 To 20
ActiveSheet.Range("T" & I).Value = "=IF(1=1;1;0)"
Next I
Next ws
End Sub
Actually there should be a way more complex formula where the IF
is (which looks something like: =IF(Q5<>0;IF(R5+S5>Q5;"ERROR";IF(R5="";S5/Q5;IF(Q5=R5;"Coord.";S5/(Q5-R5))));"N/A")
.
The thing is, I narrowed it down until I saw that even that trivial =IF(1=1;1;0)
statement fails with the 1004 runtime error.
I don't think it's of importance to solve the issue, but the whole macro is just for the purpose of updating about 15 cells on each worksheet for about 31 worksheets. As I said, the first chunk works (with the AVERAGE()
calculation) but the =IF
does not work.
For the sake of completeness, this was the code that I wanted to have:
Sub updCells()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Sheets(ws.Name).Activate
Dim I
For I = 5 To 20
ActiveSheet.Range("T" & I).Value = "=IF(Q" & I & "<>0,IF(R" & I & "+S" & I & ">Q" & I & ",""ERROR"",IF(R" & I & "="""",S" & I & "/Q" & I & ",IF(Q" & I & "=R" & I & ",""Coord."",S" & I & "/(Q" & I & "-R" & I & ")))),""N/A"")"
Next I
Next ws
End Sub