Lấy dữ liệu từ txt file

Liên hệ QC

quocthan92

Thành viên mới
Tham gia
23/3/17
Bài viết
34
Được thích
0
Hello everyone,
Nhờ mọi người giúp đỡ, em muốn lấy số SerialNumber và ModelCode thì viết code như thế nào ạ?

Thanks!
 

File đính kèm

  • Pass.txt
    1.2 KB · Đọc: 16
Lấy thì được nhưng trong file có 3 cái SerialNumber và 2 cái ModelCode thì lấy cái nào đây.
 
Upvote 0
Thanks ban da rep. Mình muốn lấy từ dưới lên lấy cái tìm thấy đầu tiên
 
Upvote 0
Bạn sử dụng code này thử xem.
Mã:
Private Sub GPE()
Dim myFile As String, textSr As String, textMd As String, textline As String, posLat As Integer, posLong As Integer
myFile = "C:\Pass.txt"
textSr = ""
textMd = ""
Open myFile For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        If InStr(textline, "SerialNumber[") <> 0 Then textSr = Right(textline, Len(textline) - InStr(textline, "SerialNumber[") + 1)
        If InStr(textline, "ModelCode:") <> 0 Then textMd = Right(textline, Len(textline) - InStr(textline, "ModelCode:") + 1)
    Loop
Close #1
If textSr <> "" Then
    posLat = InStr(textSr, "SerialNumber[")
    Range("A1").Value = Mid(textSr, posLat + 13, InStr(textSr, "]") - (posLat + 13))
End If
If textMd <> "" Then
    posLong = InStr(textMd, "ModelCode:")
    Range("A2").Value = Mid(textMd, posLong + 10, InStr(textMd, ",") - (posLong + 10))
End If
End Sub
 
Upvote 0
Mình hiểu code, giờ mình muốn lấy 4 số serial number khác nhau của file đính kèm thì làm thế nào?
 

File đính kèm

  • Pass.txt
    5 KB · Đọc: 6
Upvote 0
Vậy sửa lại thế này.
Mã:
Sub GPE()
Dim myFile As String, textSr As String, textMd As String, textline As String, posLat As Integer, posLong As Integer
Dim k As Long, i As Long
myFile = "C:\Pass.txt"
textSr = "": textMd = ""
i = 0: kl = 0
Open myFile For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        If InStr(textline, "SerialNumber[") <> 0 Then
            textSr = Right(textline, Len(textline) - InStr(textline, "SerialNumber[") + 1)
            posLat = InStr(textSr, "SerialNumber[")
            k = k + 1
            Range("A" & k).Value = Mid(textSr, posLat + 13, InStr(textSr, "]") - (posLat + 13))
        End If
        If InStr(textline, "ModelCode:") <> 0 Then
            textMd = Right(textline, Len(textline) - InStr(textline, "ModelCode:") + 1)
            posLong = InStr(textMd, "ModelCode:")
            i = i + 1
            Range("B" & i).Value = Mid(textMd, posLong + 10, InStr(textMd, ",") - (posLong + 10))
        End If
    Loop
Close #1
End Sub
 
Upvote 0
Hi giaiphap, thanks ban rat nhiu vi da tan tinh giup do.
Mình mới viết thêm nhằm để lấy PBACode dòng cuối cùng của từng Serial Number. Kết quả chạy đúng. Nhờ bạn xem giúp vậy có bị lỗi trùng biến không mình có đính kèm file txt mới.

Sub GPE()
'khai bao ham

Dim myFile As String, textSr As String, textMd As String, textline As String, posLat As Integer, posLong As Integer
Dim k As Long, i As Long
myFile = "C:\Pass.txt"
textSr = ""
textMd = ""
textPBA = ""

k = 1
i = 1
h = 1
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline

'xu ly ham

If InStr(textline, "SerialNumber[") <> 0 Then

textSr = Right(textline, Len(textline) - InStr(textline, "SerialNumber[") + 1)
posLat = InStr(textSr, "SerialNumber[")
k = k + 1
Range("A" & k).Value = Mid(textSr, posLat + 13, InStr(textSr, "]") - (posLat + 13))

End If
If InStr(textline, "ModelCode:") <> 0 Then
textMd = Right(textline, Len(textline) - InStr(textline, "ModelCode:") + 1)
posLong = InStr(textMd, "ModelCode:")
i = i + 1
Range("B" & i).Value = Mid(textMd, posLong + 10, InStr(textMd, ",") - (posLong + 10))
End If

If InStr(textline, "PBACode") <> 0 Then

textPBA = Right(textline, 11)
'h = h + 1
Range("C" & k + 1).Value = textPBA
End If

Loop
Close #1
End Sub
 

File đính kèm

  • Pass.txt
    7.3 KB · Đọc: 6
Lần chỉnh sửa cuối:
Upvote 0
Bạn sử dụng biến h cho cột C đi. Nếu dùng biến k thì bị trùng với cột B
 
Upvote 0
Web KT
Back
Top Bottom