简体   繁体   English

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

[英]Change a cell value in a sheet based on the value of another sheet cell value

I have two sheets in a workbook, "Sheet1" and "Sheet2".我在工作簿中有两张工作表,“Sheet1”和“Sheet2”。

I want sheet2 cell A1 value to be either string "Potato" or "Tomoato" based on the cell value of A1 or A2 in sheet1 with a condition.我希望 sheet2 单元格 A1 值是字符串“Potato”或“Tomoato”,基于 sheet1 中 A1 或 A2 的单元格值并带有条件。

eg例如

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

I already having code in sheet1.我已经在 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

Sorry, but if your target is:抱歉,但如果您的目标是:

if A1 (sheet1) >= 7 then A1(sheet2) = "Potato" Else A1(sheet2) = "Tomato"如果 A1 (sheet1) >= 7 那么 A1(sheet2) = "Potato" Else A1(sheet2) = "Tomato"

you don't need vba at all.你根本不需要vba。 You just simply put in A1(sheet2) cell directly the IF function in this way:您只需以这种方式直接将 IF 函数放入 A1(sheet2) 单元格中:

=IF(Sheet1!A1>=7;"Potato";"Tomato") =IF(Sheet1!A1>=7;"土豆";"番茄")

and that's it :)就是这样:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM