error 91 object variable or with block variable not set

instanceoftime

Board Regular
Joined
Mar 23, 2011
Messages
102
I am looking at each cell in column D sheet1 and finding the matching number on sheet2 column A and copying B,C,D of sheet2 back to sheet1 A,B,C

the following works to a degree. the name is being put in E,F,G instead of A,B,C and after completing the first "round" (get's one person)I get an error 91 "Object variable or With block variable not set". (and is slow...) any better ideas or help?


Sub FillIn()
Dim c As Range, a As Range
For Each c In Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
With Sheets("Sheet2").Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
Set a = .Find(c.Value, LookIn:=xlValues)
End With
c.Offset(, 1).Resize(, 3).Value = a.Offset(, 1).Resize(, 3).Value
Next c
End Sub


thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
To fix the code check if a is Nothing, if it is the find has failed and you have nothing to copy.
Code:
Sub FillIn() 
Dim c As Range, a As Range 
 For Each c In Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row) 
     With Sheets("Sheet2").Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row) 
         Set a = .Find(c.Value, LookIn:=xlValues) 
     End With 
     If Not a Is Nothing Then
         c.Offset(, 1).Resize(, 3).Value = a.Offset(, 1).Resize(, 3).Value 
     End If
 Next c 
End Sub
If you want to speed things up then you should probably be looking at using formulas, perhaps VLOOKUP or INDEX/MATCH.
 
Upvote 0
I tried these an they work in the cells but I don't know how to incorporate it in a macro:

=INDEX(Sheet2!B2:B15, MATCH(Sheet1!D2, Sheet2!A2:A14,0))
=INDEX(Sheet2!C2:C15, MATCH(Sheet1!D2, Sheet2!A2:A14,0))
=INDEX(Sheet2!D2:D15, MATCH(Sheet1!D2, Sheet2!A2:A14,0))

also I need to add an "if visible" when going down sheet1?


To fix the code check if a is Nothing, if it is the find has failed and you have nothing to copy.
Code:
Sub FillIn() 
Dim c As Range, a As Range 
 For Each c In Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row) 
     With Sheets("Sheet2").Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row) 
         Set a = .Find(c.Value, LookIn:=xlValues) 
     End With 
     If Not a Is Nothing Then
         c.Offset(, 1).Resize(, 3).Value = a.Offset(, 1).Resize(, 3).Value 
     End If
 Next c 
End Sub
If you want to speed things up then you should probably be looking at using formulas, perhaps VLOOKUP or INDEX/MATCH.
 
Upvote 0
What do you mean an 'if visible'?

Are you applying a filter or hiding rows?
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,264
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top