using variables with range(cells) reference

mhorstman

New Member
Joined
Feb 26, 2018
Messages
10
Hi,

I'm trying to build a range based off of variables that are calculated by user input. I have been able to get everything to work except for the range itself. I keep getting the "method of range of object _worksheet failed" error. The input is on one worksheet and the range is on a separate worksheet. Any help would be greatly appreciated.

Thanks,

Matt

Dim Sh2 As Worksheet
Dim Measure As String
Dim List As Range
Set Sh2 = ThisWorkbook.Worksheets("Sheet2")
Measure = Cells(2, 9).Value
Dim listaddress As String
Set List = Sh2.Range("A1:Z500").Find(Measure, lookat:=xlPart)
listaddress = List.Address
Dim listc As Integer
listc = List.Column
MsgBox "List Address = " & listaddress
MsgBox "List Column = " & listc


Dim r As Range
Dim lastr As Integer
lastr = Sh2.Cells(Sh2.Rows.Count, listc).End(xlUp).Row
MsgBox "Lastr = " & lastr
Set r = Range(Sh2.Cells(2, listc), Sh2.Cells(lastr, listc))
MsgBox "r = " & r
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Does this work?
Code:
With Sh2
    Set r = .Range(.Cells(2, listc), .Cells(lastr, listc))
End With
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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