Vba: defining a range with reference A1

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I need to define a range from A1 (top left of the range) to the last populated cell on the right, and to the last populate row downward.

https://imgur.com/a/keBiXeZ

In this case, the range has to be A1:D21.

Consider other cells in other areas could be populated.

Thank you.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
something like this?

Code:
Dim LastRowNo As Long
Dim LastColNo As Long


Sub FindLastColRow()
LastRowNo = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
LastColNo = Worksheets("Sheet1").Cells(2, Columns.Count).End(xlToLeft).Column
MsgBox "Last Row " & LastRowNo & " | Last Column " & LastColNo


End Sub
 
Upvote 0
Based on you image as assuming you want the highlighted area...
Code:
Sub CrEG()
Range("A1").CurrentRegion.Select
End Sub
 
Upvote 0
Based on you image as assuming you want the highlighted area...
Code:
Sub CrEG()
Range("A1").CurrentRegion.Select
End Sub

Something like this seems to work (without the use of "select").

Code:
Dim range1 As Range
Set range1 = sheet5.Range("A1").CurrentRegion
 
Upvote 0
You don't need Select, I only used it because you weren't clear on what you wanted to do with it :biggrin:

I could have used the below just as easily...

Code:
Sub CrEG()
MsgBox Range("A1").CurrentRegion.Address
End Sub
 
Last edited:
Upvote 0
Using current region you may get unexpected results. It is better if you have formula to find the edges as suggested previously.


From Mr Excel book, hehe

Sub FillIn()
On Error Resume Next
'Need this because if there aren't any blank 'cells, the code will error
Range("A1").CurrentRegion.SpecialCells(xlCellTypeBlanks).FormulaR1C1 _
= "empty"

Range("A1").CurrentRegion.Interior.Color = vbyellow
End Sub
 
Upvote 0
You shouldn't have unexpected results with currentregion as what makes up the currentregion is clearly defined in the help files. It is just learning what currentregion actually means
As stated I used currentregion based on the image supplied by the OP.
 
Upvote 0
Using current region you may get unexpected results. It is better if you have formula to find the edges as suggested previously.
Given the image & description supplied by the OP, if you look for the last used row/column you'll end up with a range A1:J33, which is not what the OP wanted.
Whereas CurrentRegion will give A1:D21 as per the OPs request
 
Upvote 0
if you have formula to find the edges as suggested previously.

@nikio8, just to elaborate (as I am now I am home from work) the only previous suggestion wasn't to use a formula. It was to use Rows.count xlup and Columns.Count xlleft which as per the image would work but...

it wouldn't cover for the OP's statement which was
Consider other cells in other areas could be populated.

so if you put anything if in row 2 from column F onwards or column A from row 23 onwards then it won't give the result the OP requested.

The idea of filling every blank in the current region with the word "empty" which is what
Code:
Range("A1").CurrentRegion.SpecialCells(xlCellTypeBlanks).FormulaR1C1 _ 
= "empty"
does, I would never do unless you done a replace afterwards and but even then it wouldn't adjust the currentregion as you are only filling the blanks in what already was the currentregion.

Hope that is clearer.
 
Upvote 0
Just a quick assumption that at least column A and row 1 will have something in each cell you could use

Code:
Sub RevFindLastRowCol()
LastRowNo = Worksheets("Sheet1").Range("A1").End(xlDown).Row
LastColNo = Worksheets("Sheet1").Range("A1").End(xlToRight).Column
MsgBox "Last Row " & LastRowNo & " | Last Column " & LastColNo


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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