VBA to set range name for later use in macro.

JonnyBT123

New Member
Joined
Jul 23, 2018
Messages
14
Morning all,
Racking my brains with this one as I’ve used this techniquemany of times in many different workbooks and macros.
Below is a snip of the code I’m using, which this part is tocopy the whole worksheet (named “WorkCompleted”) to values, and then highlightcolumns E:I to the last row of data there (which will be variable, as therecould be 6 rows, there could be 50 rows etc.) and name it for later use. Row 1is the header of the table, and everything else below is data.
Rich (BB code):
 With Worksheets("WorkCompleted")
Rich (BB code):
.Cells.Copy
.Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False

.Range("E1",Range("I100").End(xlUp)).Select
    Set rngWC =Selection

End With
It bugs out on the .Range(“E1”,Range(“I100”).End(xlUp)).Selectand goes to my cleanup error part, and I just don’t understand why. I’veliterally used this so many times in other macros.
Apologies if this isn’t the best, I’ve made this code fromscratch and left out the parts that work and I deem irrelevant to put in the post,but if required, I can paste the full macro.
Any help would be appreciated! I must be missing somethingso simple L

(PS: Not sure why the code is split out into two sections asabove…. Sorry!)
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If you are using a With statement your range needs to be .Range ie Range("I100") needs to be .Range("I100") however you cannot select a range that isnt on the activesheet lets not use the Select at all.

Code:
Set rngWC = .Range("E1", .Range("I100").End(xlUp))
 
Upvote 0
Knew it was something simple I'd missed! You sir, are a lifesaver. Thank you for your prompt response. Works a charm :)
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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