简体   繁体   English

Excel VBA根据条件从一张纸复制到另一张纸的特定单元格

[英]Excel VBA copy from one sheet to other sheets specific cells based on criteria

I am trying to copy from Sheet1, specific rows when on that row a specific cell has status "DONE" selected to say, and a second criteria after "DONE" is to check if on the same row, another cell has also a specific value. 我正在尝试从Sheet1复制特定行,当该行上的某个特定单元格的状态选择为“ DONE”时要说,而“ DONE”之后的第二个条件是检查同一行上是否另一个单元格也具有特定值。 After that, copy the rows found each on specific sheet, checking destination if duplicates are found. 之后,复制在特定工作表上找到的每行,检查目标是否找到重复项。

I have managed until now to copy from Sheet1 to the other based on the 2 criteria (old school with IF, I tried with autofilter but I didn't manage to do it) but I am having a hard time preventing duplicates to be copied to the other sheets. 到目前为止,我已经设法根据2个条件从Sheet1复制到另一个(使用IF的老派,我尝试使用自动过滤器,但是我没有设法做到),但是我很难阻止重复项被复制到其他的床单。

I tried everything, value checking based on first sheet with Range, writing a macro for each sheet so it prevents duplicates, nothing worked and i am stuck on this. 我尝试了一切,使用Range进行了基于第一张纸的价值检查,为每张纸写了一个宏,以防止重复,没有任何效果,我被卡在了这上面。

Another problem with below code is that after hitting Update button multiple times, it doesn't duplicate all found rows, but only the first one found, and also inserts some empty rows in between and I don't understand the reason for that. 下面的代码的另一个问题是,在多次单击“更新”按钮后,它不会复制所有找到的行,而只会复制找到的第一行,并且还会在两者之间插入一些空行,我不知道这样做的原因。

Here is the code: 这是代码:

Private Sub CommandButton1_Click()
Dim LastRow As Long
Dim i As Long, j As Long, k As Long, j1 As Long, k1 As Long, j_last As Long,
k_last As Long
Dim a As Long, b As Long
Dim ActiveCell As String

With Worksheets("PDI details")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With Worksheets("Demo ATMC")
    j = .Cells(.Rows.Count, "A").End(xlUp).Row + 2
End With

With Worksheets("Demo ATMC Courtesy")
    k = .Cells(.Rows.Count, "A").End(xlUp).Row + 2
End With

With Worksheets("Demo SHJ")
    j1 = .Cells(.Rows.Count, "A").End(xlUp).Row
    k1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With Worksheets("Demo AD")
    a = .Cells(.Rows.Count, "A").End(xlUp).Row
    b = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

MsgBox (j)
For i = 5 To LastRow
    With Worksheets("PDI details")
        If .Cells(i, 20).Value <> "" Then

            If .Cells(i, 20).Value = "DONE" Then
                If .Cells(i, 11).Value = "ATMC DEMO" Then

                    If Not .Cells(i, 7) = Worksheets("Demo ATMC").Range("D4") Then
                        Worksheets("Demo ATMC").Range("A" & j) = Worksheets("PDI details").Range("A" & i).Value
                        Worksheets("Demo ATMC").Range("B" & j) = Worksheets("PDI details").Range("E" & i).Value
                        Worksheets("Demo ATMC").Range("C" & j) = Worksheets("PDI details").Range("F" & i).Value
                        Worksheets("Demo ATMC").Range("D" & j) = Worksheets("PDI details").Range("G" & i).Value
                        Worksheets("Demo ATMC").Range("F" & j) = Worksheets("PDI details").Range("H" & i).Value
                        Worksheets("Demo ATMC").Range("G" & j) = Worksheets("PDI details").Range("I" & i).Value

                    End If
                End If
                If .Cells(i, 11).Value = "ATMC COURTESY" Then
                    If Not .Cells(i, 7) = Worksheets("Demo ATMC Courtesy").Range("D4")
                    Then
                        Worksheets("Demo ATMC Courtesy").Range("A" & k) = Worksheets("PDI details").Range("A" & i).Value
                        Worksheets("Demo ATMC Courtesy").Range("B" & k) = Worksheets("PDI details").Range("E" & i).Value
                        Worksheets("Demo ATMC Courtesy").Range("C" & k) = Worksheets("PDI details").Range("F" & i).Value
                        Worksheets("Demo ATMC Courtesy").Range("D" & k) = Worksheets("PDI details").Range("G" & i).Value
                        Worksheets("Demo ATMC Courtesy").Range("F" & k) = Worksheets("PDI details").Range("H" & i).Value
                        Worksheets("Demo ATMC Courtesy").Range("G" & k) = Worksheets("PDI details").Range("I" & i).Value

                        k = k + 1
                    End If
                End If
            End If
        End If
    End With
Next i
End Sub

I couldn't test the code suggested below but I believe that it does what you wish it to do. 我无法测试下面建议的代码,但我相信它可以满足您的要求。

Option Explicit

Private Sub CommandButton1_Click()
    ' 23 Dec 2017

    Dim WsPdi As Worksheet
    Dim WsAtmc As Worksheet, WsCourtesy As Worksheet
    Dim R As Long, Rl As Long               ' row / lastrow "PDI details"

    Set WsPdi = Worksheets("PDI Detail")
    Set WsAtmc = Worksheets("Demo ATMC")
    Set WsCourtesy = Worksheets("Demo ATMC Courtesy")

    Application.ScreenUpdating = False
    With WsPdi
        Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
        For R = 5 To Rl
            If .Cells(R, 20).Value = "DONE" Then
                Select Case .Cells(R, 11).Value
                    Case "ATMC DEMO"
                        TransferData WsPdi, WsAtmc, R
                    Case "ATMC COURTESY"
                        TransferData WsPdi, WsCourtesy, R
                End Select
            End If
        Next R
    End With
    Application.ScreenUpdating = True
End Sub

Private Sub TransferData(WsSource As Worksheet, _
                         WsDest As Worksheet, _
                         R As Long)
    ' 23 Dec 2017

    Dim Csource() As String
    Dim Rn As Long                          ' next empty row in WsDest
    Dim C As Long

    Csource = Split(",A,E,F,G,,H,R", ",")
    With WsDest
        If WsSource.Cells(R, 7).Value <> .Cells(4, "D").Value Then
            Rn = .Cells(.LastRow, "A").End(xlUp).Row + 1
            For C = 1 To 7                      ' columns A to G
                If C <> 5 Then
                    .Cells(Rn, C).Value = WsSource.Cells(R, Csource(C)).Value
                End If
            Next C
        End If
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将单元格中的单元格复制到多张Excel - VBA中 - copy cells from one sheet into multiple sheets Excel - VBA VBA - 根据汇总 Excel 表格上的条件,将工作簿中的不同模板工作表复制到另一个工作簿的多张工作表中 - VBA - copy different template sheets from a workbook, into multiple sheets of another workbook based on criteria on a summary excel sheet Excel VBA:将单元格从多个工作表复制到单个工作表 - Excel VBA: Copy cells from multiple sheets to a single sheet VBA-根据多个条件从另一张纸复制单元格 - VBA - copy cells from another sheet based on multiple criteria Excel - 根据没有 VBA 的条件将行从一个工作表复制到另一个工作表 - Excel - Copy rows from one sheet to another sheet based in a criteria without VBA 根据其他单元格中的值将行数据从一张纸复制到一张或多张纸 - copy row data from one sheet to one or more sheets based on values in other cells 将合并后的单元格从其他工作表复制到摘要工作表中的一个单元格中 - Copy merged cells from other sheets into one cell in summary sheet 如果条件如何将特定单元格从一张纸复制到另一张纸 - How to copy specific cells from one sheet to another meting if criteria 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell Excel VBA在给定多个条件的情况下从一张工作表复制到其他工作簿 - Excel VBA Copy from one sheet to other workbook given multiple criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM