Wrox Programmer Forums
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 25th, 2006, 03:14 AM
Authorized User
Join Date: Feb 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.


Old January 25th, 2006, 03:46 AM
Authorized User
Join Date: Feb 2004
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

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

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

Old January 25th, 2006, 03:52 AM
Friend of Wrox
Join Date: Jan 2005
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...


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.



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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.