can a string value be created reflecting the contents of a txt file in a single string?

spurs

Active Member
Joined
Oct 18, 2006
Messages
479
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
  5. 2003 or older
Platform
  1. Windows
I am trying to open a text file by the name of response.txt using VBA code.
I don't actually want the file to open or close on the screen; but I just want the vba code to assign the string variable A with the contents of the text file. The string is not more than 20 characters.

What coding would be needed to assign the contents of the text file response.txt to the variable A followed by the file response.txt file being deleted off the memory drive.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This code might work:


Code:
Sub parseTxt()
    Dim sTemp As String  'This will be your string from the text file
    Dim iFileNum As Integer
    Dim sFileName As String
    sFileName = "C:\Users\Surface5\Desktop\test\Response.txt" 'this should be the path to the text file
    
    iFileNum = FreeFile
    Open sFileName For Input As iFileNum
    


    
    
       sTemp = Input(LOF(iFileNum), [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFileNum]#iFileNum[/URL] )
     Close iFileNum
     'Debug.Print sTemp
     Kill "C:\Users\Surface5\Desktop\test\Response.txt" 'this again should be the path to the text file to delete it.
     
     'Debug.Print sTemp
End Sub
 
Upvote 0
This worked perfectly and it was so simple !

Thank you !
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,614
Members
452,661
Latest member
Nonhle

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