vba syntax help

daw60

New Member
Joined
Jan 26, 2015
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
The following code gives me an error of Block Variable not set but I don't recognize the problem.
Any help would be appreciated


Sub my_range()

Dim my_range As Range
Dim Last_row As Long

Last_row = Sheets("DATA").Range("B" & Rows.Count).End(xlUp).Row
my_range = Sheets("DATA").Range("B5" & ":F" & Last_row)
Range(my_range).Select

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try...
VBA Code:
Dim my_range As Range
Dim Last_row As Long

Last_row = Sheets("DATA").Range("B" & Rows.Count).End(xlUp).Row
Set my_range = Sheets("DATA").Range("B5" & ":F" & Last_row)
my_range.Select
 
Upvote 0
Solution
Try...
VBA Code:
Dim my_range As Range
Dim Last_row As Long

Last_row = Sheets("DATA").Range("B" & Rows.Count).End(xlUp).Row
Set my_range = Sheets("DATA").Range("B5" & ":F" & Last_row)
my_range.Select
Thank you for the help Mark... I have a long way to go before I will get the syntax nuances..
 
Upvote 0
You're welcome

I have a long way to go before I will get the syntax nuances..
A Range is an Object so it needs Set in front of it, Text or numbers aren't objects (as in Last_row) and so don't need the Set word.

You are creating my_range as a Range variable and so because it is already a range you don't need to wrap it in Range()
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,651
Members
449,111
Latest member
ghennedy

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