A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Ok. Thank you. I will try it again and see if anything changes. I appreciate your help.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hey!
I am writing this code for a class I am in to have excel open a file, read in the information in the file, and display the information in a message box. When I run the code, VBA states it cannot find my file. I know the file is correct and does exist.
Side note, the file is a plain text document but when I saved it from a word doc to .txt, it gives me an error telling me the format will change. Then VBA doesn't find it. Does anyone know how I can solve this? It doesn't happen to anyone else in the class.
Here is the code
Sub runclicked()
'Variables defined
Dim tFirstName As String
Dim tLastName As String
Dim tWrestlingName As String
Dim tCorner As String
Dim tAnnouncement As String
' open the file
Open ThisWorkbook.Path & "\wrestlingnames.txt" For Input As #1
'read the file
Input #1, tFirstName
Input #1, tLastName
Input #1, tWrestlingName
Input #1, tCorner
'close the file
Close #1
' Processing verbaige for text box
tFirstName = Trim(UCase(tFirstName))
tLastName = Trim(UCase(tLastName))
tWrestlingName = Trim(UCase(tWrestlingName))
tCorner = Trim(LCase(tCorner))
tAnnouncement = "In the " & tCorner & " corner, It's " & tFirstName & " ' " & tWrestlingName & " ' " & tLastName & " ! "
'Output
MsgBox tAnnouncement
End Sub
Any advice would be helpful at this point. Thanks!
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Ok. Thank you. I will try it again and see if anything changes. I appreciate your help.
I have tried using notepad ++ and I don't get this pop up, but it still states that it can not find my file.
The code works for me so I think that I have to go with the reply from Andreas that the text file is not in the same folder as your Excel project or you have the incorrect spelling for the filename. Because You have indicated that you have tested with Notepad, I should think that the file type should be correct because it only saves in text type files. However, when saving from word you could test by selecting "Windows (Default)" or "MS-Dos" option on that last popup dialog.
Thank you for your response!
Yes, I did save it in the save as field as plain text. When I do that, however, I get a window that pops up for file conversion and text formatting. It looks like this:
And then I get that error in excel. I have tried using notepad ++ and I don't get this pop up, but it still states that it can not find my file.
Your code works perfectly.
Screen shot of MsgBox.
Screen shot of Text file when opened with Notepad
When you created the text file using "Save as from Word, did you actually select the Plain Text (*.txt) option in the "Save as type field"? Or did you simply change the file extension in the "File name" field. The "Save as type field" must be selected.
When I run the code, VBA states it cannot find my file. I know the file is correct and does exist.
' open the file
Open ThisWorkbook.Path & "\wrestlingnames.txt" For Input As #1
When your Excel file is not saved and/or the file wrestlingnames.txt is not in the same folder you'll get this error.
Andreas.