vba lấy dữ liệu từ file .txt; .dat vào excel

Liên hệ QC

hoabattu3387

Thành viên chính thức
Tham gia
11/9/08
Bài viết
91
Được thích
2
Hi các anh/chị diễn đàn!
cho mình hỏi có cách nào lấy dữ liệu từ file .dat vào excel bằng vba ko?
 
Có cách bạn ạ!!!!!!!!!!!!!!!!!!!!!
 
Upvote 0
Mã:
Function GetTextFromFile(ByVal FileName As String, ByRef TextArray() As String) As Long
  Dim hFile     As Long
  Dim lngCount  As Long
  Dim bytFile() As Byte
  Dim strFile   As String
  
  Dim RegExp    As Object
  Dim Matchs    As Object
  
  On Error Resume Next
  lngCount = FileLen(FileName)
  If lngCount > 0 Then
    Set RegExp = CreateObject("VBScript.RegExp")
    If Not (RegExp Is Nothing) Then
      hFile = FreeFile
      ReDim bytFile(0 To lngCount - 1)
      Open FileName For Binary As hFile
      Get hFile, , bytFile()
      Close hFile
      strFile = StrConv(bytFile(), vbUnicode)
      Erase bytFile
      
      lngCount = 0
      With RegExp
        .Global = True
        .IgnoreCase = True
        .Pattern = "\b0{3}2\d+"
        Set Matchs = .Execute(strFile)
      End With
      
      If Matchs.Count < 1 Then
        GetTextFromFile = 0
      Else
        With Matchs
          ReDim TextArray(0 To .Count - 1)
          For lngCount = 0 To .Count - 1
            TextArray(lngCount) = "'" & Mid$(.Item(lngCount), 5)
          Next lngCount
        End With
        GetTextFromFile = lngCount
      End If
      Set Matchs = Nothing
      Set RegExp = Nothing
    Else
      GetTextFromFile = -2
    End If
  Else
    GetTextFromFile = -1
  End If
End Function

Sub Test()
  Dim TextArray() As String
  Dim FileName    As String
  Dim I As Long
  With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Text File", "*.TXT,*.DAT"
    .FilterIndex = 1
    If .Show = -1 Then
      FileName = .SelectedItems(1)
      I = GetTextFromFile(FileName, TextArray)
      If I > 0 Then
        Range("A1").Resize(I, 1).Value = Application.WorksheetFunction.Transpose(TextArray)
      End If
    End If
  End With
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Mã:
Function GetTextFromFile(ByVal FileName As String, ByRef TextArray() As String) As Long
  Dim hFile     As Long
  Dim lngCount  As Long
  Dim bytFile() As Byte
  Dim strFile   As String
  
  Dim RegExp    As Object
  Dim Matchs    As Object
  
  On Error Resume Next
  lngCount = FileLen(FileName)
  If lngCount > 0 Then
    Set RegExp = CreateObject("VBScript.RegExp")
    If Not (RegExp Is Nothing) Then
      hFile = FreeFile
      ReDim bytFile(0 To lngCount - 1)
      Open FileName For Binary As hFile
      Get hFile, , bytFile()
      Close hFile
      strFile = StrConv(bytFile(), vbUnicode)
      Erase bytFile
      
      lngCount = 0
      With RegExp
        .Global = True
        .IgnoreCase = True
        .Pattern = "\b0{3}2\d+"
        Set Matchs = .Execute(strFile)
      End With
      
      If Matchs.Count < 1 Then
        GetTextFromFile = 0
      Else
        With Matchs
          ReDim TextArray(0 To .Count - 1)
          For lngCount = 0 To .Count - 1
            TextArray(lngCount) = "'" & Mid$(.Item(lngCount), 5)
          Next lngCount
        End With
        GetTextFromFile = lngCount
      End If
      Set Matchs = Nothing
      Set RegExp = Nothing
    Else
      GetTextFromFile = -2
    End If
  Else
    GetTextFromFile = -1
  End If
End Function

Sub Test()
  Dim TextArray() As String
  Dim FileName    As String
  Dim I As Long
  With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Text File", "*.TXT,*.DAT"
    .FilterIndex = 1
    If .Show = -1 Then
      FileName = .SelectedItems(1)
      I = GetTextFromFile(FileName, TextArray)
      If I > 0 Then
        Range("A1").Resize(I, 1).Value = Application.WorksheetFunction.Transpose(TextArray)
      End If
    End If
  End With
End Sub

Code bạn này vẫn không lấy hết được nội dung trong file dat.
 
Upvote 0
Web KT
Back
Top Bottom