ขออนุญาตสอบถามไม่สามารถนำเข้าไฟล์ dbf ได้ครับ
Posted: Sat Feb 12, 2022 1:33 pm
ผมพยายามแก้ปัญหาเรื่องการนำเข้าไฟล์ dbf เข้า worksheet ตามชื่อไฟล์ แต่ไม่สามารถนำเข้าได้ครับ หลังจากกดเลือกไฟล์นำเข้า ปรากฎว่าเป็นการสร้าง worksheet ใหม่ขึ้นมาแทน และไม่สามารถนำเข้าข้อมูลจากไฟล์ dbf ได้เช่นกัน รบกวนแนะนำวิธีแก้ไขปัญหาด้วยครับ
ขอบพระคุณครับ
ขอบพระคุณครับ
Code: Select all
Sub ImportdbfFiles()
Dim fso As Object
Dim xlsheet As Worksheet
Dim qt As QueryTable
Dim dbffilesToOpen As Variant, dbffile As Variant
Dim x As Long
On Error Resume Next
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
txtfilesToOpen = Application.GetOpenFilename _
(FileFilter:="Dbf Files (*.dbf), *.dbf", _
MultiSelect:=True, Title:="àÅ×Í¡ä¿Åì·Õèµéͧ¡ÒùÓà¢éÒ by...")
If VarType(dbffilesToOpen) = vbBoolean Then
MsgBox "¡ÃسÒàÅ×Í¡ä¿Åì·Õè¨Ð¹Óà¢éÒ", , "by..."
Exit Sub
End If
For Each dbffile In dbffilesToOpen
For Each xlsheet In ThisWorkbook.Worksheets
If LCase(xlsheet.Name) = LCase(fso.GetBaseName(dbffile)) Then
xlsheet.Activate
GoTo ImportData
End If
Next xlsheet
Set xlsheet = ThisWorkbook.Worksheets.Add( _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
xlsheet.Name = Replace(fso.GetFileName(dbffile), ".dbf", "")
xlsheet.Activate
GoTo ImportData
ImportData:
ActiveSheet.Range("A:Z").EntireColumn.Delete xlShiftToLeft
With ActiveSheet.QueryTables.Add(Connection:="DBF;" & dbffile, _
Destination:=ActiveSheet.Cells(3, 1))
.Name = "DataSheet"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
.Refresh BackgroundQuery:=False
End With
'For x = 1 To 100
'UpdateProgressBar x, 100
'Next x
ActiveSheet.Range("A3").AutoFilter Field:=1, Visibledropdown:=True
ActiveSheet.Range("A3").AutoFilter Field:=2, Visibledropdown:=True
Application.ErrorCheckingOptions.NumberAsText = False
Cells.Select
Selection.NumberFormat = "0"
For Each qt In ActiveSheet.QueryTables
qt.Delete
Next qt
Next dbffile
Application.ScreenUpdating = True
MsgBox "¹Óà¢éÒ¢éÍÁÙÅÊÓàÃç¨áÅéÇ", vbInformation, "ÊÓàÃç¨áÅéÇ"
Sheets(1).Activate
Set fso = Nothing
On Error Resume Next
For Each sht In ThisWorkbook.Worksheets
xlsheet.Cells.SpecialCells(xlCellTypeVisible).EntireColumn.AutoFit
'Application.ScreenUpdating = False
'With ActiveWindow
'.DisplayGridlines = True
'.DisplayHeadings = True
'End With
'Application.ScreenUpdating = True
Next sht
On Error GoTo 0
End Sub