Run-time error 5

DavidC78

New Member
Joined
Feb 4, 2021
Messages
3
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi,

I hope someone can help.

I am trying to create a command within a master document that will loop through a folder and extract values from specific cells and paste them to the master document. The folder has hundreds of individual files, all named differently, and each file has multiple worksheets but all worksheets are in the same format so cell references are uniform. I have managed to create something which I think should work but unfortunately I have been getting the Run-time error 5 at the point where the information is to be transferred:

VBA Code:
Sub LoopThroughLogs()

Dim wb As Workbook, ws As Worksheet
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("T:\ThinkTank\CENTRAL FILES\LOGS\RETURNED LOGS\")

y = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

For Each wbFile In fldr.Files

y = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

If fso.GetExtensionName(wbFile.Name) = "xls" Then
   
    Set wb = Workbooks.Open(wbFile.Path)
   
    For Each ws In wb.Sheets
       
    ThisWorkbook.Sheets("Sheet1").Cells(y, 2) = ws.Cells("F4")       '''HERE IS WHERE THE RUN-TIME ERROR INTERUPTS THE COMMAND
    ThisWorkbook.Sheets("Sheet1").Cells(y, 3) = ws.Cells("F6")
    ThisWorkbook.Sheets("Sheet1").Cells(y, 4) = ws.Cells("B11")
    ThisWorkbook.Sheets("Sheet1").Cells(y, 5) = ws.Cells("I11")
    y = y + 1

    Next ws
       
    wb.Close
   
End If

Next wbFile

End Sub


If anyone has any ideas that might help I'd be very grateful.

Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi & welcome to MrExcel.
Try
VBA Code:
For Each ws In wb.Worksheets
 
Upvote 0
This
VBA Code:
ws.Cells("F4")
should be
VBA Code:
ws.Range("F4").value
you will need to make that change to the other rows.
 
Upvote 0
Solution
This
VBA Code:
ws.Cells("F4")
should be
VBA Code:
ws.Range("F4").value
you will need to make that change to the other rows.

Absolutely spot on, that has worked a treat.

Thanks for the help Fluff, it's much appreciated :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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