一鍵TXT檔匯入至指定的Excel Sheet

請各位指點 以下是我之前錄製的VBA,按一次按鈕只能匯入一個TXT檔 ,該如何修改才能讓我按一次鍵匯入我所要的所有TXT檔至指定的SHEET中 謝謝

Private Sub CommandButton1_Click()
Dim strFilt As String
Dim strTitle As String
Dim strFname As Variant
Dim i As Integer
Dim strMsg As String

strFilt = "文字檔案,*.txt,"
strTitle = "打開Excel文件"
strFname = Application.GetOpenFilename(FileFilter:=strFilt, Title:=strTitle, MultiSelect:=True)
If Not IsArray(strFname) Then
MsgBox "沒選擇文件!"
Else
For i = LBound(strFname) To UBound(strFname)
strMsg = strMsg & strFname(i) & vbCrLf
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & strFname(i), Destination:=Range("$A$5"))
.Name = "C"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = "\"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveWindow.SmallScroll Down:=33
Cells.Select
Selection.RowHeight = 10
Selection.ColumnWidth = 5
Rows("19:50").Select
Selection.RowHeight = 0
Rows("76:134").Select
Selection.RowHeight = 0
ActiveWindow.SmallScroll Down:=-69
Columns("P:AS").Select
Selection.ColumnWidth = 0
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 15
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 1
Range("K5").Select
Selection.TextToColumns Destination:=Range("K5"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 2), Array(9, 2), Array(11, 2)), TrailingMinusNumbers:= _
True

Next
MsgBox "選擇的文件是:" & vbCrLf & strMsg
End If

End Sub

這是我目前想要修改的圖例 謝謝

一鍵TXT檔匯入至指定的Excel Sheet
2017-06-12 9:17 發佈
文章關鍵字 TXT檔 excel sheet
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?