Bug: gộp dữ liệu từ nhiều file riêng rẽ (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

vodinhnam

Thành viên mới
Tham gia
7/8/11
Bài viết
29
Được thích
1
Chào các anh, chị,
Em có đoạn code gộp dữ liệu từ nhiều file xls có cấu trúc giống nhau, code này chạy ngon lành.
Tuy nhiên, giờ em muốn thêm 1 tí này: khi mở mỗi file xls lên thì tự động chèn giá trị (tên file đó) vào cột sau cùng của sheet, sau đó mới chép dữ liệu đó vào file chính.
Tuy nhiên, khi chạy thì nó chỉ mở 1 file đầu tiên và insert tên file vào cột mong muốn rồi đứng im luôn, không tuần tự làm với các file còn lại. Em không trace ra lỗi ở chỗ nào, mong anh chị chỉ giúp.
Đoạn code như sau:
Mã:
Option Explicit
Sub import_data()
    
    Dim Master As Worksheet, sh As Worksheet
    Dim wk As Workbook
    Dim strFolderPath As String
    Dim selectedFiles As Variant
    Dim iFileNum As Integer, iLastRowReport As Integer, iNumberOfRowsToPaste As Integer
    Dim strFileName As String
    Dim rSTT As Range, rSBD As Range, rTTNV As Range, rMaNganh As Range, rTTTT As Range, rTruong As Range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim rMaNganh_Truong As Range
    Dim FileName As String
    
    
    
    getSpeed (True)
    Set Master = ActiveWorkbook.Sheets("Data")
    
    ''Xoa noi dung trong sheet hien tai
    With Sheets("Data")
    .Range("A2").Resize(20000, 7).ClearContents
    End With
    
    strFolderPath = ActiveWorkbook.Path
    
    ChDrive strFolderPath
    ChDir strFolderPath
    
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="Excel Files (*.xls*),*.xlsx*", MultiSelect:=True)
                    
    startTime = Timer
    For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
        strFileName = selectedFiles(iFileNum)
        
        Set wk = Workbooks.Open(strFileName)
        For Each sh In wk.Sheets
            If sh.Name Like "XT_DK" Then
                With sh
                    iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
                    iNumberOfRowsToPaste = iLastRowReport - 3 + 1
                    
                   'Tu dong dien ma truong theo ten file
                   FileName = wk.Name
                   .Range("F3").Select
                   .Range("F3").Value = FileName
                    Selection.AutoFill Destination:=Range("F3:F" & iLastRowReport)
                    .Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
                    Selection.AutoFill Destination:=Range("G3:G" & iLastRowReport)
                    
                                        
                    Set rSTT = .Range("A3:A" & iLastRowReport)
                    Set rSBD = .Range("B3:B" & iLastRowReport)
                    Set rTTNV = .Range("C3:C" & iLastRowReport)
                    Set rMaNganh = .Range("D3:D" & iLastRowReport)
                    Set rTTTT = .Range("E3:E" & iLastRowReport)
                    Set rTruong = .Range("F3:F" & iLastRowReport)
                    Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
                    With Data
                        iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
                        iRowStartToPaste = iCurrentLastRow + 1
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
                        .Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
                        .Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
                        .Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
                        .Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
                        .Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
                        .Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
                    End With
                    
                End With
            End If
        Next sh
        wk.Close
    Next
    
    MsgBox "Done in " & Int(Timer - startTime) & " s."
    getSpeed (False)
NoFileSelected:
    MsgBox "Chua co file nao duoc chon!"
End Sub

Function getSpeed(doIt As Boolean)
    Application.ScreenUpdating = Not (doIt)
    Application.EnableEvents = Not (doIt)
    Application.Calculation = IIf(doIt, xlCalculationManual, xlCalculationAutomatic)
End Function
 
