I am getting a bad file name error with this code:
- User
I am getting a bad file name error with this code:
- Assistant
I'm sorry, but there is no code provided for me to troubleshoot the error. Please provide the code and specify which line is causing the error.
- User
Sub OutstandingPO()
'Application.EnableEvents = False
'Declare variables Dim lastRow As Long, i As Long, j As Long Dim foundRows As Collection, cell As Range Dim txtFile As Integer, txtFilePath As String, txtContent As String
Worksheets("PORDER").Activate
'Find last row in column A lastRow = ActiveSheet.Cells(ActiveSheet.Rows.count, 1).End(xlUp).Row
'Create a collection to store found rows Set foundRows = New Collection
'Loop through column A from last row upwards For i = lastRow To 1 Step -1 If Not IsEmpty(ActiveSheet.Cells(i, 1)) Then 'If the cell has a formula value 'Loop through column G for empty cells in the same row For j = 1 To 6 'Columns A to F If IsEmpty(Cells(i, j + 6)) Then 'If the cell is empty 'Add the row to the collection foundRows.Add Range(Cells(i, 1), Cells(i, 6)).Value Exit For 'Exit the inner loop End If Next j End If Next i
'If no matches were found, exit the sub If foundRows.count = 0 Then MsgBox "No matches found.", vbInformation Exit Sub End If
'Open Notepad and create a new file txtFilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zzzz ServProvDocs\AwaitingPO.txt" txtFile = AwaitingPO Open txtFilePath For Output As txtFile
- Assistant
The error is most likely due to the file path in this line:
txtFilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zzzz ServProvDocs\AwaitingPO.txt"Make sure that the file path exists and is spelled correctly. Additionally, if the file already exists, ensure that it is not currently open in another program.
- User
the path is correct but I am still getting Bad File Name or Number
- Assistant
Here are a few suggestions to resolve the "Bad File Name or Number" error:
-
Permission issue: Check if you have write permissions to the folder where the file is being created. If you don't have write permissions, try changing the folder or updating the permissions.
-
Check if the file is open: If the file is already open in another program, you may not be able to open it again. Make sure that the file is not already open in Excel or any other program.
-
Incorrect file name: Make sure you have spelled the file name correctly, and it contains valid characters. Check if the file name includes any illegal characters such as ?, , /, :, *, ", >, <, |, etc.
-
Too long file name: Check if the file name is too long. Windows has a limit on the length of file paths. Try to reduce the path length.
-
Use FreeFile function: Try using the FreeFile function to return a free file number instead of the AwaitingPO constant. Change the following line:
txtFile = AwaitingPOto
txtFile = FreeFile()I hope one of these solutions help you resolve the error.
-
- User
When I run the code with your changes, it says that the file is already open. I have opened the text file and closed it down then run the code but it still gives me the error 'File is already open'
- Assistant
The error message "File is already open" suggests that the file is still open in another program or Excel. Here are a few things you can try:
-
Close the file manually: Make sure that the file is not open in another program, such as Notepad or Excel. If it is open, close it before running the code again.
-
Use the FreeFile Function: Instead of using a constant to represent the file number, use the FreeFile function to get the next available file number. Change the following line:
txtFile = AwaitingPOto
txtFile = FreeFile()- Use the Open statement with appropriate parameters: The correct syntax for the Open statement in VBA is:
Open fileName For mode [Access access] [lock] As #fileNoMake sure that you are using the correct parameters in this statement. You can try changing the Open statement to:
Open txtFilePath For Output Lock Write As #txtFileIf none of the above solutions work, you can try restarting your computer to see if it resolves the issue.
-