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
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!

View attachment 199189
Anh thấy kết quả sheet List BB đâu có giống nội dung em nêu tại BG5:BK20, em phải đưa vào thì người ta mới hiểu.
Cái này anh thấy làm thủ công có khi còn lẹ hơn chờ code:
1/ Copy sheet VoVa sang, Filter cột D chọn Blanks, Delete dòng.
2/ Xóa cột không cần thiết.
 
Upvote 0
Anh thấy kết quả sheet List BB đâu có giống nội dung em nêu tại BG5:BK20, em phải đưa vào thì người ta mới hiểu.
đây là file mẫu để lấy dữ liệu sang thôi a
copy từ tất cả dữ liệu từ dòng thứ 4 trở đi sang 1 cột file khác dựa trên 2 điều kiện
Untitled1.png
 
Upvote 0
Upvote 0
Cột D Sheet VoVa chỉ có số 2 và 3, còn trong hình thì 1,3, 6, 7, 10, 11.
Nó đâu có thống nhất đâu mà hiểu.
Nghĩa là khi copy dữ liệu sang thì điện kiện ko copy những dòng mà cột D có dữ liệu, và cột dữ liệu có dòng trống sẽ bỏ qua để copy dữ liệu sang kia liền kết quả như ở ảnh trên Điều kiện:
copy sang file khác ý anh ạ. em đăng lên 2 file mà
 
Lần chỉnh sửa cuối:
Upvote 0
Nghĩa là khi copy dữ liệu sang thì điện kiện ko copy những dòng mà cột D có dữ liệu, và cột dữ liệu có dòng trống sẽ bỏ qua để copy dữ liệu sang kia liền kết quả như ở ảnh trên Điều kiện:
Em xem lại điều kiện (trong hình) cột D chỉ có số 2 và 3 (số 2 thì trùng), cột F thì lại ghi K=0,95 số 1, 3, 6, 7, 10, 11, trong khi cột D thì không có.
Ý anh nêu là em ví dụ không thống nhất thì làm sao hiểu nổi.
 
Upvote 0
trong hình thì
Em xem lại điều kiện cột D chỉ có số 2 và 3 (số 2 thì trùng), cột F thì lại ghi K=0,95 số 1, 3, 6, 7, 10, 11, trong khi cột D thì không có.
Ý anh nêu là em ví dụ không thống nhất thì làm sao hiểu nổi.
để e tường thuật lại đã rồi anh giúp em nhé!
 
Upvote 0
Em xem lại điều kiện (trong hình) cột D chỉ có số 2 và 3 (số 2 thì trùng), cột F thì lại ghi K=0,95 số 1, 3, 6, 7, 10, 11, trong khi cột D thì không có.
Ý anh nêu là em ví dụ không thống nhất thì làm sao hiểu nổi.
em trình bầy lại theo như hình ở 2 ảnh dưới anh xem dùm em
ành 1 copy dữ liệu sang
file tên Data
Untitled.png

Ảnh 2: file > lay du lieu DaTa
Copy sang file này Kết Quả như dưới
Untitled 2.png
 
Lần chỉnh sửa cuối:
Upvote 0
Em xem lại điều kiện (trong hình) cột D chỉ có số 2 và 3 (số 2 thì trùng), cột F thì lại ghi K=0,95 số 1, 3, 6, 7, 10, 11, trong khi cột D thì không có.
Ý anh nêu là em ví dụ không thống nhất thì làm sao hiểu nổi.
Đây anh ơi! em update code vào file rồi, giờ làm sao loại bỏ điều kiện
1. cột D có dữ liệu thì ko copy dòng đấy
2. Cột E dữ liệu trống sẽ bỏ qua ko copy dòng đấy
 

File đính kèm

  • lay du lieu DaTa.xlsm
    54.5 KB · Đọc: 7
  • Data.xlsx
    53.6 KB · Đọc: 4
Upvote 0
Đây anh ơi! em update code vào file rồi, giờ làm sao loại bỏ điều kiện
1. cột D có dữ liệu thì ko copy dòng đấy
2. Cột E dữ liệu trống sẽ bỏ qua ko copy dòng đấy
File bài #1 xử lý được, bài #9 thua
Căn cứ vào đâu bạn tô màu đánh dấu các dòng bài #9? :mad:
 
Upvote 0
File bài #1 xử lý được, bài #9 thua
Căn cứ vào đâu bạn tô màu đánh dấu các dòng bài #9? :mad:
Code trong file
Cái phần bôi mầu là em đánh dấu code chưa loại bỏ được 2 điều kiện đó khi lấy dữ liệu từ file Data sang
Phần bôi màu xám là em không muốn lấy sang:

Untitled.png

em lấy được 1 code trên mạng giờ muốn tùy biến thêm:
Untitled.png
 
Lần chỉnh sửa cuối:
Upvote 0

File đính kèm

  • Data.xlsx
    52.7 KB · Đọc: 8
  • lay du lieu DaTa.xlsm
    51.9 KB · Đọc: 10
Upvote 0
Vâng em cảm ơn! anh giúp em nhé
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(), Res(), eRow As Integer, i As Integer, k As Integer, j As Integer
    Dim iFileNum As Integer
    Dim startTime As Double
    getSpeed (True)
    
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then Range("A2:H" & eRow).ClearContents
    sCol = Array("", "", 3, 8, 27, 37, 0, 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))
        k = 0
        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 Res(LBound(sArr) To UBound(sArr), 1 To 8)
                    For i = LBound(sArr) To UBound(sArr)
                        If Len(sArr(i, 3)) Then
                            If Len(sArr(i, 1)) = 0 Then
                                k = k + 1
                                Res(k, 1) = k
                                For j = 2 To UBound(sCol)
                                    If sCol(j) > 0 Then Res(k, j) = sArr(i, sCol(j))
                                Next j
                            Else
                                Res(k, 2) = Res(k, 2) & ", " & sArr(i, 3)
                            End If
                        End If
                    Next i
                End If
                If k Then
                    eRow = shMain.Range("B" & Rows.Count).End(xlUp).Row
                    If eRow = 1 Then eRow = 2 Else eRow = eRow + 2
                    shMain.Range("A" & eRow).Resize(k, 8) = Res
                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
    
'TAT CHE DO TINH TOAN VA CAP NHAT MAN HINH
Function getSpeed(doIt As Boolean)
    Application.ScreenUpdating = Not (doIt)
    Application.EnableEvents = Not (doIt)
    Application.Calculation = IIf(doIt, xlCalculationManual, xlCalculationAutomatic)
End Function
 
Upvote 0
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(), Res(), eRow As Integer, i As Integer, k As Integer, j As Integer
    Dim iFileNum As Integer
    Dim startTime As Double
    getSpeed (True)

    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then Range("A2:H" & eRow).ClearContents
    sCol = Array("", "", 3, 8, 27, 37, 0, 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))
        k = 0
        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 Res(LBound(sArr) To UBound(sArr), 1 To 8)
                    For i = LBound(sArr) To UBound(sArr)
                        If Len(sArr(i, 3)) Then
                            If Len(sArr(i, 1)) = 0 Then
                                k = k + 1
                                Res(k, 1) = k
                                For j = 2 To UBound(sCol)
                                    If sCol(j) > 0 Then Res(k, j) = sArr(i, sCol(j))
                                Next j
                            Else
                                Res(k, 2) = Res(k, 2) & ", " & sArr(i, 3)
                            End If
                        End If
                    Next i
                End If
                If k Then
                    eRow = shMain.Range("B" & Rows.Count).End(xlUp).Row
                    If eRow = 1 Then eRow = 2 Else eRow = eRow + 2
                    shMain.Range("A" & eRow).Resize(k, 8) = Res
                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

