根据另一个工作表单元格值更改工作表中的单元格值

时间:2020-03-27 12:49:19

标签: excel vba

我在工作簿中有两张纸,“ Sheet1”和“ Sheet2”。

我希望基于有条件的sheet1中A1或A2的单元格值将sheet2单元格A1的值设为字符串“土豆”或“番茄”。

例如

if A1 (sheet1) >= 7 or A2 (sheet1) >= 7 then
    A1(sheet2) = "Potato"
Else 
    A1(sheet2) = "Tomato"

我已经在sheet1中有代码了。

Dim xVal, yVal As String

Private Sub Worksheet_change(ByVal Target As Range)
    Static xCount As Integer
    Static yCount As Integer
    Application.EnableEvents = False
    If Target.Address = Range("C28").Address Then
        Worksheets("sheet2").Range("T3").Offset(xCount, 0).Value = xVal
        xCount = xCount + 1
    Else
        If xVal <> Range("C28").Value Then
         Worksheets("sheet2").Range("T3").Offset(xCount, 0).Value = xVal
        xCount = xCount + 1
        End If
    End If

    If Target.Address = Range("C24").Address Then
        Worksheets("sheet1").Range("U3").Offset(yCount, 0).Value = yVal
        yCount = yCount + 1
    Else
        If yVal <> Worksheets("Main").Range("C24").Value Then
            Worksheets("sheet1").Range("U3").Offset(yCount, 0).Value = yVal
            yCount = yCount + 1
        End If
    End If
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    xVal = Range("C28").Value
    yVal = Range("C24").Value
End Sub

1 个答案:

答案 0 :(得分:1)

对不起,但是如果您的目标是:

如果A1(sheet1)> = 7,则A1(sheet2)=“土豆”其他A1(sheet2)=“西红柿”

您根本不需要vba。 您只需以这种方式直接将IF函数直接放入A1(sheet2)单元中即可:

= IF(Sheet1!A1> = 7;“土豆”;“西红柿”)

就这样:)

相关问题