MsgBox strFileContent
where can i put the txt file path? can you make an example?See here for how to do that: Read text file into string variable
Then you would just add:
to the bottom of the code to return that in a message box.VBA Code:MsgBox strFileContent
Just be sure to update the file path/name in the code to watch your text file.
Sub MyReturnString()
Dim strFilename As String: strFilename = "C:\temp\yourfile.txt"
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)
Close #iFile
MsgBox strFileContent
End Sub
I've tried it, but what appears is the data in the sheets, not showing the data in the txt file that I want.Did you look at that link? You pretty much copy that code, verbatim, dropping it into a Sub procedure.
The file path/name should be pretty evident (it is the only file path/name in the whole code).
The file name/path in red is the part you need to replace your your text file path/name.Rich (BB code):Sub MyReturnString() Dim strFilename As String: strFilename = "C:\temp\yourfile.txt" Dim strFileContent As String Dim iFile As Integer: iFile = FreeFile Open strFilename For Input As #iFile strFileContent = Input(LOF(iFile), iFile) Close #iFile MsgBox strFileContent End Sub
The line in blue is the part we added to the original code to return the value in the message box.
I don't see how that is possible if you are running this particular VBA code.I've tried it, but what appears is the data in the sheets, not showing the data in the txt file that I want.
I've made it in Modul1 but what appears is the data on the sheetI don't see how that is possible if you are running this particular VBA code.
Where exactly have you placed the VBA code?
You should place it in a General module that you create (see here: Insert and run VBA macros in Excel - step-by-step guide - Ablebits.com).