If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Formula does not work when used in VBA code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-11, 12:55
mike703 mike703 is offline
Registered User
 
Join Date: Mar 2005
Posts: 23
Formula does not work when used in VBA code

OK...so i'm a little perplexed at this point.

Trying to replicate a worksheet using VBA code to build and I have run into a problem. The following line causes an "application-defined or object-defined error":

Range("AA3").Formula = "=IF(X3 ="",IF(W3="",N3+V3,N3+W3), N3+X3)"

What is really bothering me is the fact that the formula is the exact same formula in the cell of the sheet that I am trying to replicate. If I copy and pasted this formula into a cell it would work as intented, but in the line above, it generates an error. Is there something that i'm missing here?
Reply With Quote
  #2 (permalink)  
Old 03-31-11, 13:45
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi Mike,

In VBA, the " character denotes a string so, to include a " in a string, you have to do it like this:
Code:
Range("AA3").Formula = "=IF(X3 ="""",IF(W3="""",N3+V3,N3+W3), N3+X3)"
(or you can use Chr 34 to represent them)
Code:
Range("A3").Formula = "=IF(X3 =" & Chr$(34) & Chr$(34) & ",IF(W3=" & Chr$(34) & Chr$(34) & ",N3+V3,N3+W3), N3+X3)"
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #3 (permalink)  
Old 03-31-11, 13:53
mike703 mike703 is offline
Registered User
 
Join Date: Mar 2005
Posts: 23
That did it! Thanks!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On