select range to the right and down

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
171
Good afternoon everyone. I have a macros which goes to a certain cell, and selects all the active cells to the right and below in the certain table.

Code:
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select

the goal is that it selects everything in that range and then colors the cells based on criteria.

Code:
For Each cl In Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column))       
         If cl >= Cells(cl.Row, "E") Then
           cl.Interior.Color = 5287936
        End If
        Next cl

its been working fine for the most part with my reports, however, in one of my reports, I noticed when the table has only one row of data, this function selects all the rows to the right in the table, but it selects all the rows to infinity below instead of just in the table

is there a way to fix it so that it selects only the cells in the table,

or is there a way for me to put an if else loop, where "if the table has one row of data, then only select to the right, else if greater than 1, select to the right and down"

thanks
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You could look from the bottom up so that it selects only one row when there is only one row, assuming there are no blanks that may though last row off.
Code:
Dim lr As Long
lr = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
Range(ActiveCell, Cells(lr, ActiveCell.End(xlToRight).Column)).Select

or


You could test if the cell under the active cell is blank
Code:
If ActiveCell.Offset(1, 0) <> "" Then
    Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
Else
    Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.End(xlToRight).Column)).Select
End If
 
Upvote 0
Try:
Code:
Sub test()
    If ActiveCell.CurrentRegion.Rows.Count > 1 Then
        Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
    Else
         Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.End(xlToRight).Column)).Select
    End If
End Sub
 
Upvote 0
Assuming your UsedRange starts at cell A1, here is another macro for you to consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub SelectDownAndToRight()
  On Error GoTo NotInUsedRange
  Intersect(ActiveSheet.UsedRange, Range(ActiveCell, Cells(Rows.Count, Columns.Count))).Select
NotInUsedRange:
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Try:
Code:
Sub test()
    If ActiveCell.[B][COLOR="#FF0000"]CurrentRegion[/COLOR][/B].Rows.Count > 1 Then
        Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
    Else
         Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.End(xlToRight).Column)).Select
    End If
End Sub
What about if there are blank rows (or columns) and the selected cell is above (or to the left of) of that blank row (or column)?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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