ขอความช่วยเหลือ CodeVB นำเข้า Text ไฟล์ ครับ
Posted: Tue Apr 26, 2022 9:35 pm
จากไฟล์ที่แนบมา สองไฟล์ คือ ไฟล์ชื่อ Ok.xlsb กับ ไฟล์ชื่อ error.xlsb
โครงสร้าง ไฟล์แบบเดียวกับ Code เดียวกัน
ไฟล์ Ok ทำงานได้ ส่วนไฟล์ error ทำงานไม่ได้
เกิดจากสาเหตุใดครับ (เริ่มต้นเกิดจากการ copy แยกไฟล์ ไปเป็นอีกไฟล์หนึ่ง)
โครงสร้าง ไฟล์แบบเดียวกับ Code เดียวกัน
ไฟล์ Ok ทำงานได้ ส่วนไฟล์ error ทำงานไม่ได้
เกิดจากสาเหตุใดครับ (เริ่มต้นเกิดจากการ copy แยกไฟล์ ไปเป็นอีกไฟล์หนึ่ง)
Code: Select all
Sub ImportAll()
Dim rTarget As Range
Dim i As Integer
Dim TextFileImport As Variant
On Error GoTo MsgError
TextFileImport = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _
"Select Text Data File", , True)
ActiveSheet.Unprotect Password:="1165"
For i = 1 To UBound(TextFileImport)
Set rTarget = Worksheets("DataMoneyUp").Range("C2000").End(xlUp).Offset(1, 0)
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TextFileImport(i), _
Destination:=rTarget)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 874
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next i
ActiveSheet.Protect Password:="1165"
Exit Sub
MsgError:
MsgBox "ยังไม่ได้เลือกไฟล์ที่จะนำเข้าข้อมูล"
Exit Sub
End Sub