Chào các anh, chị,
Em có đoạn code gộp dữ liệu từ nhiều file xls có cấu trúc giống nhau, code này chạy ngon lành.
Tuy nhiên, giờ em muốn thêm 1 tí này: khi mở mỗi file xls lên thì tự động chèn giá trị (tên file đó) vào cột sau cùng của sheet, sau đó mới chép dữ liệu đó vào file chính.
Tuy nhiên, khi chạy thì nó chỉ mở 1 file đầu tiên và insert tên file vào cột mong muốn rồi đứng im luôn, không tuần tự làm với các file còn lại. Em không trace ra lỗi ở chỗ nào, mong anh chị chỉ giúp.
Đoạn code như sau:
Mã:
Option Explicit
Sub import_data()
   
    Dim Master As Worksheet, sh As Worksheet
    Dim wk As Workbook
    Dim strFolderPath As String
    Dim selectedFiles As Variant
    Dim iFileNum As Integer, iLastRowReport As Integer, iNumberOfRowsToPaste As Integer
    Dim strFileName As String
    Dim rSTT As Range, rSBD As Range, rTTNV As Range, rMaNganh As Range, rTTTT As Range, rTruong As Range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim rMaNganh_Truong As Range
    Dim FileName As String
   
   
   
    getSpeed (True)
    Set Master = ActiveWorkbook.Sheets("Data")
   
    ''Xoa noi dung trong sheet hien tai
    With Sheets("Data")
    .Range("A2").Resize(20000, 7).ClearContents
    End With
   
    strFolderPath = ActiveWorkbook.Path
   
    ChDrive strFolderPath
    ChDir strFolderPath
   
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="Excel Files (*.xls*),*.xlsx*", MultiSelect:=True)
                   
    startTime = Timer
    For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
        strFileName = selectedFiles(iFileNum)
       
        Set wk = Workbooks.Open(strFileName)
        For Each sh In wk.Sheets
            If sh.Name Like "XT_DK" Then
                With sh
                    iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
                    iNumberOfRowsToPaste = iLastRowReport - 3 + 1
                   
                   'Tu dong dien ma truong theo ten file
                   FileName = wk.Name
                   .Range("F3").Select
                   .Range("F3").Value = FileName
                    Selection.AutoFill Destination:=Range("F3:F" & iLastRowReport)
                    .Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
                    Selection.AutoFill Destination:=Range("G3:G" & iLastRowReport)
                   
                                       
                    Set rSTT = .Range("A3:A" & iLastRowReport)
                    Set rSBD = .Range("B3:B" & iLastRowReport)
                    Set rTTNV = .Range("C3:C" & iLastRowReport)
                    Set rMaNganh = .Range("D3:D" & iLastRowReport)
                    Set rTTTT = .Range("E3:E" & iLastRowReport)
                    Set rTruong = .Range("F3:F" & iLastRowReport)
                    Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
                    With Data
                        iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
                        iRowStartToPaste = iCurrentLastRow + 1
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
                        .Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
                        .Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
                        .Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
                        .Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
                        .Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
                        .Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
                    End With
                   
                End With
            End If
        Next sh
        wk.Close
    Next
   
    MsgBox "Done in " & Int(Timer - startTime) & " s."
    getSpeed (False)
NoFileSelected:
    MsgBox "Chua co file nao duoc chon!"
End Sub

Function getSpeed(doIt As Boolean)
    Application.ScreenUpdating = Not (doIt)
    Application.EnableEvents = Not (doIt)
    Application.Calculation = IIf(doIt, xlCalculationManual, xlCalculationAutomatic)
End Function
code bạn muốn thêm bao nhiêu cột rồi khai báo trong code
 

File đính kèm

