Solved

Excel VBA Range reference error

Posted on 2013-06-28
3
310 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

759 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

18 Experts available now in Live!

Get 1:1 Help Now