Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:54:40+00:00 2026-05-27T03:54:40+00:00

Using: Excel 2007/Win 7 First, I created a subroutine to find the dynamic range

  • 0

Using: Excel 2007/Win 7

First, I created a subroutine to find the dynamic range of a worksheet called ‘WIP’:

Sub GetWIPRange()
Dim WIPrng1 As Range
Dim WIPrng2 As Range
Sheets("WIP").Activate
Set WIPrng1 = Cells.find("*", [a1], , , xlByRows, xlPrevious)
Set WIPrng2 = Cells.find("*", [a1], , , xlByColumns, xlPrevious)
If Not WIPrng1 Is Nothing Then
    Set WIPrng3 = Range([a1], Cells(WIPrng1.Row, WIPrng2.Column))
Application.Goto WIPrng3
Else
    MsgBox "sheet is blank", vbCritical
End If
End Sub

Now I want to find a given contract number within the range defined above:

Sub find()
Dim find As Long
find = Application.WorksheetFunction.Match("545499", Range(WIPrng3.Parent.Name & "!" & WIPrng3.Address), 0)
MsgBox "Found at row : " & find
End Sub

But the error I get from the code above is:

Run-time error ’91’:
Object variable With block variable not set.

  1. How can I fix this code so that it returns the row number of the value I’m seeking?
  2. Is there a more efficient way of finding cell values using VBA? For example, if I have many worksheets and I want to search all worksheets and return a value’s specific row number and worksheet location.

Many thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:54:41+00:00Added an answer on May 27, 2026 at 3:54 am

    Where is WIPrng3 defined? Is it defined as Public? The problem is that WIPrng3 has gone out of scope by the time you run “find” and is therefore Nothing. You can check for Nothing in your “find” code and run the Get procedure if needed. Like this

    Sub find()
        Dim find As Long
    
        If WIPrng3 Is Nothing Then GetWIPRange
    
        find = Application.WorksheetFunction.Match("545499", Range(WIPrng3.Parent.Name & "!" & WIPrng3.Columns(1).Address), 0)
        MsgBox "Found at row : " & find
    End Sub
    

    Two things to note: If WIPrng3 returns a multicolumn range, MATCH will fail with a 1004 error. MATCH only works on a single column or row. In the example above, I restricted WIPrng3 to the first column in the MATCH function to avoid this. You didn’t have this in your code.

    Another thing is that you’re looking for the text string “545499”, not the number 545499. If your range contains the number and not the string, you’ll get an error. You can trap that error with an On Error statement and handle appropriately.

    Finally, I don’t see the advantage to defining WIPrng3 (but I can’t see the whole of what you’re doing). You could easily use

    Sub Find2()
    
        Dim lRow As Long
    
        On Error Resume Next
            lRow = Application.WorksheetFunction.Match("545499", Sheets("WIP").UsedRange.Columns(1), 0)
    
        If lRow > 0 Then
            'add where the used range starts in case it's not row 1
            MsgBox "Found at row : " & lRow + Sheets("WIP").UsedRange.Cells(1).Row - 1
        Else
            MsgBox "Not found"
        End If
    
    End Sub
    

    You might end up looking through a larger range, but it won’t appreciably affect performance.

    I added the On Error in this example so you could see how it works. Don’t put the On Error in there until you’ve tested it because it will mask all the other errors.

    Charles Williams did some nice analysis on finding efficiency here http://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First: I'm using Excel 2007, but the code has to work for Excel 2003
Using Windows XP and Excel 2007 I have a worksheet with a macro in
I've been experimenting with using MS Excel 2007 to edit tabular data stored in
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I'm developing an Excel 2007 add-in using Visual Studio Tools for Office (2008). I
I am currently building an Excel 2007 Add-in using VSTO (latest version + sp1)
I'm using a series of ToolStrips in an Excel 2007 Actions Pane. I'm programatically
What's the better way to insert cell comments in excel 2007 files programmatically using
I have written a few Excel 2003/2007 add-ins using VSTO, and I usually end
Using Excel 2007 VBA Have a sheet Dashboard, have other sheets but some special

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.