[Giúp đỡ] Tìm kiếm, lọc và lấy dữ liệu từ nhiều file Word vào 1 file excel

Chia sẻ bởi:hands
★★★★★
Quảng cáo

em xin chân thành cảm ơn ạ.

Hiện tại em có nhiều file word có chứa dữ liệu cần lầy để thêm vào 1 file Excel.
Các dữ liệu muốn lấy nằm đúng vị trí thứ tự giống nhau trong các file word.
(Mẫu e đính kèm theo)
Kính nhờ các bác hướng dẫn em viết VBA hoặc làm cách nào để mình lấy dữ liệu qua excel được ạ, em xin chân thành cảm ơn ạ.

Bấm nút Chạy code -> duyệt tìm và chọn những file Word cần lấy dữ liệu -> bấm Open => Xong.

Sub GetDataWord()
    Dim WordApp As Object, myDoc As Object
    Dim i&, aTitle, aRes, MyPath, FullName

aTitle = Range("B1:L1").Value
    ReDim aRes(1 To UBound(aTitle, 2))
    Application.ScreenUpdating = False
    MyPath = Application.GetOpenFilename(Title:="Chon cac file Word can lay du lieu.", _
    FileFilter:="Excel Files *.doc* (*.doc*),", MultiSelect:=True)
    On Error GoTo WithArray
    If MyPath = False Then
        MsgBox "Ban chua chon file nào.", vbExclamation, "Sorry!"
        Exit Sub
    Else
WithArray:
        Set WordApp = CreateObject("Word.Application")
        For Each FullName In MyPath
            Set myDoc = WordApp.Documents.Open(FullName)
            WordApp.Visible = False
            With WordApp.Selection
                .HomeKey Unit:=6                              'wdStory
                For i = 1 To UBound(aTitle, 2)
                    If i = 1 Then
                        .Find.Text = Left(aTitle(1, i), 2)
                    Else
                        .Find.Text = aTitle(1, i)
                    End If
                    .Find.Execute: .MoveRight Unit:=1, Count:=2
                    If i = 1 Then
                        .MoveRight Unit:=2, Count:=6, Extend:=1
                    ElseIf i = UBound(aTitle, 2) Then
                        .MoveDown Unit:=4, Extend:=1
                        .Find.Text = "ngày": .Find.Execute
                        .MoveDown Unit:=4, Extend:=1
                    Else
                        .MoveDown Unit:=4, Extend:=1
                    End If
                    aRes(i) = Trim(.Range)
                    .MoveRight Unit:=1, Count:=1
                Next
            End With
            Range("B65536").End(xlUp).Offset(1).Resize(1, UBound(aRes)) = aRes
            myDoc.Close False
        Next FullName
    End If
    WordApp.Quit: Set myDoc = Nothing: Set WordApp = Nothing
    Application.ScreenUpdating = True
    MsgBox "Xong."
End Sub

www.giaiphapexcel.com/diendan/threads/gi%C3%BAp-%C4%91%E1%BB%A1-t%C3%ACm-ki%E1%BA%BFm-l%E1%BB%8Dc-v%C3%A0-l%E1%BA%A5y-d%E1%BB%AF-li%E1%BB%87u-t%E1%BB%AB-nhi%E1%BB%81u-file-word-v%C3%A0o-1-file-excel.166249/

Ứng dụng AI và Chat GPT trong Quản trị nhân sự
Khóa học SprinGO phù hợp

Ứng dụng AI và Chat GPT trong Quản trị nhân sự

Học xong khóa này, học viên có thể: Hiểu đúng bản chất AI, các nhóm AI phổ biến và cách AI “hoạt động” ở...

Xem khóa học
★★★★★ 5 ★ 1 👤 1 ▥ 0
Quảng cáo

Bạn nên đọc

One Response

  1. hands says:

    không giống mình tưởng tượng lắm.
    gửi bạn tham khảo code copy từ word sang excel, mình sưu tầm được

    Sub CopyWord()
        Dim objWord As Word.Application
    'Nho them VBAproject- Microsoft word ... Object Library
    
    Dim objDoc As Word.Document
        Dim RgDoc, RgStart, RgEnd, RgCopy As Word.Range
        Set objWord = New Word.Application
        With Application.FileDialog(msoFileDialogFilePicker)
            .Filters.Clear
            .Filters.Add "Word Documents", "*.*"
            If .Show = -1 Then
    Dim i As Long
    For i = 1 To .SelectedItems.Count
    
    Set objDoc = objWord.Documents.Open(.SelectedItems(i), ReadOnly:=True)
    '---Bat dau doan copy
        Set RgStart = objDoc.Content
            If Not RgStart.Find.Execute("noi dung tim 1") Then
                Resume Next
            End If
    
    '---ket thuc doan copy
        Set RgEnd = objDoc.Content
    
    Set RgEnd = objDoc.Content
        'RgEnd.Find.Execute ("noi dung tim 2")
            If Not RgEnd.Find.Execute(Sheets(wsName).Range("B2").Value) Then
                'Exit Sub
                Resume Next
            End If
    
    '---Vung van ban word can copy
        Set RgCopy = objDoc.Range(RgStart.Start, RgEnd.End)
    
    RgCopy.Select
        objWord.Selection.Copy
    
    'Vi tri paste trong excel
        Sheets(wsName).Range("A7").Select
        ActiveSheet.Paste
        objDoc.Close SaveChanges:=False
    
    Next i
    
    objWord.Quit
            Else
                Exit Sub
            End If
        End With
    
    End Sub

    Mình đã thử, code chạy khá tốt, thêm vòng lặp vào và xào chế thêm 1 xíu chắc sẽ đáp ứng được yêu cầu của thớt.

    E chạy báo lỗi chỗ :
    Sub CopyWord()
    Dim objWord As Word.Application
    mặc dù đã vào tool tick vào obj này r ạ.

    Bạn nhớ thêm VBAproject- Microsoft word … Object Library vào file excel chạy marco này nhé.

Leave a Reply

Your email address will not be published. Required fields are marked *

Quảng cáo

Cũ vẫn chất

Xem thêm