Simple range set question

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm having trouble setting a range. The range should be all A:B where data exists (so A1: last entry in B)

I've tried

VBA Code:
 Range("A1:B", Range("A1:B1").End(xlDown)

and a few variations of, but I keep getting "application-define or object-defined error.

What am I doing wrong? It must be something simple but I think I've gone blind to it at this point
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
One way:
VBA Code:
 Range("A1:B" & Cells(Rows.Count,"B").End(xlUp).Row)
Of course, that statement, by itself doesn't do anything. You either need to do something like setting it equal to a range value, or perform some action on it, like selecting it, etc.
 
Upvote 0
Try it like
VBA Code:
 Range("A1", Range("B" & rows.count).End(xlup))
 
Upvote 0
Try it like
VBA Code:
 Range("A1", Range("B" & rows.count).End(xlup))
Hi,

Went with this solution though I believe both provided work perfectly.

It stops working when I add
VBA Code:
Worksheets("sheet1").
to the front of it for some reason though. Not sure what's going on there or why.
 
Upvote 0
You need to to qualify both ranges like
VBA Code:
Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp))
or
VBA Code:
With Sheets("Sheet1")
   .Range("A1", .Range("B" & Rows.Count).End(xlUp))
End With
 
Upvote 0
You need to to qualify both ranges like
VBA Code:
Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp))
or
VBA Code:
With Sheets("Sheet1")
   .Range("A1", .Range("B" & Rows.Count).End(xlUp))
End With
Okay, so my code looks like this

VBA Code:
Dim InputRng As Range, ReplaceRng As Range



Set InputRng = Worksheets("Rota").Range("C1", Range("C1").End(xlToRight))

Set ReplaceRng = Worksheets("Data").Range("A1", Range("B" & Rows.Count).End(xlUp))

if I'm on Rota, the second set fails.

if I'm on Data, the first set fails.

I've inserted this as a module, not worksheet level - I don't remember having to switch to the correct sheet first just in order to set a range?
 
Upvote 0
That's because you haven't qualified the ranges as I showed.
 
Upvote 0
That's because you haven't qualified the ranges as I showed.
So it would look like the below?

VBA Code:
Set InputRng = With Worksheets("Rota")
                                  .Range("C1", Range("C1").End(xlToRight))
                                  End With
 
Upvote 0
Nope it should be
VBA Code:
With Worksheets("Rota")
   Set InputRng = .Range("C1", .Range("C1").End(xlToRight))
End With
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,049
Latest member
THMarana

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