VBA: Select last cell in specific table column

KyleSC

New Member
Joined
Dec 30, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I'm having an issue making excel select the last cell in a particular column of a table. Here is the part that is causing trouble:

VBA Code:
     For Each lo In wksNew3.ListObjects
          Set rngAuditNoColumn = lo.ListColumns(6).DataBodyRange
                           
          'the next 2 lines remove any references to the original workbook
          rngAuditNoColumn.Copy
          rngAuditNoColumn.PasteSpecial (xlPasteValues)

          'select last cell in column 6 of table
'------------------------------>THE LINE BELOW IS THE PROBLEM! <-------------------------------------------
          Cells(lo.HeaderRowRange.Row + lo.ListRows.Count, 6).Activate
     Next lo

Current outcome:
The cells that are selected after the code runs is the range rngAuditNoColumn, which is NOT my goal. There is no error message. Do you know why the last cell of the table in the 6th column is not activated after executing this code? I feel like I'm missing something very simple.

Other info:
The name of the table won't always have the same name. The For loop is a workaround for that. The full code for the module is relatively long (for me, anyway). It involves copying worksheets, hiding and deleting table columns, and creating and saving new workbooks. Let me know if you want me to post the full module, but I'd rather avoid the hassle of removing any confidential information. Please let me know if there's any other information you require.

Thanks,

Kyle
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Is wksNew3 the active sheet when you run the code?
 
Upvote 0
This is not exactly what you want but may give you an example to look at.
VBA Code:
Sub Select_Last_Row_Table()
'Modified 5/14/2022  7:43:41 AM  EDT
Application.ScreenUpdating = False
Dim ans As Long
    With Sheets("Alpha").ListObjects(1).DataBodyRange
        ans = .Rows.Count
        .Cells(ans, 6).Activate
    End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I think @My Aswer Is This's syntax will work for you although I would add a couple of things.
In your code "Cells.(" is not given any context which means it defaults to wksNew3.Cells. This means that your Column 6 is column 6 of the sheet not column 6 of the Table (List Object)

Adapting My Anwer Is This's syntax to your specific scenario and making a couple of other changes it becomes this:

VBA Code:
          Application.CutCopyMode = False
          lo.DataBodyRange.Cells(lo.ListRows.Count, 6).Select

Turning cutcopymode off gets rid of the marching ants around your copy area.
Changing Activate to Select means only the Last Cell is selected. Otherwise Selection.Address and Activecell.Address will give different ranges and visually you can see the whole column still being selected
It seems unlikely that is what you want.
 
Upvote 0
Solution
Both codes will fail if wksNew3 is not the active sheet, which is why I asked the question. ;)
 
Upvote 0
If activating the last cell is just to get rid of the whole column being selected you can use
VBA Code:
     For Each lo In wksNew3.ListObjects
          Set rngAuditNoColumn = lo.ListColumns(6).DataBodyRange
                           
'          the next 2 lines remove any references to the original workbook
          rngAuditNoColumn.Value = rngAuditNoColumn.Value

          'select last cell in column 6 of table
'------------------------------>THE LINE BELOW IS THE PROBLEM! <-------------------------------------------
     Next lo
which will not select the cells.
 
Upvote 0
I think @My Aswer Is This's syntax will work for you although I would add a couple of things.
In your code "Cells.(" is not given any context which means it defaults to wksNew3.Cells. This means that your Column 6 is column 6 of the sheet not column 6 of the Table (List Object)

Adapting My Anwer Is This's syntax to your specific scenario and making a couple of other changes it becomes this:

VBA Code:
          Application.CutCopyMode = False
          lo.DataBodyRange.Cells(lo.ListRows.Count, 6).Select

Turning cutcopymode off gets rid of the marching ants around your copy area.
Changing Activate to Select means only the Last Cell is selected. Otherwise Selection.Address and Activecell.Address will give different ranges and visually you can see the whole column still being selected
It seems unlikely that is what you want.
This worked perfectly for what I needed. Sorry for the late response; I've been swamped lately. Thanks for the help!
 
Upvote 0
This worked perfectly for what I needed. Sorry for the late response; I've been swamped lately. Thanks for the help!
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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