Nhờ anh chị viết hộ em code lấy dữ liệu file khác

Liên hệ QC

vova2209

Thành viên tích cực
Tham gia
5/4/17
Bài viết
828
Được thích
110
Giới tính
Nam
Nghề nghiệp
Đường bộ
Em có 1 mong muốn nhỏ nhờ anh chị giúp đỡ!
Nhờ anh chị viết cho em code lấy dữ liệu dựa trên 2 điều kiện như hình dưới đây.. em xin chân thành cám ơn!

Untitled.png
 

File đính kèm

  • Data.xlsx
    92.1 KB · Đọc: 9
  • lay du lieu DaTa.xlsm
    51 KB · Đọc: 9
Mình nhầm v

Để lại cột F chi cho rối, sao không để code tính luôn cho gọn
Mã:
Sub Import_Data()
    Dim shMain As Worksheet, sh As Worksheet
    Dim wb As Workbook
    Dim strFolderPath As String
    Dim selectedFiles As Variant, sCol As Variant
    Dim sArr(), Res1(), Res2(), eRow As Integer, i As Integer, k As Integer, j As Integer
    Dim iFileNum As Integer
    Dim startTime As Double
    getSpeed (True)
   
    eRow = Range("B" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then
      Range("B3:E" & eRow).ClearContents
      Range("G3:H" & eRow).ClearContents
    End If
    sCol = Array("", 2, 8, 27, 37, "", 40, 41)
    Set shMain = ActiveWorkbook.Sheets("List BB")

    strFolderPath = ActiveWorkbook.Path
    ChDrive strFolderPath
    ChDir strFolderPath
   
    On Error GoTo NoFileSelected
    selectedFiles = Application.GetOpenFilename( _
                    filefilter:="Excel Files (*.xls*),*.xlsx*,*.xlsm*,*.xlsb*", MultiSelect:=True)
    'startTime = Timer
    For iFileNum = LBound(selectedFiles) To UBound(selectedFiles)
        Set wb = Workbooks.Open(selectedFiles(iFileNum))
        For Each sh In wb.Sheets
            If sh.Name Like "*VoVa" Then
                eRow = sh.Range("A" & Rows.Count).End(xlUp).Row
                If eRow > 2 Then
                    sArr = sh.Range("D3:AR" & eRow).Value
                    ReDim Res1(LBound(sArr) To UBound(sArr), 1 To 4)
                    ReDim Res2(LBound(sArr) To UBound(sArr), 1 To 2)
                    k = 0
                    For i = LBound(sArr) To UBound(sArr)
                        If Len(sArr(i, 2)) Then
                            If Len(sArr(i, 1)) = 0 Then
                                k = k + 1
                                For j = 1 To 4
                                    Res1(k, j) = sArr(i, sCol(j))
                                Next j
                                Res2(k, 1) = sArr(i, sCol(6))
                                Res2(k, 2) = sArr(i, sCol(7))
                            End If
                        End If
                    Next i
                End If
                If k Then
                    eRow = shMain.Range("B" & Rows.Count).End(xlUp).Row
                    If eRow < 3 Then eRow = 3 Else eRow = eRow + 2
                    shMain.Range("B" & eRow).Resize(k, 4) = Res1
                    shMain.Range("G" & eRow).Resize(k, 2) = Res2
                End If
            End If
        Next sh
        wb.Close savechanges:=False
    Next
    'MsgBox "Done in " & Int(Timer - startTime) & " s."
    getSpeed (False)
NoFileSelected:
    'MsgBox "Chu*a có File nào duoc chon!"
End Sub
Ngon rồi! vì em tính cột F còn tác dụng khi thay đổi giá trị tại mấy cột ngay cạnh. em cảm ơn
 
Upvote 0
Web KT
Back
Top Bottom