PDA

View Full Version : Solved: .formula not working with Excel



ukdane
09-27-2009, 11:53 PM
Hi folks,
This one is quite complicated, so I'll do my best to try to explain.
Unfortunately I can't post the code, as it is work specific.

We use a database program at work, in which I can program VBA.
I have written a script that will open an Excel (2003) workbook, find the last used row, and add the contents of a variable to any cell on the sheet, before saving and closing the Excel workbook.
All the code is written as VBA within the database program (Reflection)
This is done using the following code:
objWorksheet.Range("F" & WRITETOROW).Value = CUSTOMER
where the variables WRITETOROW and CUSTOMER have been defined earlier in the code.
This all works fine.

I then decided I wanted to take this a step further.
so instead of using .value, I wanted to use .formula.
objWorksheet.Range("L" & WRITETOROW).Formula = "=K10"
the above code works, and the contents of cell L (WRITETOROW) does indeed contain the contents of cell K10 (more precisely, it contains the formula =K10).
All fine and dandy.

So the next step was to try a more complicated formula. In this case an IF formula.
objWorksheet.Range("L" & WRITETOROW).Formula = "=IF(OFFSET(L" & WRITETOROW & ";0;-1)>"" "";""N"";"""")" 'formula

This doesn't work, If I debug, and step through the code, the formula is equal to ""
Why is this happening, and more importantly, how can I get this slightly more advanced .formula code to work in excel?

I hope I've explained this problem properly.
Thanks in advance.

mdmackillop
09-28-2009, 12:37 AM
Quote are always fiddly in such a case.
Enter your formula in the sheet as normal, then remove the = sign. Record a macro while re-inserting the =. This should give you the syntax.

Bob Phillips
09-28-2009, 01:09 AM
It looks fine to me, and writes the formula fine, when I change ; to ,.

But I would think this bit

>"" "";

should be

<>"""";

ukdane
09-28-2009, 10:35 AM
Grud dammit- language barrier- it was a comma, semicolon problem.

Thanks again xld.