Solved

Excel VBA Range reference error

Posted on 2013-06-28
3
313 Views
Last Modified: 2013-06-28
I'm having trouble setting a searchrange.  I need to search through the entire worksheet and I have a parameter for searchrange.  Any help would be great.


    'get range
    Dim LastCell As Range
    Dim SearchRange as Range
    Dim ColID As String
    Dim lastCol As String
    Dim lastRow As Long

    Set LastCell = sheet.Cells.Find(what:="*", after:=sheet.Cells(1, 1), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    ColID = LastCell.Address
    lastCol = Left(ColID, InStr(2, ColID, "$") - 1)
    lastCol = Replace(lastCol, "$", "")
    lastRow = CLng(LastCell.Row)

Set SearchRange = Range("A1:" & lastCol & ":" & lastRow)
0
Comment
Question by:gwarcher
3 Comments
 
LVL 10

Accepted Solution

by:
Anthony Berenguel earned 500 total points
ID: 39285819
try this

Set SearchRange = Range("A1:" & lastCol & lastRow)

Open in new window

0
 

Author Closing Comment

by:gwarcher
ID: 39285833
heh...oops
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39285834
Cleaner:

    'get range
    Dim LastCell As Range
    Dim SearchRange As Range

    Set LastCell = Sheet.Cells.Find(what:="*", after:=Sheet.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    If Not LastCell Is Nothing Then
        Set SearchRange = Sheet.Range("A1", LastCell)
    End If

Or, if you know there will always be a value on the sheet:

    'get range
    Dim LastCell As Range
    Dim SearchRange As Range

    Set LastCell = Sheet.Cells.Find(what:="*", after:=Sheet.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    Set SearchRange = Sheet.Range("A1", LastCell)

Kevin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel 2013 Power Query 1 46
Question to Pivot table 1 33
Troubleshooting a Worksheet Graphic 8 25
macro for same location path 2 19
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now