Run-time error 5 when using a "For" statement

gusbus

New Member
Joined
Mar 31, 2022
Messages
9
Office Version
  1. 2021
Platform
  1. Windows
Hi all,

I am creating a macro that will record the 32 digit ID of each file in a specified folder. When I try to record this into the current excel book I get a run-time error.

Appreciate the help.


VBA Code:
Sub MoveKs()

Dim folderName As String
Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim FSOFile As Object
'Workbook declarations
Dim wbBook1 As Workbook
Dim wsSheet1 As Worksheet
Dim wsSheet2 As Worksheet
'Subscription Attributes
Dim subID As String
Dim dtToday As Date


'Set the file name to a variable
folderName = "Filepathhere"




'   Set all the references to the FSO Library
    Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
    Set FSOFolder = FSOLibrary.GetFolder(folderName)
    Set FSOFile = FSOFolder.Files
'   Designate worksheets for file references and location
    Set wbBook1 = ThisWorkbook
    Set wsSheet1 = wbBook1.Worksheets("Macro")
    Set wsSheet2 = wbBook1.Worksheets("Records")
'   Set when the action is occuring
    dtToday = Now()



'Use For Each loop to loop through each file in the folder
For Each FSOFile In FSOFile
    subID = Left(FSOLibrary.GetFileName(FSOFile), 32)
    wsSheet2.Cells("A" & Rows.Count).End(xlUp).Offset(1).Value = subID
    wsSheet2.Cells("B" & Rows.Count).End(xlUp).Offset(1).Value = dtToday
Next


MsgBox Done!



End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You're going to make us look up what error 5 is out of thousands of error messages?
Try
wsSheet2.Range("A" & Rows.count).End(xlUp).Offset(1).Value = subID
wsSheet2.Range("B" & Rows.count).End(xlUp).Offset(1).Value = dtToday
 
Upvote 0
Solution
Forgot to mention that your message box line will raise a run time or compile error because of no quotes around your message string.
 
Upvote 0
You're going to make us look up what error 5 is out of thousands of error messages?
Try
wsSheet2.Range("A" & Rows.count).End(xlUp).Offset(1).Value = subID
wsSheet2.Range("B" & Rows.count).End(xlUp).Offset(1).Value = dtToday

This worked - thank you!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
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