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...
Can anyone help me select what I need out to the right of the refCell? Thanks in advance!!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
Last edited by cdustybk; 08-04-2011 at 10:30 AM.
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:
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.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
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
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.Do you really need to select the data? When working with macros it's rarely a requirement.
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!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks