PC Review


Reply
Thread Tools Rate Thread

Application Defined Error setting a formula via VBA

 
 
BerkshireGuy
Guest
Posts: n/a
 
      27th Oct 2006
The following code is giving me a Application Defined or Object Defined
error trying to set a formula for cell I for each row:

Dim wb As Workbook
Dim ws As Worksheet
Dim res As Variant

Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Data")
Set rng = Cells(Rows.Count, 1).End(xlUp)


For i = rng.Row To 2 Step -1

alias2 = Sheets("Data").Rows(i).Cells(3).Value

ws.Cells(i, "C").Value = Application.VLookup(alias2, _
Sheets("PolicyStatus").Range("A:B"), 2, False)
'The above works

'Below getting error

ws.Cells(i, "I").Formula =
"=IF(AND(RIGHT(H2,3)>='001',RIGHT(H2,3)<='275'),'FTA', " & _
"IF(AND(RIGHT(H2,3)>='276',RIGHT(H2,3)<='299'),'Special
Retired',IF(AND(RIGHT(H2,3)>='300',RIGHT(H2,3)<='899'),'BROKER', " & _
"IF(AND(RIGHT(H2,3)>='900',RIGHT(H2,3)<='999'),'FR','UNASSIGNED'))))"

TIA

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      27th Oct 2006
In excel formulas, strings are surrounded by double quotes.

If you were typing the formula in a cell, you'd see:
=IF(AND(RIGHT(H2,3)>="001", ....

But when you really want a double quote in VBA, you have to use two of them:

ws.Cells(i, "I").Formula = _
"=IF(AND(RIGHT(H2,3)>=""001"", ...

Then try it out.

When I'm debugging my long formulas, I like to do this:

ws.Cells(i, "I").Formula = _
"IF(AND(RIGHT(H2,3)>=""001"", ...

Then I can go back to that cell and add the leading equal sign and see how I
screwed it up. Then back to the VBE and make another attempt.


BerkshireGuy wrote:
>
> The following code is giving me a Application Defined or Object Defined
> error trying to set a formula for cell I for each row:
>
> Dim wb As Workbook
> Dim ws As Worksheet
> Dim res As Variant
>
> Set wb = ActiveWorkbook
> Set ws = wb.Worksheets("Data")
> Set rng = Cells(Rows.Count, 1).End(xlUp)
>
> For i = rng.Row To 2 Step -1
>
> alias2 = Sheets("Data").Rows(i).Cells(3).Value
>
> ws.Cells(i, "C").Value = Application.VLookup(alias2, _
> Sheets("PolicyStatus").Range("A:B"), 2, False)
> 'The above works
>
> 'Below getting error
>
> ws.Cells(i, "I").Formula =
> "=IF(AND(RIGHT(H2,3)>='001',RIGHT(H2,3)<='275'),'FTA', " & _
> "IF(AND(RIGHT(H2,3)>='276',RIGHT(H2,3)<='299'),'Special
> Retired',IF(AND(RIGHT(H2,3)>='300',RIGHT(H2,3)<='899'),'BROKER', " & _
> "IF(AND(RIGHT(H2,3)>='900',RIGHT(H2,3)<='999'),'FR','UNASSIGNED'))))"
>
> TIA


--

Dave Peterson
 
Reply With Quote
 
BerkshireGuy
Guest
Posts: n/a
 
      27th Oct 2006
Dave:

Perfect! Thanks for quick response!

-B

Dave Peterson wrote:
> In excel formulas, strings are surrounded by double quotes.
>
> If you were typing the formula in a cell, you'd see:
> =IF(AND(RIGHT(H2,3)>="001", ....
>
> But when you really want a double quote in VBA, you have to use two of them:
>
> ws.Cells(i, "I").Formula = _
> "=IF(AND(RIGHT(H2,3)>=""001"", ...
>
> Then try it out.
>
> When I'm debugging my long formulas, I like to do this:
>
> ws.Cells(i, "I").Formula = _
> "IF(AND(RIGHT(H2,3)>=""001"", ...
>
> Then I can go back to that cell and add the leading equal sign and see how I
> screwed it up. Then back to the VBE and make another attempt.
>
>
> BerkshireGuy wrote:
> >
> > The following code is giving me a Application Defined or Object Defined
> > error trying to set a formula for cell I for each row:
> >
> > Dim wb As Workbook
> > Dim ws As Worksheet
> > Dim res As Variant
> >
> > Set wb = ActiveWorkbook
> > Set ws = wb.Worksheets("Data")
> > Set rng = Cells(Rows.Count, 1).End(xlUp)
> >
> > For i = rng.Row To 2 Step -1
> >
> > alias2 = Sheets("Data").Rows(i).Cells(3).Value
> >
> > ws.Cells(i, "C").Value = Application.VLookup(alias2, _
> > Sheets("PolicyStatus").Range("A:B"), 2, False)
> > 'The above works
> >
> > 'Below getting error
> >
> > ws.Cells(i, "I").Formula =
> > "=IF(AND(RIGHT(H2,3)>='001',RIGHT(H2,3)<='275'),'FTA', " & _
> > "IF(AND(RIGHT(H2,3)>='276',RIGHT(H2,3)<='299'),'Special
> > Retired',IF(AND(RIGHT(H2,3)>='300',RIGHT(H2,3)<='899'),'BROKER', " & _
> > "IF(AND(RIGHT(H2,3)>='900',RIGHT(H2,3)<='999'),'FR','UNASSIGNED'))))"
> >
> > TIA

>
> --
>
> Dave Peterson


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Time Error 1004 Application-defined or object-defined error forsimple loop? Need help with what's wrong? Naji Microsoft Excel Programming 2 16th Oct 2009 05:45 PM
Re: How to set a formula in a cell? Application-defined error. John Wirt Microsoft Excel Programming 1 18th Feb 2005 02:11 PM
Re: How to set a formula in a cell? Application-defined error. Myrna Larson Microsoft Excel Programming 0 18th Feb 2005 04:00 AM
Re: How to set a formula in a cell? Application-defined error. Tom Ogilvy Microsoft Excel Programming 0 18th Feb 2005 03:34 AM
RE: How to set a formula in a cell? Application-defined error. =?Utf-8?B?RGF2aWQ=?= Microsoft Excel Programming 0 18th Feb 2005 03:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:47 AM.