Page 1 of 1

ขออนุญาตสอบถามเรื่องการนำเข้า Text file กรณีนำเข้าไฟล์แล้วเกิดปัญหาชื่อ sheet ครับ

Posted: Wed Jun 30, 2021 9:59 am
by 9KiTTi
ขออนุญาตสอบถามเรื่องการนำเข้า Text file กรณีนำเข้าไฟล์แล้วเกิดปัญหาชื่อ sheet ที่เป็นภาษาอังกฤษตัวเล็กกับตัวใหญ่ครับ
กรณีที่ชื่อ sheet เป็นตัวเล็กและชื่อ text ไฟล์ที่นำเข้าเป็นตัวเล็ก สามารถนำเข้าได้ตรงกับ sheet ครับ แต่ถ้าชื่อไฟล์ที่นำเข้าเป็นตัวใหญ่จะนำเข้าไม่ตรงกับชื่อ sheet แต่เป็นการแทรก sheet ใหม่

Code: Select all

Sub ImportTXTFiles()
    Dim fso As Object
    Dim xlsheet As Worksheet
    Dim qt As QueryTable
    Dim txtfilesToOpen As Variant, txtfile As Variant
    On Error Resume Next
    
    Application.ScreenUpdating = False
    Set fso = CreateObject("Scripting.FileSystemObject")

    txtfilesToOpen = Application.GetOpenFilename _
                 (FileFilter:="Text Files (*.txt), *.txt", _
                  MultiSelect:=True, Title:="เลือกไฟล์ที่ต้องการนำเข้า")
    If VarType(txtfilesToOpen) = vbBoolean Then
        MsgBox "กรุณเลือกไฟล์ที่จะนำเข้า"
        Exit Sub
    End If

    For Each txtfile In txtfilesToOpen
               ' ทำหน้าที่หา sheet ที่ต้องการ
        For Each xlsheet In ThisWorkbook.Worksheets
            If xlsheet.Name = Replace(fso.GetFileName(txtfile), ".txt", "") Then
                xlsheet.Activate
                GoTo ImportData
            End If
        Next xlsheet

             ' สร้าง sheet ใหม่ ถ้าไม่พบ sheet ที่ต้องการ
        Set xlsheet = ThisWorkbook.Worksheets.Add( _
                             After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        xlsheet.Name = Replace(fso.GetFileName(txtfile), ".txt", "")
        xlsheet.Activate
        GoTo ImportData

ImportData:
           ' ลบข้อมูลเก่าทิ้ง
        ActiveSheet.Range("A:Z").EntireColumn.Delete xlShiftToLeft

        ' นำเข้าข้อมูลจาก text ไฟล์
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & txtfile, _
          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) ' จำนวน column ที่ต้องการให้มีตัวกรอง
        .Refresh BackgroundQuery:=False
        End With

    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 txtfile

    Application.ScreenUpdating = True
    MsgBox "นำเข้าข้อมูลสำเร็จแล้ว", vbInformation, "สำเร็จแล้ว"
    
 ' Sheets("F43Import").activate 'หลังจากนำเข้าเสร็จแล้วให้ไปทำงานที่ sheet แรก
    Sheets(1).Activate  ' ให้ทำงานที่ sheetแรกอัตโนมัติ

    Set fso = Nothing
    
     On Error Resume Next
    For Each sht In ThisWorkbook.Worksheets
      xlsheet.Cells.SpecialCells(xlCellTypeVisible).EntireColumn.AutoFit
    Next sht
  On Error GoTo 0
    
End Sub
 
รบกวนแนะนำวิธีแก้ไขด้วยครับ ขอบพระคุณครับ

Re: ขออนุญาตสอบถามเรื่องการนำเข้า Text file กรณีนำเข้าไฟล์แล้วเกิดปัญหาชื่อ sheet ครับ

Posted: Wed Jun 30, 2021 3:45 pm
by puriwutpokin
เบื้องต้นแก้แบบนี้ก่อนดูครับ

Code: Select all

  If xlsheet.Name = LCase(Replace(fso.GetFileName(txtfile), ".txt", "")) Then

Re: ขออนุญาตสอบถามเรื่องการนำเข้า Text file กรณีนำเข้าไฟล์แล้วเกิดปัญหาชื่อ sheet ครับ