'TAT CHE DO TINH TOAN VA CAP NHAT MAN HINH
Function getSpeed(doIt As Boolean)
    Application.ScreenUpdating = Not (doIt)
    Application.EnableEvents = Not (doIt)
    Application.Calculation = IIf(doIt, xlCalculationManual, xlCalculationAutomatic)
End Function
Code copy nhầm cột đầu tiên rồi anh ơi (cột đầu em muốn là cột E) như ảnh bài #12!
+ Kết quả lấy sang file lay du lieu DaTa chỉnh lại hộ e bắt đầu từ dòng thứ 3
+ Bỏ hộ em code đánh số thứ tự với ạ
 
Lần chỉnh sửa cuối:
Upvote 0
Code copy nhầm cột đầu tiên rồi anh ơi (cột đầu em muốn là cột E) như ảnh bài #12!
+ Kết quả lấy sang file lay du lieu DaTa chỉnh lại hộ e bắt đầu từ dòng thứ 3
+ Bỏ hộ em code đánh số thứ tự với ạ
Code cho bài #12
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(), Res(), eRow As Integer, i As Integer, k As Integer, j As Integer
    Dim iFileNum As Integer
    Dim startTime As Double
    getSpeed (True)
    
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then Range("B3:H" & eRow).ClearContents
    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))
        k = 0
        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 Res(LBound(sArr) To UBound(sArr), 1 To 7)
                    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 UBound(sCol)
                                    If j <> 5 Then Res(k, j) = sArr(i, sCol(j))
                                Next j
                                Res(k, 5) = Res(k, 6) - Res(k, 4) + 1
                            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, 7) = Res
                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
 
Upvote 0
Vâng UOTE="HieuCD, post: 867435, member: 373036"]Code cho bài #12
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(), Res(), eRow As Integer, i As Integer, k As Integer, j As Integer
    Dim iFileNum As Integer
    Dim startTime As Double
    getSpeed (True)
   
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then Range("B3:H" & eRow).ClearContents
    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))
        k = 0
        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 Res(LBound(sArr) To UBound(sArr), 1 To 7)
                    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 UBound(sCol)
                                    If j <> 5 Then Res(k, j) = sArr(i, sCol(j))
                                Next j
                                Res(k, 5) = Res(k, 6) - Res(k, 4) + 1
                            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, 7) = Res
                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
[/QUOTE]
Vâng chiều về em xem.. Có j mắc giúp e nốt nhé
 
Upvote 0
Code cho bài #12
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(), Res(), eRow As Integer, i As Integer, k As Integer, j As Integer
    Dim iFileNum As Integer
    Dim startTime As Double
    getSpeed (True)

    eRow = Range("A" & Rows.Count).End(xlUp).Row
    If eRow > 1 Then Range("B3:H" & eRow).ClearContents
    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))
        k = 0
        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 Res(LBound(sArr) To UBound(sArr), 1 To 7)
                    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 UBound(sCol)
                                    If j <> 5 Then Res(k, j) = sArr(i, sCol(j))
                                Next j
                                Res(k, 5) = Res(k, 6) - Res(k, 4) + 1
                            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, 7) = Res
                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
Bỏ cho em cái code trừ ngày ở cột F này với
em xóa đoạn code này, em điền tay 1 công thức khác vào cột F khi chạy code thì toàn bị xóa dữ liệu cột F
Res(k, 5) = Res(k, 6) - Res(k, 4) + 1


Untitled.png
 
Lần chỉnh sửa cuối:
Upvote 0
Bị lỗi khi lấy thêm dữ liệu file tiếp theo, nó xóa mất dòng đầu tiên, anh xem dùm hộ em

View attachment 199270

View attachment 199271
Mình nhầm v
Bỏ cho em cái code trừ ngày ở cột F này với
em xóa đoạn code này, em điền tay 1 công thức khác vào cột F khi chạy code thì toàn bị xóa dữ liệu cột F
Res(k, 5) = Res(k, 6) - Res(k, 4) + 1


View attachment 199276
Để 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
 
Upvote 0
Web KT
Back
Top Bottom