Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Why am I getting error 91 with "Sheets(ws).Range("g2") = foundCell.Address"

0

this is the code I wrote. The comments below end sub is code that works for what I am trying to do. I get an "Object variable or with block not set" error in line 

 Sheets(ws).Range("g2") = foundCell.Address [CODE]. 
Here's everything
[CODE] Sub FindCellAddress()     Dim rng As Range     Dim foundCell As Range     Dim searchValue As Text     Dim ws As Worksheet    Set ws = Sheets("input")    For x = 4 To 13  searchValue = Sheets(ws).Cells(x, 7)  MsgBox searchValue     Set rng = Worksheets("Sheet1").Range("i1:k20")    Set foundCell = rng.Find(What:=searchValue, LookIn:=xlValues)    Sheets(ws).Range("g2") = foundCell.Address MsgBox foundCell.Address Next x End Sub  '---------------------this works for finding address--------------    ' searchValue = "cinnamon"    ' Set rng = Worksheets("Sheet1").Range("i1:k20")    ' Set foundCell = rng.Find(What:=searchValue, LookIn:=xlValues)    ' Range("g2") = foundCell.Address     '---------------------------------

It looks to me like my variable is defined

Answer
Discuss

Answers

0
Selected Answer

Hi Baddog1016.

Although you declared the variable ws, your line:

Set ws = Sheets("input")

makes ws a worksheet. 

Your line causing Error 91 should therefore read:

   ws.Range("g2") = foundCell.Address

Hope this fixes your problem (I haven't looked at the rest of your code). If so, please remember to mark this Answer as Selected. 

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login