View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default VBA - UserForm Reset Option

Try this. I made some minor changes to the recorded section to insure it
would work. There are some odd-ball problems with recorded macros that cause
errors. then I made the changes to accept a variable filename. I had some
problems in the past when the filename and the Base name didn't match soem I
made sure they will always match. See changes below.

Sub Import()

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen = False Then
MsgBox "Cannot Open file - Exiting sub"
Exit Sub
End If

BaseName = fileToOpen
'remove path from filename
Do While InStr(BaseName, "\") 0
BaseName = Mid(BaseName, InStr(BaseName, "\") + 1)
Loop
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & fileToOpen, _
Destination:=Range("A1"))


.Name = BaseName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Totals"
End Sub


"megatron08" wrote:

It worked. So I'm assuming that I can proceed with using the same code going
forward.

The following code that I created using a recorded macro he

Sub Import()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;Y:\Financial Services\FS2008\Woodworkers\ER
Reports\052008\Test\Original Weyerhaeuser.TOC May 08.200806172100086.txt" _
, Destination:=Range("A1"))
.Name = "Original Weyerhaeuser.NelsonTrust.200806172055088"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Totals"
End Sub


Is there code that can prompt the user to select the location of the file
prior to doing the import of the .txt file? I'd like to use the same
subroutine if it is added.

TIA
~Christopher