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

  • SEARCH
  • Home
  • 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 8097727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:51:11+00:00 2026-06-05T21:51:11+00:00

Learning some VBA. So far, I’ve constructed this piece of code which should allow

  • 0

Learning some VBA. So far, I’ve constructed this piece of code which should allow me (though it does not, yet) to do the following thing:

  1. Get the number in the "M" & i cell (in the first iteration it is M5).
  2. Find that number in the A column.
  3. Once it finds it, set the value of PutHereIfFound the same as the value of F6 (hence the offset).
  4. If a number is found, then increment i in order for the loop to continue searching for M6, M7, … all up to cell M20.

It’s returning a Run-Time Error 91, which stands for Object Variable or With Variable not set. When I debug, it points to the Set PuthereIfFound line.

What is the reason for this mistake?

 Sub FindThis()
    Dim FindThis As Range
    Dim PutHereIfFound As Range
    Dim i As Integer
    Dim f As Integer

    i = 5
    f = 5
    Do
        Set FindThis = ActiveSheet.Range("M" & i)
        Set PutHereIfFound = ActiveSheet.Range("N" & i)
            With ActiveSheet.Range("A:A")
                Set PutHereIfFound = .Find(What:=FindThis, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False).Offset(0, 5)

                If Not PutHereIfFound Is Nothing Then
                    i = i + 1
                Else
                    i = i                       
                End If                                    
            End With
     Loop While i <= 20
End Sub
  • 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-06-05T21:51:12+00:00Added an answer on June 5, 2026 at 9:51 pm

    Further to my comment, your code can be optimized like this.

     Sub FindThis()
        Dim ws As Worksheet
        Dim FindThis As String
        Dim aCell As Range
        Dim i As Long
    
        Set ws = Sheets("Sheet1")
    
        With ws
            For i = 5 To 20
                FindThis = .Range("M" & i).Value
    
                Set aCell = .Columns(1).Find(What:=FindThis, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    
                If Not aCell Is Nothing Then
                    '~~> Do whatever you want here with the F Value
                    PutHereIfFound = aCell.Offset(, 5).Value
    
                    Debug.Print PutHereIfFound
                End If
            Next i
        End With
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning some COM code and the following code puzzled me. STDMETHODIMP _(ULONG)
I have been learning python for some time now. While starting this learning python
I was learning some Rspec stuff and accidentally introduced some code into my model
I am learning some WPF/C# so this question might be silly for some of
I'm learning some CSS to tweak my project template. I come to this problem
I've been learning some Clojure, and I currently have a single .clj file which
I just started learning some ruby, and I want to do something like this:
I'm working on learning some Haskell (please excuse the newbie error)- This routine errors
I'm learning some DirectX programming by re-implementing some DirectX code into different projects, I
I'm still learning some of this c# stuff, and I couldn't find an answer

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.