Nhờ các cao thủ sửa 1 đoạn code ngắn (1 người xem)

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

kid_trainee

Thành viên mới
Tham gia
3/1/15
Bài viết
2
Được thích
0
Chào các bác.

Em muốn sử dụng VBA để tự động tổng hợp số liệu từ các file được chọn vào một file.

Đây là file mẫu và đoạn code của em. Trong folder này có 3 file, em muốn lần lượt copy data ở các ô E33, J33, O33 trong file kqdq_alt.csv và kqdq_bcb.csv vào dòng cuối cùng không chứa dữ liệu ở trong file master.xlsx

Đây là phần khai báo biến và truy cập vào đường link chứa file.

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 rCopiedRange As range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim i As Integer
    
    
    Set master = ActiveWorkbook.ActiveSheet
    
    strFolderPath = ActiveWorkbook.Path
    
    ChDrive strFolderPath
    ChDir strFolderPath

Ở đây em lưu các file được chọn vào một biến có tên là "selectedFiles. Đồng thời "On Error GoTo NoFileSelected" để thông báo lỗi
Mã:
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="All Files (*.csv*),*.csv*", MultiSelect:=True)
Đoạn dưới là để truy cập vào từng file và lấy số liệu
Mã:
    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 "kqdq_*" Then
                With sh
                    For i = 1 To 3
                            Cells(60, i) = 2014 - i
                            Cells(61, i) = Cells(33, 5 * i).Value
                    Next i
                    Set rCopiedRange = .range(Cells(61, 1), Cells(61, 3))
                End With
[COLOR=#ff0000] With master
                        iCurrentLastRow = .range("A100").End(xlUp).Row
                        iRowStartToPaste = (iCurrentLastRow + 2)
                        .range("A" & iRowStartToPaste).Resize(1, 3) = rCopiedRange.Value2  
                    End With[/COLOR][B]
                                    
            End If
        Next sh
        wk.Close
    Next iFileNum
    

NoFileSelected:
    MsgBox "No file selected"[/B]
End Sub
Các cao thủ giúp em với ạ. Em nghĩ suốt 2 ngày rồi mà không hiểu sai ở đâu.+-+-+-+ phần tô đỏ có gì sai ko ạ?
 

File đính kèm

Lần chỉnh sửa cuối:
Chào các bác.

Em muốn sử dụng VBA để tự động tổng hợp số liệu từ các file được chọn vào một file.

Đây là file mẫu và đoạn code của em

Đây là phần khai báo biến và truy cập vào đường link chứa file. Trong folder này có 3 file, em muốn copy data từ file kqdq_alt.csv và kqdq_bcb.csv vào file master.xlsx

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 rCopiedRange As range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim i As Integer
    
    
    Set master = ActiveWorkbook.ActiveSheet
    
    strFolderPath = ActiveWorkbook.Path
    
    ChDrive strFolderPath
    ChDir strFolderPath

Ở đây em lưu các file được chọn vào một biến có tên là "selectedFiles. Đồng thời "On Error GoTo NoFileSelected" để thông báo lỗi
Mã:
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="All Files (*.csv*),*.csv*", MultiSelect:=True)
Đoạn dưới là để truy cập vào từng file và lấy số liệu
Mã:
    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 "kqdq_*" Then
                With sh
                    For i = 1 To 3
                            Cells(60, i) = 2014 - i
                            Cells(61, i) = Cells(33, 5 * i).Value
                    Next i
                    Set rCopiedRange = .range(Cells(61, 1), Cells(61, 3))
                End With
[B]                    
                   With master
                        iCurrentLastRow = .range("A100").End(xlUp).Row
                        iRowStartToPaste = (iCurrentLastRow + 2)
                        .range("A" & iRowStartToPaste).Resize(1, 3) = rCopiedRange.Value2  
                    End With
                                    
            End If
        Next sh
        wk.Close
    Next iFileNum
    

NoFileSelected:
    MsgBox "No file selected"[/B]
End Sub
Các cao thủ giúp em với ạ. EmView attachment 134754 nghĩ suốt 2 ngày rồi mà không hiểu sai ở đâu.+-+-+-+
Mình thấy mọi người viết thế này
PHP:
   For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
      Workbooks.Open selectedFiles(yiFileNum)
      With ActiveWorkbook
         For Each sh In .Worksheets
                'Code here
         Next
         .Close False
      End With
   Next
 
Upvote 0
thanks bác đã rep. Nhưng hình như chưa vào đoạn code chính ạ mà em cũng không hiểu lắm ý bác.
 
Upvote 0
Chào các bác.

Em muốn sử dụng VBA để tự động tổng hợp số liệu từ các file được chọn vào một file.

Đây là file mẫu và đoạn code của em. Trong folder này có 3 file, em muốn lần lượt copy data ở các ô E33, J33, O33 trong file kqdq_alt.csv và kqdq_bcb.csv vào dòng cuối cùng không chứa dữ liệu ở trong file master.xlsx

Đây là phần khai báo biến và truy cập vào đường link chứa file.

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 rCopiedRange As range
    Dim iCurrentLastRow As Integer, iRowStartToPaste As Integer
    Dim startTime As Double
    Dim i As Integer
    
    
    Set master = ActiveWorkbook.ActiveSheet
    
    strFolderPath = ActiveWorkbook.Path
    
    ChDrive strFolderPath
    ChDir strFolderPath

Ở đây em lưu các file được chọn vào một biến có tên là "selectedFiles. Đồng thời "On Error GoTo NoFileSelected" để thông báo lỗi
Mã:
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="All Files (*.csv*),*.csv*", MultiSelect:=True)
Đoạn dưới là để truy cập vào từng file và lấy số liệu
Mã:
    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 "kqdq_*" Then
                With sh
                    For i = 1 To 3
                            Cells(60, i) = 2014 - i
                            Cells(61, i) = Cells(33, 5 * i).Value
                    Next i
                    Set rCopiedRange = .range(Cells(61, 1), Cells(61, 3))
                End With
[COLOR=#ff0000] With master
                        iCurrentLastRow = .range("A100").End(xlUp).Row
                        iRowStartToPaste = (iCurrentLastRow + 2)
                        .range("A" & iRowStartToPaste).Resize(1, 3) = rCopiedRange.Value2  
                    End With[/COLOR][B]
                                    
            End If
        Next sh
        wk.Close
    Next iFileNum
    

NoFileSelected:
    MsgBox "No file selected"[/B]
End Sub
Các cao thủ giúp em với ạ. Em nghĩ suốt 2 ngày rồi mà không hiểu sai ở đâu.+-+-+-+ phần tô đỏ có gì sai ko ạ?
Mã:
 selectedFiles = Application.GetOpenFilename( _
                    filefilter:="All Files (*.csv*),*.csv*", MultiSelect:=True)
Bạn kiểm tra lại đoạn này xem sao và phần dấu chấm trước Cells như anh Quang Hải góp ý
 
Upvote 0

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

Back
Top Bottom