多个if语句,如果未满足,则删除单元格中已有的内容

时间:2017-12-05 06:46:43

标签: excel vba excel-vba

如果活动工作表是特定工作表,我试图让Excel复制一些值。我遇到的问题是,如果将一张表中的值转移到需要打开的表单上,它将覆盖其他if语句中提到的单元格,但我需要这些值不被覆盖。有人可以帮我解决这个问题吗?

If ActiveSheet = Sheets("Parts for site") Then
    Sheets("INTERNAL").Cells(35, 4) = Sheets("Parts for site").Cells(i + 3 - n, 17)
    Else
End If

If ActiveSheet = Sheets("Parts for options") Then
    Sheets("INTERNAL").Cells(52, 4) = Sheets("Parts for options").Cells(i + 3 - n, 17)
    Else
End If

If ActiveSheet = Sheets("Parts for renovation") Then
    Sheets("EXTERNAL").Cells(29, 4) = Sheets("Parts for renovation").Cells(i + 2 - n, 17)
    Else
End If

If ActiveSheet = Sheets("Parts for site") Then
    Sheets("EXTERNAL").Cells(35, 4) = Sheets("Parts for site").Cells(i + 2 - n, 17)
    Else
End If

If ActiveSheet = Sheets("Parts for options") Then
    Sheets("EXTERNAL").Cells(52, 4) = Sheets("Parts for options").Cells(i + 2 - n, 17)
    Else
End If

2 个答案:

答案 0 :(得分:0)

你可以简单地使用“&”结合两个价值观。

这是一个例子:

If ActiveSheet = Sheets("Parts for site") Then
    Sheets("INTERNAL").Cells(35, 4) = Sheets("INTERNAL").Cells(35, 4) & Sheets("Parts for site").Cells(i + 3 - n, 17)
    Else
End If

答案 1 :(得分:0)

您可以尝试以下方法:

Sub MySub()

Select Case ActiveSheet.Name

    Case "Parts for site"
        Sheets("INTERNAL").Cells(35, 4) = Sheets("INTERNAL").Cells(35, 4) & "||" & Sheets("Parts for site").Cells(i + 3 - n, 17)

    Case "Parts for options":
        Sheets("INTERNAL").Cells(52, 4) =  Sheets("INTERNAL").Cells(52, 4) & "||" & Sheets("Parts for options").Cells(i + 3 - n, 17)

    Case "Parts for renovation"
        Sheets("EXTERNAL").Cells(29, 4) = Sheets("EXTERNAL").Cells(29, 4) & "||" & Sheets("Parts for renovation").Cells(i + 2 - n, 17)

    Case "Parts for site"
        Sheets("EXTERNAL").Cells(35, 4) = Sheets("EXTERNAL").Cells(35, 4) &"||" & Sheets("Parts for site").Cells(i + 2 - n, 17)

    Case "Parts for options"
        Sheets("EXTERNAL").Cells(52, 4) = Sheets("EXTERNAL").Cells(52, 4) & "||" & Sheets("Parts for options").Cells(i + 2 - n, 17)

End If

End Sub

这是你想要的,这不会清除原始值,但也会有旧值。