+ Reply to Thread
Results 1 to 3 of 3

Thread: Help with variables in "Range"

  1. #1
    Registered User
    Join Date
    06-15-2011
    Location
    Tennessee, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Help with variables in "Range"

    Hi all! I have a question...
    I am writing a macro that will search from the bottom of a spreadsheet going up to find the last row. Then I want my selection to use "xlUp" again to get to my real data. Then I need to extend my selection to the right to select everything I need (which will eventually be copied to somewhere else in the workbook).

    However, I am having problems putting variables in the Range class...

    Sub Sample()
    
        Dim lastRow As Range, refCell As Range
    
        Sheets("Generic Name").Select
        Set lastRow = Range("B1048576").End(xlUp)
        Set refCell = lastRow.End(xlUp)
        Range(Cells(refCell), Range(Cells(refCell).End(xlToRight))).Select
    
    
    End Sub
    Can anyone help me select what I need out to the right of the refCell? Thanks in advance!!
    Last edited by cdustybk; 08-04-2011 at 10:30 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,958

    Re: Help with variables in "Range"

    Do you really need to select the data? When working with macros it's rarely a requirement.

    Are all of the rows going to have the same number of columns in them, or might it vary. If it's always the same then try:

    Sub Example()
    
    Const lWORKING_COLUMN=2
    
    Dim rngWorkingArea as Range
    
    With Sheets("Generic Sheet Name")
    
      Set rngWorkingArea=.Cells(.Rows.Count,lWORKING_COLUMN).End(xlUp)
      Set rngWorkingArea=rngTempRange.End(xlUp)
      Set rngWorkingArea=.Range(rngWorkingArea, rngWorkingArea.Offset(0,.Columns.Count-(lWORKING_COLUMN+1)).End(xlToLeft))
    
    End With
    
    End Sub
    At the end of that the working area will contain the last row, from column B across to the last used cell in the row and you can then do anything with that range that you'd normally do with a selection.

  3. #3
    Registered User
    Join Date
    06-15-2011
    Location
    Tennessee, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Help with variables in "Range"

    Thank you Andrew for your reply! And sorry it took all night for me to get back.

    I have changed my code like your example, and now it works to do what I need it to!

    Sub Sample()
    
        Dim lastRow As Range, refCell As Range
        
        Sheets("Generic Sheet").Select
        Set lastRow = Range("B1048576").End(xlUp)
        Set refCell = lastRow.End(xlUp)
        Set refCell = Range(refCell, refCell.Offset(0, Columns.Count - 3).End(xlToLeft))
        refCell.Select
    
    End Sub

    Do you really need to select the data? When working with macros it's rarely a requirement.
    No, I really didn't need to, but it's just my way of making sure I have the macro looking at the right stuff, if you will.
    I just started learning VBA about a week ago, so I probably do some things the long way because I don't exactly know all that's available to me yet. I'm ready to learn more though!

    Thanks again for the quick reply!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0