Runtime Error '91' Object variable or With block variable not set

treyabrown

New Member
Joined
May 23, 2014
Messages
7
Can someone help me fix my code. I am trying to loop through a column that has numbers beside each of the new files. For each i it should set the range as an offset 2 columns over and pull that file name, but when it loops through I get a runtime error '91'

Then at the bottom I am trying to loop through each of the new file names and move it over to the column "already uploaded" so they don't show as new the next time it is run.

Sub UploadNewData()
Dim FileName As Range
Dim FullFileName As String
ActiveWorkbook.Sheets("Comparison").Select
For i = 1 To Range("NewFiles")
FileName = Range("F:F").Find(What:=i, LookIn:=xlValues).Offset(0, -2)
FullFileName = Range("FilePath") & FileName
ChDir (Range("FilePath"))
Workbooks.Open FileName:=FullFileName
Cells.Select
Selection.Copy
Sheets("Raw Data").Select
Cells.Select
ActiveSheet.Paste
Next i
ActiveWorkbook.Sheets("Comparison").Select
For i = 1 To Range("NewFiles")
Set FileName = Range("F:F").Find(What:=i, LookIn:=xlValues).Offset(0, -2)
FileName.Copy
Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Next i
ActiveWorkbook.Save
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You need to use Set throughout for FileName.
Rich (BB code):
For i = 1 To Range("NewFiles")
    Set FileName = Range("F:F").Find(What:=i, LookIn:=xlValues).Offset(0, -2)
 
Upvote 0
Code:
For i = 1 To Range("NewFiles")

Range("NewFiles") the way you are using it should also be a number not a range.
What exactly is Range("NewFiles")?
Including what is the start cell of Range("NewFiles")?
 
Last edited:
Upvote 0
You need to use Set throughout for FileName.
Rich (BB code):
For i = 1 To Range("NewFiles")
    Set FileName = Range("F:F").Find(What:=i, LookIn:=xlValues).Offset(0, -2)

I tried using "Set FileName ="
but after the first loop I get Error '91'
 
Upvote 0
That probably means what you are trying to find isn't being found where you are looking for it.

When that happens Find returns Nothing and you can't offset from nothing.

What exactly, in words, are you trying to do with the code?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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