Posted: Thu Jul 01, 2021 1:02 pm
by 9KiTTi
puriwutpokin wrote: Wed Jun 30, 2021 3:45 pm เบื้องต้นแก้แบบนี้ก่อนดูครับ

Code: Select all

  If xlsheet.Name = LCase(Replace(fso.GetFileName(txtfile), ".txt", "")) Then
ขอบพระคุณสำหรับคำแนะนำที่ดีครับ สามารถนำเข้าได้แล้วครับ แต่ยังติดปัญหานามสกุลของ text ไฟล์ ครับ ถ้านามสกุลเป็นตัวเล็ก เช่น 123.txt สามารถนำเข้าได้ แต่ถ้าเป็นนามสกุลตัวพิมพ์ใหญ่ เช่น 123.TXT จะนำเข้าไม่ตรง sheet ครับ จะไปสร้าง sheet ต่อท้าย รบกวนแนะนำวิธีแก้ปัญหาด้วยครับ

ขอบพระคุณครับ

Re: ขออนุญาตสอบถามเรื่องการนำเข้า Text file กรณีนำเข้าไฟล์แล้วเกิดปัญหาชื่อ sheet ครับ

Posted: Thu Jul 01, 2021 5:31 pm
by puriwutpokin
9KiTTi wrote: Thu Jul 01, 2021 1:02 pm
puriwutpokin wrote: Wed Jun 30, 2021 3:45 pm เบื้องต้นแก้แบบนี้ก่อนดูครับ

Code: Select all

  If xlsheet.Name = LCase(Replace(fso.GetFileName(txtfile), ".txt", "")) Then
ขอบพระคุณสำหรับคำแนะนำที่ดีครับ สามารถนำเข้าได้แล้วครับ แต่ยังติดปัญหานามสกุลของ text ไฟล์ ครับ ถ้านามสกุลเป็นตัวเล็ก เช่น 123.txt สามารถนำเข้าได้ แต่ถ้าเป็นนามสกุลตัวพิมพ์ใหญ่ เช่น 123.TXT จะนำเข้าไม่ตรง sheet ครับ จะไปสร้าง sheet ต่อท้าย รบกวนแนะนำวิธีแก้ปัญหาด้วยครับ

ขอบพระคุณครับ
ปรับตามนี้ครับ

Code: Select all

  If xlsheet.Name = Replace(LCase(fso.GetFileName(txtfile)), ".txt", "") Then

Re: ขออนุญาตสอบถามเรื่องการนำเข้า Text file กรณีนำเข้าไฟล์แล้วเกิดปัญหาชื่อ sheet ครับ

Posted: Thu Jul 01, 2021 8:02 pm
by 9KiTTi
puriwutpokin wrote: Thu Jul 01, 2021 5:31 pm
9KiTTi wrote: Thu Jul 01, 2021 1:02 pm
puriwutpokin wrote: Wed Jun 30, 2021 3:45 pm เบื้องต้นแก้แบบนี้ก่อนดูครับ

Code: Select all

  If xlsheet.Name = LCase(Replace(fso.GetFileName(txtfile), ".txt", "")) Then
ขอบพระคุณสำหรับคำแนะนำที่ดีครับ สามารถนำเข้าได้แล้วครับ แต่ยังติดปัญหานามสกุลของ text ไฟล์ ครับ ถ้านามสกุลเป็นตัวเล็ก เช่น 123.txt สามารถนำเข้าได้ แต่ถ้าเป็นนามสกุลตัวพิมพ์ใหญ่ เช่น 123.TXT จะนำเข้าไม่ตรง sheet ครับ จะไปสร้าง sheet ต่อท้าย รบกวนแนะนำวิธีแก้ปัญหาด้วยครับ

ขอบพระคุณครับ
ปรับตามนี้ครับ

Code: Select all

  If xlsheet.Name = Replace(LCase(fso.GetFileName(txtfile)), ".txt", "") Then

ขอบพระคุณมากครับ สามารถนำเข้าผ่านแล้วครับ เป็นกรณีศึกษาที่ผมจะได้นำไปศึกษาหาความรู้เพิ่มเติม ขอบพระคุณอีกครั้งครับ