VBA Copy formula down data set

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
126
Office Version
  1. 365
Platform
  1. Windows
Hi,

Code:
Range("AD2:AD" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""""))))"

Trying to get this formula to copy down column AD for the entire dataset and getting an error "Method "Range' of object' _Global' failed.

Just curious what I'm doing wrong here :)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
How are you calculating "LastRow"?
What is its value when you get to this line of code?
 
Upvote 0
How are you calculating "LastRow"?
What is its value when you get to this line of code?
Oh shoot. Is this one of those things where I need to declare what "LastRow" is? I'm in this forum so much you'd think I'd know these things by now but I'm kind of a scatterbrain sometimes.

This is the entire code right now:

Code:
   Sheets("1537").Range("AO:AO").EntireColumn.Insert Shift:=xlToRight
    Sheets("1537").Range("AO1").Value = "Claim Type"
    Sheets("1537").Range("AO2:AO" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""Mail""))))"

I added the sheet name since last time thinking that might do it but no avail
 
Upvote 0
Not just declare, but you also need to sets its value!
Since it is a user defined variable, its value is nothing (or zero if declared to be a number) until you set it equal to something.

So you need some sort of calculation in there to determine the last row.

You can temporarily add this MsgBox to confirm you have a good value before using it.
Rich (BB code):
    MsgBox LastRow
    Sheets("1537").Range("AO2:AO" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""Mail""))))"
 
Upvote 0
Solution
Not just declare, but you also need to sets its value!
Since it is a user defined variable, its value is nothing (or zero if declared to be a number) until you set it equal to something.

So you need some sort of calculation in there to determine the last row.

You can temporarily add this MsgBox to confirm you have a good value before using it.
Rich (BB code):
    MsgBox LastRow
    Sheets("1537").Range("AO2:AO" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""Mail""))))"
Got it. This will at least get me down the right path. Thanks as always!
 
Upvote 0
You are welcome.

Yes, somewhere up higher in your code, you need to have your LastRow calculation.
It should start something like this:
VBA Code:
LastRow = ...
 
Upvote 0
You are welcome.

Yes, somewhere up higher in your code, you need to have your LastRow calculation.
It should start something like this:
VBA Code:
LastRow = ...
Okay, I got that with LastRow = Cells(Rows.Count, "AO").End(xlUp).Row, but now it's pasting the text of the formula and not the result of the formula lol.

Code:
Sub Macro4()
    LastRow = Cells(Rows.Count, "AO").End(xlUp).Row
    Sheets("1537").Range("AO:AO").EntireColumn.Insert Shift:=xlToRight
    Sheets("1537").Range("AO1").Value = "Claim Type"
    Sheets("1537").Range("AO2:AO" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""Mail""))))"
    
    End Sub
Do I have too many quotes?
 
Upvote 0
Try changing
VBA Code:
Sheets("1537").Range("AO2:AO" & LastRow).Formula
to
Rich (BB code):
Sheets("1537").Range("AO2:AO" & LastRow).FormulaR1C1

Also make sure your cells are formatted as General and not Text before you run the code
 
Upvote 0
Try changing
VBA Code:
Sheets("1537").Range("AO2:AO" & LastRow).Formula
to
Rich (BB code):
Sheets("1537").Range("AO2:AO" & LastRow).FormulaR1C1

Also make sure your cells are formatted as General and not Text before you run the code
How do I force it to general?

This didn't do it - it was flipped back to text when the macro finished :(

Code:
Sub Macro4()
    LastRow = Cells(Rows.Count, "AO").End(xlUp).Row
     Columns("AO:AO").Select
    Selection.NumberFormat = "General"
    Sheets("1537").Range("AO:AO").EntireColumn.Insert Shift:=xlToRight
    Sheets("1537").Range("AO1").Value = "Claim Type"
    Sheets("1537").Range("AO2:AO" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""Mail""))))"
    
    

End Sub
 
Upvote 0
How do I force it to general?

This didn't do it - it was flipped back to text when the macro finished :(

Code:
Sub Macro4()
    LastRow = Cells(Rows.Count, "AO").End(xlUp).Row
     Columns("AO:AO").Select
    Selection.NumberFormat = "General"
    Sheets("1537").Range("AO:AO").EntireColumn.Insert Shift:=xlToRight
    Sheets("1537").Range("AO1").Value = "Claim Type"
    Sheets("1537").Range("AO2:AO" & LastRow).Formula = _
        "=IF(RC[-1]=""Y"",""Specialty"",IF(RC[-2]=""Y"",""Retail 90"",IF(RC[-4]=""R"",""Retail"",IF(RC[-4]=""M"",""Mail"",""Mail""))))"
   
   

End Sub
oh wait, i put it in the wrong order. ha. all good now. Thanks so much all!
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top