Object Variable or With block variable not set error in VBA

jbuist

New Member
Joined
Mar 16, 2018
Messages
29
Hello. I am relatively new tocoding and have a problem. I have created a script that pulls data from multiple sheets into one sheet and adds the name of the original file (“Widget#”). I’m trying to copy and fill the empty cells with the file name as I will be creating a pivot table. I added the code beginning at Dim rng AsRange, cell As Range and I now get the error “Object variable of With block variable not set”. I’m not seeing where I have set an object. Code is below, current output and desired output.


Sub simpleXlsMerger()
Dim bookList As Workbook
Dim xfile(999) As String

Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObjAs Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
x = 1
Set dirObj = mergeObj.Getfolder("H:\Widgets")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)

Range("E1:F9").Copy
xfile(x) = ActiveWorkbook.Name
x = x + 1
ThisWorkbook.Worksheets(1).Activate

Range("B65536").End(xlUp).Offset(1, 0).PasteSpecialPaste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Dim SrchRng As Range, cel As Range
Set SrchRng = Range("B:B")
y = 1
For Each cel In SrchRng
If cel.Value ="Test" Then
cel.Offset(0, -1).Value =xfile(y)
y = y + 1
End If
Next cel
bookList.Saved = True
bookList.Close
Next
Columns("A:B").EntireColumn.AutoFit
Dim rngAs Range, cell As Range
rng = Range("A2").Select
For Each cell In rng
Selection.End(xlDown).Select
ActiveCell.Offset(-1,0).Select
rng(Selection,Selection.End(xlUp)).Select
Selection.FillDown
Nextcell
End Sub


CURRENT OUTPUT

Widget 1
Test
Count
Total Records

0
Untimely Notification

0
Untimely Dismissal

0
Widget 2
Test
Count
Total Records

12
Untimely Notification

0
Untimely Dismissal

0
Widget 3
Test
Count
Total Records

0
Untimely Notification

0
Untimely Dismissal

0
Widget 4
Test
Count
Total Records

0
Untimely Notification

0
Untimely Dismissal

0
Widget 5
Test
Count
Total Records

0
Untimely Notification

0
Untimely Dismissal

0
Widget 6
Test
Count
Total Records

0
Untimely Notification

0
Untimely Dismissal

0

<tbody>
</tbody>


DESIRED OUTPUT

Widget 1
Test
Count
Widget 1
Total Records

0
Widget 1
Untimely Notification

0
Widget 1
Untimely Dismissal

0
Widget 2
Test
Count
Widget 2
Total Records

12
Widget 2
Untimely Notification

0
Widget 2
Untimely Dismissal

0
Widget 3
Test
Count
Widget 3
Total Records

0
Widget 3
Untimely Notification

0
Widget 3
Untimely Dismissal

0
Widget 4
Test
Count
Widget 4
Total Records

0
Widget 4
Untimely Notification

0
Widget 4
Untimely Dismissal

0
Widget 5
Test
Count
Widget 5
Total Records

0
Widget 5
Untimely Notification

0
Widget 5
Untimely Dismissal

0
Widget 6
Test
Count
Widget 6
Total Records

0
Widget 6
Untimely Notification

0
Widget 6
Untimely Dismissal

0

<tbody>
</tbody>
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What about the first two questions I asked?
That might be able to pinpoint exactly what line it is choking on.
 
Upvote 0
When you get the error message do you not get a dialog with a Debug button?
 
Upvote 0
If you are not getting the error message with Debug button (like Norie mentioned), then I would recommend stepping through your code line-by-line (using the F8), and watching and seeing where things blow up.
If you know that certain parts are working fine (i.e. up to a certain point), you can add a breakpoint, and run it. It will stop at the breakpoint, and then you can use F8 to go through line-by-line from that point.
 
Upvote 0
It's hanging up on the code below. That's when the object variable error appears.

Code:
    rng = Range("A2:A60000").Select
    For Each cell In rng
        Selection.End(xlDown).Select
        ActiveCell.Offset(-1, 0).Select
        rng(Selection, Selection.End(xlUp)).Select
        Selection.FillDown
    Next cell


Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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