I have some code that takes in a file path from a cell and reads the list of folders at the directory. I want it to do this in a loop for two cells (in my case B8 and B9). At the moment the code is looking at the first file path twice, rather than both paths once. The section of the code I believe is causing me the problem is here:
Dim objFSO As Object
Dim objFolder As Object
For k = 8 to 9
Set objFSO = CreateObject("Scripting.FileSystemObject")
MsgBox k
Set objFolder = objFSO.GetFolder(Range("B" & k).Value)
MsgBox objFolder
'do the bit of code that reads the files
Next
The first message box returns 8 & later 9 as expected, but MsgBox objFolder stays stuck on B8.value. I feel like I need to be clearing objFolder by setting it to null or similar but have tried a few variations of this with no success.
Update to provide more code in case I'm unwittingly doing something I shouldn't be:
The whole thing reads in the file paths, finds a particular text file at the path, unzips it and then imports the text files into two tabs.
Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim Directory(15) As String
Dim ZIPFile As Variant
Set objFSO = CreateObject("Scripting.FileSystemObject")
'moved to outside now
For k = 8 To 9
Set objFolder = objFSO.GetFolder(Range("B" & k).Value)
i = 0
For Each objFile In objFolder.Files
Directory(i) = objFile.Path
i = i + 1
Next objFile
For i = 0 To 14
If Right(Directory(i), 6) = "FQ.zip" Then ZIPFile = Directory(i)
Next
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String
DefPath = "Path name..."
'Create the folder name
strDate = Format(Now, " dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
MkDir FileNameFolder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(Fname).items
Sheets(1).Range("F" & k).Value = Replace(Right(ZIPFile, 25), ".zip", "") & "\EL-contract-rg.txt"
oApp.Namespace(FileNameFolder).CopyHere _
oApp.Namespace(ZIPFile).items.Item(Replace(Right(ZIPFile, 26), ".zip", "") & "\EL-contract-rg.txt")
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
Sheets(k - 6).Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & FileNameFolder & "EL-contract-rg.txt", Destination:=Range("$A$1") _
)
.Name = "Sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next
End Sub

Set objFSOshould not be done inside that loop.objFoldercan and should be set within the loop.objFSOshould not be.On Error Resume Nextand the folder in B9 doesn't exist.On Error Resume Nextand never reset error handling. UnderneathFor k = 8 To 9PutOn Error GoTo 0and let us know what happens.