Page 1 of 1

การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Thu Aug 18, 2016 8:13 pm
by nayjatonay
สวัสดีครับ

ผมอยากจะประยุกต์ใช้ code vba สำหรับตรวจสอบข้อมูลในไฟล์ .txt ว่ามีข้อความว่า Thailand ที่ปรากฏอยู่ในตำแหน่งใดๆก็ได้ของไฟล์ .txt
โดยการกำหนดตำแหน่งไฟล์ที่จะไปเช็ค ผมจะเป็นคนพิมพ์เองในเซลล์ใดๆ เช่น เซลล์ A1 ระบุตำแหน่ง D:\test\sample.txt
อยากได้ code สำหรับแสดงผลในช่อง B1 ว่ามันมีข้อความว่า Thailand นี้อยู่ในไฟล์ .txt ตามตำแหน่งไฟล์ที่ใส่ไว้ในช่อง A1 ครับ
รบกวนขอคำแนะคำด้วยครับ

Re: การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Thu Aug 18, 2016 11:24 pm
by snasui
:D การใช้ VBA จำเป็นต้องเขียน Code มาเองตามกฎการใช้บอร์ดข้อ 5 ด้านบน :roll: ติดตรงไหนค่อยถามกันต่อครับ

Re: การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Mon Aug 22, 2016 5:54 pm
by nayjatonay
ครับผม Code ที่ผมลองเขียนตอนนี้ใช้ได้แล้วครับ แต่ผิดปัญหาคือมันทำงานเข้า Loop แสดงผล Pass แต่ไม่เข้าทำงานแสดงผล Fail ครับ
ผมต้องแก้ไขยังไงครับ รบกวนช่วยดูให้หน่อยครับ

Code: Select all

Sub Check_word()
'
' Check_word Macro
' Trairong B.
'
    Dim r As Integer
    r = 1
    Range("A1").Select
    Do Until IsEmpty(ActiveCell)
    
            strFileName = Range("a" & r)
            
            Const strSearch = "Thailand"
            Dim strLine As String
            Dim f As Integer
            Dim lngLine As Long
            Dim blnFound As Boolean
            f = FreeFile
            Open strFileName For Input As #f
                 Do While Not EOF(f)
                    lngLine = lngLine + 1
                    Line Input #f, strLine
                    If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
                        'MsgBox "Search string found in line " & lngLine, vbInformation
                        ActiveCell.Offset(0, 1).Select
                        ActiveCell.FormulaR1C1 = "Pass"
                        ActiveCell.Offset(0, -1).Select
                        blnFound = True
                        Exit Do
                    End If
                 Loop
            Close #f
            If Not blnFound Then
               ' MsgBox "Search string not found", vbInformation
                 ActiveCell.Offset(0, 1).Select
                 ActiveCell.FormulaR1C1 = "Fail"
                 ActiveCell.Offset(0, -1).Select
            End If
      ActiveCell.Offset(1, 0).Select
      r = r + 1
      
      Loop
'
End Sub

file ที่แนบไปต้องวางใน Drive C ก่อนน่ะครับ
ขอบคุณครับ

Re: การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Mon Aug 22, 2016 6:00 pm
by nayjatonay
แนบไฟล์ใหม่ครับ
ผมแนบไฟล์ .txt ลงไม่ได้ครับ
รบกวนทดลองสร้างไฟล์ . txt ตามนี้น่ะครับ

A.txt

Code: Select all

d
a
g
h
Thailand
1
5
6
B.txt

Code: Select all

d
a
gfweas
hfef
Thai1land
1
5
6
C.txt

Code: Select all

dfeaf
a
g
h
Thailand555
1
5
6
D.txt

Code: Select all

dw
adwad
g
h
aaaThailand
1
5
6dwadwa
E.txt

Code: Select all

d
adwad
g
h
Thaaailand
1
5dwa
6
ต้องวางไฟล์ txt ไว้ใน Folder ชื่อ Temp ใน Drive C น่ะครับ

ขอบคุณครับ

Re: การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Mon Aug 22, 2016 7:21 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub Check_word()
    '
    ' Check_word Macro
    ' Trairong B.
    '
    Dim r As Integer
    Dim strLine As String
    Dim f As Integer
    Dim lngLine As Long
    Dim blnFound As Boolean
    Const strSearch = "Thailand"
    r = 1
    Do Until IsEmpty(Range("a" & r))
            f = FreeFile
            blnFound = False
            strFileName = Range("a" & r)
            Open strFileName For Input As #f
            Do While Not EOF(f)
               lngLine = lngLine + 1
               Line Input #f, strLine
               If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
                   blnFound = True
                   Exit Do
               End If
            Loop
            Close #f
            If blnFound Then
                Range("a" & r).Offset(0, 1).Value = "Pass"
            Else
                Range("a" & r).Offset(0, 1).Value = "Fail"
            End If
            r = r + 1
      Loop
End Sub

Re: การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Mon Aug 22, 2016 9:00 pm
by nayjatonay
ขอบคุณมากๆครับ Code ใช้ได้ตามต้องการแล้วครับ มันดูง่ายและเป็นระเบียบขึ้นเยอะเลยครับ
ผมอยากถามต่อสักนิดครับ ถ้าผมต้องการดึงคำที่ต่อจาก คำว่า Thailand ออกมาวางใน Cell ต้องใช้คำสั่งอะไรครับ

สมมติ F.txt

Code: Select all

d
f
we
q dj Thailand Beautiful
fe
e
จาก .txt ดังกล่าวจะเห็นได้ว่าก่อนที่จะกด Enter ได้มีข้อความหลังคำว่า Thailand คือ Beautiful
ถ้าผมต้องการดึงคำว่า Beautiful นี้ออกมาต้องใช้คำสั่งในการเขียน Code อย่างไรครับ
ขอบคุณครับ

Re: การเช็คข้อความจากไฟล์.txt โดยระบุตำแหน่งที่อยู่ไฟล์เอง

Posted: Mon Aug 22, 2016 9:43 pm
by snasui
:D ที่ถามมานั้นต้องเขียนมาเองก่อน ติดแล้วค่อยถามกันต่อครับ