Upvote 0
Chào các anh, chị,
Em có đoạn code gộp dữ liệu từ nhiều file xls có cấu trúc giống nhau, code này chạy ngon lành.
Tuy nhiên, giờ em muốn thêm 1 tí này: khi mở mỗi file xls lên thì tự động chèn giá trị (tên file đó) vào cột sau cùng của sheet, sau đó mới chép dữ liệu đó vào file chính.
Tuy nhiên, khi chạy thì nó chỉ mở 1 file đầu tiên và insert tên file vào cột mong muốn rồi đứng im luôn, không tuần tự làm với các file còn lại. Em không trace ra lỗi ở chỗ nào, mong anh chị chỉ giúp.
Đoạn code như sau:
Mã:
Option Explicit
Sub import_data()
   
    Dim Master As Worksheet, sh As Worksheet
    Dim wk As Workbook
    Dim strFolderPath As String
    Dim selectedFiles As Variant
    Dim iFileNum As Integer, iLastRowReport As Integer, iNumberOfRowsToPaste As Integer
    Dim strFileName As String
    Dim rSTT As Range, rSBD As Range, rTTNV As Range, rMaNganh As Range, rTTTT As Range, rTruong As Range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim rMaNganh_Truong As Range
    Dim FileName As String
   
   
   
    getSpeed (True)
    Set Master = ActiveWorkbook.Sheets("Data")
   
    ''Xoa noi dung trong sheet hien tai
    With Sheets("Data")
    .Range("A2").Resize(20000, 7).ClearContents
    End With
   
    strFolderPath = ActiveWorkbook.Path
   
    ChDrive strFolderPath
    ChDir strFolderPath
   
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="Excel Files (*.xls*),*.xlsx*", MultiSelect:=True)
                   
    startTime = Timer
    For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
        strFileName = selectedFiles(iFileNum)
       
        Set wk = Workbooks.Open(strFileName)
        For Each sh In wk.Sheets
            If sh.Name Like "XT_DK" Then
                With sh
                    iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
                    iNumberOfRowsToPaste = iLastRowReport - 3 + 1
                   
                   'Tu dong dien ma truong theo ten file
                   FileName = wk.Name
                   .Range("F3").Select
                   .Range("F3").Value = FileName
                    Selection.AutoFill Destination:=Range("F3:F" & iLastRowReport)
                    .Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
                    Selection.AutoFill Destination:=Range("G3:G" & iLastRowReport)
                   
                                       
                    Set rSTT = .Range("A3:A" & iLastRowReport)
                    Set rSBD = .Range("B3:B" & iLastRowReport)
                    Set rTTNV = .Range("C3:C" & iLastRowReport)
                    Set rMaNganh = .Range("D3:D" & iLastRowReport)
                    Set rTTTT = .Range("E3:E" & iLastRowReport)
                    Set rTruong = .Range("F3:F" & iLastRowReport)
                    Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
                    With Data
                        iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
                        iRowStartToPaste = iCurrentLastRow + 1
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
                        .Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
                        .Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
                        .Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
                        .Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
                        .Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
                        .Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
                    End With
                   
                End With
            End If
        Next sh
        wk.Close
    Next
   
    MsgBox "Done in " & Int(Timer - startTime) & " s."
    getSpeed (False)
NoFileSelected:
    MsgBox "Chua co file nao duoc chon!"
End Sub

Function getSpeed(doIt As Boolean)
    Application.ScreenUpdating = Not (doIt)
    Application.EnableEvents = Not (doIt)
    Application.Calculation = IIf(doIt, xlCalculationManual, xlCalculationAutomatic)
End Function

Khi File được mở lên, có thuộc tính Name của Workbook xác định tên của File.
Vậy phần còn lại, phải xem bạn đặt nó ở đâu trong đoạn code, mà nó chỉ áp dụng có 01 lần vậy.
 
Upvote 0
Khi File được mở lên, có thuộc tính Name của Workbook xác định tên của File.
Vậy phần còn lại, phải xem bạn đặt nó ở đâu trong đoạn code, mà nó chỉ áp dụng có 01 lần vậy.
Phần còn lại đặt trong vòng lặp, thì nó sẽ XL hết file này đến file khác chứ anh?
 
Upvote 0
Phần còn lại đặt trong vòng lặp, thì nó sẽ XL hết file này đến file khác chứ anh?

Chính xác.
- Khi file được mở lên.
- Lấy tên File dán vào workbook đích
- Làm tiếp công việc
- Đóng lại không lưu
- Quay lại tiếp tục bước 1.
Cứ vậy là nó sẽ xuất hết cho bạn Name File thôi.
 
Upvote 0
Vậy anh xem dùm em tại sao nếu xóa đoạn màu đỏ đi thì gộp dữ liệu chạy bth, nhưng cho thêm phần màu đỏ là ko chạy nữa?
Vòng lặp như sau:

For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
strFileName = selectedFiles(iFileNum)

Set wk = Workbooks.Open(strFileName)
For Each sh In wk.Sheets
If sh.Name Like "XT_DK" Then
With sh
iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
iNumberOfRowsToPaste = iLastRowReport - 3 + 1

'Tu dong dien ma truong theo ten file
FileName = wk.Name
.Range("F3").Select
.Range("F3").Value = FileName
Selection.AutoFill Destination:=Range("F3:F" & iLastRowReport)
.Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
Selection.AutoFill Destination:=Range("G3:G" & iLastRowReport)



