+ Reply to Thread
Results 1 to 7 of 7

How can I select data to the end of a range in a macro?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-06-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    7

    How can I select data to the end of a range in a macro?

    I want my macro to always select all the data in a range. The problem is if I specify a range it will always select that exact range, not the data itself. I've tried converting it to a table but the same problem occurs, it will always do the same range of cells instead of the actual data.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,733

    Re: How can I select data to the end of a range in a macro?

    Option Explicit
    
    Sub sLastRow()
    
    Dim lLR As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    Range("A2:A" & lLR).Select
    
    ' or just
    
    Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Select
    
    ' or
    
    Range(Range("A2"), Range("A" & Rows.Count).End(xlUp)).Select
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    05-06-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How can I select data to the end of a range in a macro?

    Hi,
    Here is a good example, it looks like when it comes to the selection, maybe it is selecting to the end of the data, but when it does the command to format the selection as a table it explicitly does the range in the example file I used to setup the macro. I think what needs to be edited is the Range() parameter in the .Add section that creates Table1:

    Range("A1").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$120"), , xlYes).Name = _
            "Table1"

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,733

    Re: How can I select data to the end of a range in a macro?

    No, that's not a good example, it's a different example.

    If you have a Structured Table, Table1, and you want to select the data, you can use:

    Range("Table1").select
    But, in practical terms, you should avoid selecting anything. In the majority of cases, it isn't necessary.


    Regards, TMS

  5. #5
    Registered User
    Join Date
    05-06-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How can I select data to the end of a range in a macro?

    No, I'm not trying to select the table, here I am creating the table. I select to the end of the data and then click format as table, but the macro interprets that as converting a literal range. The next time I run this macro on a file it will convert A1 to F120 to a table instead of all the data. I think I need to replace Range("$A$1:$F$120") with something that would always capture the entire range.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,733

    Re: How can I select data to the end of a range in a macro?

    Sub sRngAddress()
    Dim lLR As Long, lLC As Long
    lLR = Cells(Rows.Count, 1).End(xlUp).Row
    lLC = Cells(1, Columns.Count).End(xlToLeft).Row
    With Sheets("Sheet1")
        .ListObjects.Add( _
                    xlSrcRange, _
                    .Range(.Cells(1, 1), .Cells(lLR, lLC)), , _
                    xlYes).Name = "Table1"
    End With
    End Sub

    BUT, you're not going to be able to run the macro more than once because the Table will already exist and you won't be able to overlap a table.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,733

    Re: How can I select data to the end of a range in a macro?

    Avoiding the problem ...

    Sub sRngAddress()
    Dim lLR As Long, lLC As Long
    lLR = Cells(Rows.Count, 1).End(xlUp).Row
    lLC = Cells(1, Columns.Count).End(xlToLeft).Row
    With Sheets("Sheet1")
        On Error Resume Next
        ' remove Table1 if it is has already been added
        .ListObjects("Table1").Unlist
        On Error GoTo 0
        .ListObjects.Add( _
                    xlSrcRange, _
                    .Range(.Cells(1, 1), .Cells(lLR, lLC)), , _
                    xlYes).Name = "Table1"
    End With
    End Sub

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to select a date range within data with dates
    By akynyemi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2013, 11:58 AM
  2. [SOLVED] macro to select data range in rows correctly..
    By arifmasum in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2012, 07:33 AM
  3. Macro to select data within a date range.
    By dannymcg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-12-2012, 05:44 AM
  4. Macro to select cells at end of range of data
    By johnlynches in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2012, 02:32 PM
  5. Replies: 14
    Last Post: 07-22-2005, 02:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1