如何根据两张纸中的单元格值将某些单元格从一张纸复制到另一张纸?

问题描述 投票:0回答:1

我有一系列不连续的单元格,我想将它们从一张纸 (wseDNA1) 复制到第二张纸 (wsElog1)。我只想在 wseDNA1 中的值不是“NR”并且 wsElog1 中的单元格尚未包含值时进行复制。 我想做一个循环,但不知道如何在不查看每个单元格(而不仅仅是我想要复制的单元格)的情况下执行此操作。下面是没有条件规则的复制范围的示例。我每天都会有一百多个这样的地图来绘制。

wsElog1.Range("BP" & RowNum) = wseDNA1.Range("AE12") 
wsElog1.Range("BQ" & RowNum) = wseDNA1.Range("AG12") 
wsElog1.Range("BN" & RowNum) = wseDNA1.Range("AO12") 
wsElog1.Range("BR" & RowNum) = wseDNA1.Range("AQ12") 
wsElog1.Range("BS" & RowNum) = wseDNA1.Range("AS12") ```
excel conditional-statements copy multiple-conditions
1个回答
0
投票

循环并有条件复制值

  • 这就是我已经走了多远。如果您可以在评论中分享我所要求的信息,那就会更好。
Sub Test()
    
    ' Make sure these two have the same number of elements
    ' and that they are correctly associated.
    Dim eCols() As Variant: eCols = VBA.Array("BP", "BQ", "BN", "BR", "BS")
    Dim dCols() As Variant: dCols = VBA.Array("AE", "AG", "AO", "AQ", "AS")

    Dim nUpper As Long: nUpper = UBound(eCols)

    Dim RowNum As Long, n As Long, eStr As String, dStr As String
    
    For RowNum = 2 To 3 ' adjust!
        For n = 0 To nUpper
            eStr = CStr(wsElog1.Cells(RowNum, eCols(n)).Value)
            If Len(eStr) > 0 Then
                dStr = CStr(wsDNA1.Cells(RowNum, dCols(n)).Value)
                If StrComp(dStr, "NR", vbTextCompare) <> 0 Then
                    wsElog1.Cells(RowNum, eCols(n)).Value _
                        = wsDNA1.Cells(12, dCols(n)).Value
                End If
            End If
        Next n
    Next RowNum

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.