Wrox Programmer Forums Inserting formula
 | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 Excel VBA Discuss using VBA for Excel programming.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
January 25th, 2006, 03:14 AM
 Authorized User Join Date: Feb 2004 Location: , , Denmark. Posts: 44 Thanks: 0 Thanked 0 Times in 0 Posts
Inserting formula

Hi all
I ran into a problem that I cannot solve myself.
I would like to enter a formula in some cells using VBA

Cells(2, 19).Formula = "=IF('2006-1'!I2<>"";'2006-1'!I2;"")"
Where 2006-1 is another sheet in the workbook.

If I just put some simple formula in there is no problem, but the above one gives me the following error.

Application-defined or object-defined error

Any help is appriciated.

Cheers
Karsten

January 25th, 2006, 03:46 AM
 Authorized User Join Date: Feb 2004 Location: , , Denmark. Posts: 44 Thanks: 0 Thanked 0 Times in 0 Posts

Hi again
I solved my problem.

Sub insertFormula()
Dim RowNo As Integer
Dim J As Integer

RowNo = 2
For J = 5 To 107 Step 2
Cells(J, 19).Formula = "=IF('2006-1'!I" & RowNo & ">0,'2006-1'!I" & RowNo & ","""")"
RowNo = RowNo + 1
Next J
End Sub

January 25th, 2006, 03:47 AM
 Friend of Wrox Join Date: Oct 2004 Location: Clinton, UT, USA. Posts: 564 Thanks: 0 Thanked 4 Times in 4 Posts

Sweet - I was halfway through typing up the very same solution!!

Mike
EchoVue.com
January 25th, 2006, 03:52 AM
 Friend of Wrox Join Date: Jan 2005 Location: Bournemouth, Dorset, United Kingdom. Posts: 180 Thanks: 0 Thanked 0 Times in 0 Posts

I was about to say, the formula need to have a quadruple " if it is to be recoginised as a string which is to be embedded into a formula...

"=IF('2006-1'!A2<>"""",'2006-1'!A2,"""")"

But then again looks like you figured it out anyhow. There is an alternative which I prefer...

Cells(J, 19).Formula = "=IF('2006-1'!I" & RowNo & ">0,'2006-1'!I" & RowNo & "," & Chr\$(34) & Chr\$(34) & ")"

That is, to use the ASCII character code set to specify the Chr\$(34) i.e. ", then concatenate this to the rest of the formula. But that is only my preference.

cheers

Matt

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Help with formula Juan0214 Access 4 August 1st, 2008 07:25 AM Help with formula 2 Juan0214 Access 3 July 10th, 2008 06:52 PM Formula scandalous Access VBA 11 February 27th, 2007 09:49 AM Inserting formula into active cell alisonchase Excel VBA 3 March 3rd, 2005 10:06 AM Formula Ned Pro VB 6 2 September 10th, 2003 10:26 AM

All times are GMT -4. The time now is 10:06 PM.