Set rSTT = .Range("A3:A" & iLastRowReport)
Set rSBD = .Range("B3:B" & iLastRowReport)
Set rTTNV = .Range("C3:C" & iLastRowReport)
Set rMaNganh = .Range("D3:D" & iLastRowReport)
Set rTTTT = .Range("E3:E" & iLastRowReport)
Set rTruong = .Range("F3:F" & iLastRowReport)
Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
With Data
iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
iRowStartToPaste = iCurrentLastRow + 1
.Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
.Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
.Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
.Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
.Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
.Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
.Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
.Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
End With

End With
End If
Next sh
wk.Close
Next
 
Upvote 0
Vậy anh xem dùm em tại sao nếu xóa đoạn màu đỏ đi thì gộp dữ liệu chạy bth, nhưng cho thêm phần màu đỏ là ko chạy nữa?
Vòng lặp như sau:

For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
strFileName = selectedFiles(iFileNum)

Set wk = Workbooks.Open(strFileName)
For Each sh In wk.Sheets
If sh.Name Like "XT_DK" Then
With sh
iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
iNumberOfRowsToPaste = iLastRowReport - 3 + 1

'Tu dong dien ma truong theo ten file
FileName = wk.Name
.Range("F3").Select
.Range("F3").Value = FileName
Selection.AutoFill Destination:=Range("F3:F" & iLastRowReport)
.Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
Selection.AutoFill Destination:=Range("G3:G" & iLastRowReport)



Set rSTT = .Range("A3:A" & iLastRowReport)
Set rSBD = .Range("B3:B" & iLastRowReport)
Set rTTNV = .Range("C3:C" & iLastRowReport)
Set rMaNganh = .Range("D3:D" & iLastRowReport)
Set rTTTT = .Range("E3:E" & iLastRowReport)
Set rTruong = .Range("F3:F" & iLastRowReport)
Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
With Data
iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
iRowStartToPaste = iCurrentLastRow + 1
.Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
.Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
.Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
.Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
.Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
.Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
.Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
.Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
End With

End With
End If
Next sh
wk.Close
Next

Bạn nên đua File lên, debug mới dễ, và tìm nguyên nhân nhanh hơn.
 
Upvote 0
đây anh ơi, anh xem dùm em tí

Bạn bị những lỗi sau:
- Khai báo không tương minh các đối tượng workbook, worksheet
- With data.........End With-> sửa lại With worksheets("Data").........End with
- Mình bỏ bẫy lỗi luôn, khó debug quá.
- Code khủng quá.
+ Bạn xem code rồi sửa lại nhé.

PHP:
Sub import_data()
 
    Dim Master As Worksheet, sh As Worksheet
    Dim wk As Workbook
    Dim strFolderPath As String
    Dim selectedFiles As Variant
    Dim iFileNum As Integer, iLastRowReport As Integer, iNumberOfRowsToPaste As Integer
    Dim strFileName As String
    Dim rSTT As Range, rSBD As Range, rTTNV As Range, rMaNganh As Range, rTTTT As Range, rTruong As Range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim rMaNganh_Truong As Range
    Dim FileName As String
 
 
 
    getSpeed (True)
    Set Master = Workbooks("Master.xlsm").Sheets("Data")
 

    With Sheets("Data")
    .Range("A2").Resize(20000, 7).ClearContents
    End With
 
    strFolderPath = ActiveWorkbook.Path
 
    ChDrive strFolderPath
    ChDir strFolderPath
 

    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="Excel Files (*.xls*),*.xlsx*", MultiSelect:=True)
                 
    startTime = Timer
    For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
        strFileName = selectedFiles(iFileNum)
     
        Set wk = Workbooks.Open(strFileName)
        For Each sh In wk.Sheets
            If sh.Name Like "XT_DK" Then
                With sh
                    iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
                    iNumberOfRowsToPaste = iLastRowReport - 3 + 1
                 

                   FileName = wk.Name
                   .Range("F3").Select
                   .Range("F3").Value = FileName
                    .Range("F3").AutoFill Destination:=sh.Range("F3:F" & iLastRowReport)
                    .Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
                    .Range("G3").AutoFill Destination:=sh.Range("G3:G" & iLastRowReport)
                 
                                     
                    Set rSTT = .Range("A3:A" & iLastRowReport)
                    Set rSBD = .Range("B3:B" & iLastRowReport)
                    Set rTTNV = .Range("C3:C" & iLastRowReport)
                    Set rMaNganh = .Range("D3:D" & iLastRowReport)
                    Set rTTTT = .Range("E3:E" & iLastRowReport)
                    Set rTruong = .Range("F3:F" & iLastRowReport)
                    Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
                    With Workbooks("Master.xlsm").Worksheets("Data")
                        iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
                        iRowStartToPaste = iCurrentLastRow + 1
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
                        .Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
                        .Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
                        .Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
                        .Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
                        .Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
                        .Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
                    End With
                 
                End With
            End If
        Next sh
        wk.Close
    Next
 
    MsgBox "Done in " & Int(Timer - startTime) & " s."
    getSpeed (False)
