Select up to last used cell in ListColumn

eirikdaude

Board Regular
Joined
Nov 26, 2013
Messages
93
Office Version
  1. 365
Platform
  1. Windows
I have a ListObject looking like this, and want to select B4:B16 for ListColumn(1), C4:C13 for ListColumn(2) and D4:D13 for ListColumn(3).

1672840935551.png


Is there any quick way to do this?

I know I can use
VBA Code:
ActiveWorksheet.ListObjects(1).ListColumn(1).DataBodyRange
to select ListColumn(1), but what is the smartest way to find the last used cell in the two other columns?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
want to select B4:B16 for ListColumn(1), C4:C13 for ListColumn(2) and D4:D13 for ListColumn(3).

Is there any quick way to do this?

Maybe there is another way, but I show you an option:
VBA Code:
Sub selectdata_1()
  Dim f As Range
  Dim i As Long, lr1 As Long, lr2 As Long, lr3 As Long
   
  Set f = Range("B:B").Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
  lr1 = f.Row
  Set f = Range("C:C").Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
  lr2 = f.Row
  Set f = Range("D:D").Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
  lr3 = f.Row
 
  i = ActiveSheet.ListObjects(1).DataBodyRange.Cells(1, 1).Row
  ActiveSheet.Range("B" & i & ":B" & lr1 & ",C" & i & ":C" & lr2 & ",D" & i & ":D" & lr3).Select
End Sub


Or this:

VBA Code:
Sub Macro7()
    ActiveSheet.ListObjects(1).DataBodyRange.SpecialCells(xlCellTypeConstants, 23).Select
End Sub
 
Upvote 0
Why are you trying to select those ranges?
I am asking because generally in vba there is no need to actually select cells/ranges to work with them and selecting can slow your code considerably.
 
Upvote 0
You need to address Peter's question on why you want to select the range but see it this is any closer to what you were hoping for.

VBA Code:
Sub GetLastUsedRowInColumn()
    Dim ws As Worksheet
    Dim cellLastUsed As Range
    Dim rngCol2 As Range, rngCol3 As Range
    
    Set ws = ActiveSheet

    With ws.ListObjects(1)
        With .ListColumns(2)
            Set cellLastUsed = .Range.Find( _
                       What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
            Set rngCol2 = ws.Range(.DataBodyRange.Cells(1, 1), cellLastUsed)
        End With
        
        With .ListColumns(3)
            Set cellLastUsed = .Range.Find( _
                       What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
            Set rngCol3 = ws.Range(.DataBodyRange.Cells(1, 1), cellLastUsed)
        End With
        
        Debug.Print rngCol2.Address, rngCol3.Address
        ' Syntax to select
        rngCol2.Select
        rngCol3.Select

    End With

End Sub
 
Upvote 0
Why are you trying to select those ranges?
I am asking because generally in vba there is no need to actually select cells/ranges to work with them and selecting can slow your code considerably.
To know how many rows I need to allocate when copying the values from the ListObject to another range. It could be I chose my words poorly when I wrote that I wanted to select the range up to the last used cell, as I am not making an actual select-statement.

What I ended up using was something like this:



VBA Code:
    With Innstillingar.ListObjects(1)
        Set rngCol2 = .DataBodyRange.Cells(.DataBodyRange.Rows.Count, 2)
        If IsEmpty(rngCol2) Then
            Set rngCol2 = rngCol2.End(xlUp)
        End If
        Set rngCol2 = Range(.DataBodyRange.Cells(1, 2), rngCol2)
    End With
 
Upvote 0
What I ended up using was something like this:

The following does the same as your code:

VBA Code:
  With Innstillingar.ListObjects(1).ListColumns(2)
    Set rngCol2 = Range(.Range.Cells(2), Columns(.Range.Column).Find("*", , xlValues, 2, 1, 2))
  End With
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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