Solved

Excel VBA Range reference error

Posted on 2013-06-28
3
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

751 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