Opening and converting *.txt with variable path

  • Thread starter Thread starter carloshernandezy
  • Start date Start date
C

carloshernandezy

Hello to all,


I´m triying to open a *.txt file in a variable path and with a
variable name.


C:\.....\year\weekXX\


After opening the select dialog box I select the *.txt file and convert

with comma delimiters.


-year- is the value of a cell in worksheet
-weekXX- is the value of a cell in worksheet


Thanks for your help
 
If the text file always has the same comma delimited fields, you could record a
macro when you import one of these files.

Do all the stuff you want during the import, but keep recording when you add
headers/page setup/filters/subtotals/whatever.

Then when you need to open the next file, just rerun the macro.

You could even ask the user (you???) for the name of the file to import:

Option Explicit
Sub testme()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub

You'll have to add all that .opentext stuff from your recorded macro.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 
Back
Top