Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA Range reference error

Posted on 2013-06-28
3
Medium Priority
?
331 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

718 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