2010年6月2日 星期三

Excel VBA 可讀取多筆txt資料

這是可以多筆txt匯入Execl的VBA程式
Try看看吧!!!

Sub inCSV()
x = InputBox("請輸入檔案數量")
Range("a1").Select
For i = 1 To x
If i = 1 Then
z = 1
Else
z = 1
End If
y = ActiveCell.Address
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\xx\" & i & ".txt", Destination:=Range(y))
.Name = "1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 950
.TextFileStartRow = z
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 5)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Application.CommandBars("External Data").Visible = False
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Next
End Sub
Private Sub CommandButton1_Click()
inCSV
End Sub

沒有留言:

張貼留言