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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Sorry I should have said that you don't need to use Select / Selection (in fact you should avoid it).
Rather than

VBA Code:
Columns("AO:AO").Select
    Selection.NumberFormat = "General"

just use

VBA Code:
Columns("AO:AO").NumberFormat = "General"
 
Upvote 0
Sorry I should have said that you don't need to use Select / Selection (in fact you should avoid it).
Rather than

VBA Code:
Columns("AO:AO").Select
    Selection.NumberFormat = "General"

just use

VBA Code:
Columns("AO:AO").NumberFormat = "General"
Thank you!! I always just record and go from there. I so appreciate this kind of feedback.
 
Upvote 0
Thank you!! I always just record and go from there. I so appreciate this kind of feedback.
Yeah, recording is very literal, and often needs a little clean-up.
Most any time you have one row ending in "Select" and the next starting with "Selection" or "ActiveCelll", you can combine those two rows.
Not only does it make your code shorter, but faster to (as selecting things slows down your code).

It will also record things like screen scrolling, which can be deleted, as it is not critical to the functioning of the code.
 
Upvote 0
Yeah, recording is very literal, and often needs a little clean-up.
Most any time you have one row ending in "Select" and the next starting with "Selection" or "ActiveCelll", you can combine those two rows.
Not only does it make your code shorter, but faster to (as selecting things slows down your code).

It will also record things like screen scrolling, which can be deleted, as it is not critical to the functioning of the code.
Recording the scrolling is soo frustrating! I'm very appreciative of all the tips I've gotten here today and over the years. I'll definitely be spending some time cleaning up some coding in the next few weeks!
 
Upvote 0
You are welcome! Glad we could help!

Yeah, the Macro Recording is VERY literal and record most everything!
 
Upvote 0
@rachel06 Selecting will also cause your code to crash if the activesheet isn't the sheet referenced in the code.
For example if you had Sheet2 active and you ran

VBA Code:
Sub XXX()
    Sheets("Sheet1").Range("A1:A2").Select
    Selection.Interior.ColorIndex = 6
End Sub

It would error, but the code below would color the cells on Sheet1

VBA Code:
Sub YYY()
  Sheets("Sheet1").Range("A1:A2").Interior.ColorIndex = 6
End Sub

so there are various reasons to try and avoid Selecting
 
Upvote 0
@rachel06 Selecting will also cause your code to crash if the activesheet isn't the sheet referenced in the code.
For example if you had Sheet2 active and you ran

VBA Code:
Sub XXX()
    Sheets("Sheet1").Range("A1:A2").Select
    Selection.Interior.ColorIndex = 6
End Sub

It would error, but the code below would color the cells on Sheet1

VBA Code:
Sub YYY()
  Sheets("Sheet1").Range("A1:A2").Interior.ColorIndex = 6
End Sub

so there are various reasons to try and avoid Selecting
Good to note! I tend to just have a sheet that has all of my buttons so I (or anyone else) is always running from the same sheet.
 
Upvote 0

Forum statistics

Threads
1,223,578
Messages
6,173,168
Members
452,504
Latest member
frankkeith2233

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