I recorded that example based on a text file which had 3 columns of
data in it (as stated), and the item which is in cell C1 (once
imported) is written to the variable cell_read and then the workbook
is closed. If you only have one item of data in your text file, then
change the macro to this:
Sub File_open()
'
' File_open Macro
' Macro recorded 14/07/2008 by Pete Ashurst
'
Dim file_name As String
file_name = Application.GetOpenFilename()
Workbooks.OpenText Filename:=file_name, _
Origin:=xlWindows, StartRow:=1, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 1))
cell_read = Cells(1, 1).Value
ActiveWorkbook.Close
'
' do what you want with cell_read ...
'
End Sub
Perhaps you can modify this to suit what you want to do.
Hope this helps.
Pete
- Show quoted text -
Thanks very mush for your response. However I have managed to solve
my problem thus:
Private Sub CommandButton1_Click()
Dim FileSaveName As Variant
Dim File_Name_String As String
Dim Text_Stream As Object
Dim fs As Object
Dim prompt As String
Dim Dummy_String As String
'Open dialog box and get file name to save from user
FileSaveName = Application.GetSaveAsFilename("")
'Check to see if user cancelled action
If FileSaveName <> False Then
'Convert the name of the file to a string
File_Name_String = CStr(FileSaveName)
'Create object for saving file
Set fs = CreateObject("Scripting.FileSystemObject")
'Ask user if they want to overwrite file
If fs.FileExists(File_Name_String) Then
Dummy_String = "File " & File_Name_String & " already exists.
Do you want to replace the existing file?"
prompt = MsgBox(Dummy_String, vbOKCancel)
If prompt = 2 Then Exit Sub
End If
'Associate text stream with file and allow to overwrite previous
data
Set Text_Stream = fs.CreateTextFile(File_Name_String, True)
'Output to file
Text_Stream.write "my test, not Hello World"
'Close file
Text_Stream.Close
End If
End Sub