Selecting a table row in a table using VBA

bearcub

Well-known Member
Joined
May 18, 2005
Messages
731
Office Version
  1. 365
  2. 2013
  3. 2010
  4. 2007
Platform
  1. Windows
Hi,

If I have a table and I want to highlight the table row (ala conditional formatting a row in a table) -- how would I do that?

I original thought of using the current range property but that will select the entire table not active table row where I have my cursor.

My first thought is to do something like this:

Range(Activecell.end(xlRight), Activecell.end(xlLeft). select

However, I don't think this would really work in VBA and there is probably a more efficient and correct what of doing this.

What am I doing wrong?

Michael
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Assuming your Table is named "Table1", this line of code will select the row for the ActiveCell...

Code:
[table="width: 500"]
[tr]
	[td]Intersect(ActiveCell.EntireRow, Sheet1.ListObjects("Table1").DataBodyRange).Select[/td]
[/tr]
[/table]

I guess you should test that the active cell is, in fact, inside the table before you execute the above line...

Code:
[table="width: 500"]
[tr]
	[td]If Not Intersect(ActiveCell, ActiveSheet.ListObjects("Table1").DataBodyRange) Is Nothing Then
  Intersect(ActiveCell.EntireRow, Sheet1.ListObjects("Table1").DataBodyRange).Select
End If[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Hello Michael,

When it comes to selecting cells, there are multiple ways you can do it. If you have a table.. lets say the table spans columns D to H, you can select the row using the code below.
Code:
Sub SelectRow()

ActiveSheet.Range("D" & ActiveCell.Row, "H" & ActiveCell.Row).Select


End Sub

This if for tables of a fixed width. Is your table fixed? or do you need it to be dynamic?

Hope that helps,
Caleeco
 
Upvote 0
thank you for the quick response.

I would like it to be dynamic and what happens if I don't have a table but just a number of rows of data (a list that hasn't been converted to a table)?

Michael
 
Upvote 0
thank you for the quick response.

I would like it to be dynamic and what happens if I don't have a table but just a number of rows of data (a list that hasn't been converted to a table)?

Michael

Hello Michael,

In what context will the be used? Is it part of a larger macro?

When you say it needs to be dynamic... does the start column change??

Code:
Sub SelectRow()
ActiveSheet.Range(Selection.End(xlToLeft), Selection.End(xlToRight)).Select
End Sub

The above code will work in most circumstances (falls over if your active cell is at the start or end of the table and there are blank columns on that respective side)

Caleeco
 
Upvote 0
One ugly way....
Code:
Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column)).Select
 
Upvote 0
Assuming your "table" is set off from any other data that may be on the sheet, this should work...

Intersect(ActiveCell.EntireRow, ActiveCell.CurrentRegion).Select
 
Upvote 1
A bit less ugly than what I posted earlier... seems ok.

Code:
Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, Columns.Count).End(xlToLeft)).Select
 
Upvote 0
That was a good answer. I tried working this and could not solve if.

Interesting how the title to the question was:
"Selecting a table row in a table using VBA "

But then OP said they had no Table.
Assuming your Table is named "Table1", this line of code will select the row for the ActiveCell...

Code:
[table="width: 500"]
[tr]
	[td]Intersect(ActiveCell.EntireRow, Sheet1.ListObjects("Table1").DataBodyRange).Select[/td]
[/tr]
[/table]

I guess you should test that the active cell is, in fact, inside the table before you execute the above line...

Code:
[table="width: 500"]
[tr]
	[td]If Not Intersect(ActiveCell, ActiveSheet.ListObjects("Table1").DataBodyRange) Is Nothing Then
  Intersect(ActiveCell.EntireRow, Sheet1.ListObjects("Table1").DataBodyRange).Select
End If[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,222,615
Messages
6,167,065
Members
452,093
Latest member
JamesFromAustin

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