有条件地将数据从一个工作表传输到另一个工作表



我希望你能帮助我。我正在尝试根据多个索引和规范复制某些数据。我在下面描述了我的问题:

嗨,我是 VBA 的基本学习者。我可以移动整个数据,但不能做条件格式。

我目前还没有准备好代码

我希望根据条件将数据从工作表 1 传输到工作表 2、工作表 3、工作表 4:

copy cell D VALUE if worksheet 1  column A = "Are" 
(if column B = 2 (open worksheet 2), 
AND 
if column C = "You" paste the multiple values of column D in cells C12 TO C19
if column c = "me" paste the multiple values of column D in cell C20 TO C29
if column B = 3 (open worksheet 3), 
AND
if column C = "You" paste the multiple values of column D in cells C12 TO C19
if column c = "me" paste the multiple values of column D in cell C20 TO C29
if column B = 4 (open worksheet 4))  
AND 
if column C = "You" paste the multiple values of column D in cells C12 TO C19
if column c = "me" paste the multiple values of column D in cell C20 TO C29

谢谢!!

拆分为多个工作表

Option Explicit
Sub SplitToMultiWorksheets()
    Const cSheets As String = "Sheet1,Sheet2,Sheet3,Sheet4"
    Const cStr1 As String = "Are"
    Const cStr2 As String = "You"
    Const cStr3 As String = "me"
    Const cFirstR As Long = 1
    Const cCol1 As Variant = "A"
    Const cCol2 As Variant = "B"
    Const cCol3 As Variant = "C"
    Const cCol4 As Variant = "D"
    Const cFirst1 As Long = 12
    Const cFirst2 As Long = 20
    'Const cLast1  As Long = 19
    'Const cLast2 As Long = 29
    Dim vntSheets As Variant
    Dim vnt1(2) As Variant
    Dim vnt2(2) As Variant
    Dim lastR As Long
    Dim i As Long
    Dim wsName As String
    Dim First1 As Long
    Dim First2 As Long

    vntSheets = Split(cSheets, ",")
    For i = 0 To UBound(vnt1)
        vnt1(i) = cFirst1 - 1
        vnt2(i) = cFirst2 - 1
    Next
    With ThisWorkbook.Worksheets(vntSheets(0))
        lastR = .Cells(.Rows.Count, cCol1).End(xlUp).Row
        For i = cFirstR To lastR
            If .Cells(i, cCol1) = cStr1 Then
                wsName = vntSheets(.Cells(i, cCol2) - 1)
                Select Case .Cells(i, cCol3)
                    Case cStr2
                        First1 = vnt1(.Cells(i, cCol2) - 2) + 1
                        vnt1(.Cells(i, cCol2) - 2) = First1
                        .Parent.Worksheets(wsName).Cells(First1, cCol3) _
                                = .Cells(i, cCol4)
                    Case cStr3
                        First2 = vnt2(.Cells(i, cCol2) - 2) + 1
                        vnt2(.Cells(i, cCol2) - 2) = First2
                        .Parent.Worksheets(wsName).Cells(First2, cCol3) _
                                = .Cells(i, cCol4)
                End Select
            End If
        Next
    End With
End Sub

最新更新