End Sub
 

File đính kèm

Upvote 0
đây anh ơi, anh xem dùm em tí
chỉnh cho bạn vài lệnh
Mã:
Sub import_data()
   
    Dim Master As Worksheet, sh As Worksheet
    Dim wk As Workbook
    Dim strFolderPath As String
    Dim selectedFiles As Variant
    Dim iFileNum As Integer, iLastRowReport As Integer, iNumberOfRowsToPaste As Integer
    Dim strFileName As String
    Dim rSTT As Range, rSBD As Range, rTTNV As Range, rMaNganh As Range, rTTTT As Range, rTruong As Range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim rMaNganh_Truong As Range
    Dim FileName As String
   
    getSpeed (True)
    Set Master = ActiveWorkbook.Sheets("Data")
   
    ''Xoa noi dung trong sheet hien tai
    With Sheets("Data")
    .Range("A2").Resize(20000, 7).ClearContents
    End With
   
    strFolderPath = ActiveWorkbook.Path
   
    ChDrive strFolderPath
    ChDir strFolderPath
   
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="Excel Files (*.xls*),*.xlsx*", MultiSelect:=True)
    If Not IsArray(selectedFiles) Then
      getSpeed (False)
      MsgBox "Chua co file nao duoc chon!"
      Exit Sub
    End If
    startTime = Timer
    For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
        strFileName = selectedFiles(iFileNum)
       
        Set wk = Workbooks.Open(strFileName)
        For Each sh In wk.Sheets
            If sh.Name Like "XT_DK" Then
                With sh
                    iLastRowReport = .Range("A" & Rows.Count).End(xlUp).Row
                    iNumberOfRowsToPaste = iLastRowReport - 3 + 1
                   
                   'Tu dong dien ma truong theo ten file
                    .Range("F3").Value = wk.Name
                    .Range("F3").AutoFill Destination:=Range("F3:F" & iLastRowReport)
                    .Range("G3").FormulaR1C1 = "=RC[-1]&""_""&RC[-3]"
                    .Range("G3").AutoFill Destination:=Range("G3:G" & iLastRowReport)
                                       
                    Set rSTT = .Range("A3:A" & iLastRowReport)
                    Set rSBD = .Range("B3:B" & iLastRowReport)
                    Set rTTNV = .Range("C3:C" & iLastRowReport)
                    Set rMaNganh = .Range("D3:D" & iLastRowReport)
                    Set rTTTT = .Range("E3:E" & iLastRowReport)
                    Set rTruong = .Range("F3:F" & iLastRowReport)
                    Set rMaNganh_Truong = .Range("G3:G" & iLastRowReport)
                    With Master
                        iCurrentLastRow = .Range("A" & Rows.Count).End(xlUp).Row
                        iRowStartToPaste = iCurrentLastRow + 1
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1).NumberFormat = "@"
                        .Range("A" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSTT.Value2
                        .Range("B" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rSBD.Value2
                        .Range("C" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTNV.Value2
                        .Range("D" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh.Value2
                        .Range("E" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTTTT.Value2
                        .Range("F" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rTruong.Value2
                        .Range("G" & iRowStartToPaste).Resize(iNumberOfRowsToPaste, 1) = rMaNganh_Truong.Value2
                    End With
                   
                End With
            End If
        Next sh
        wk.Close
    Next
   
    MsgBox "Done in " & Int(Timer - startTime) & " s."
    getSpeed (False)
End Sub
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom