如何更新excel中的输入单元格

时间:2012-05-11 02:25:21

标签: excel

好的..我试图整天解决这个问题,现在我正在寻求帮助

我有什么:在excel中,我在单元格A1中输入值101。在它旁边,在单元格B1中,我有一个下拉列表,显示A1的单位。现在是Pa。所以我有(101 | Pa)

我想要什么:我将B1下拉列表更改为atm,我希望它自动将单元格A1中的值更新为1 |大气压。

我有一个解决方法,需要用户输入另一个单元格,然后根据B1更新,但这不是那么干净和专业。

我希望你能理解我的问题。

您表示赞赏。

由于

1 个答案:

答案 0 :(得分:1)

你需要一些VBA。

使用Change事件检测单位何时更改,更新原始值

将其放入Workbook模块中:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim OldUnit As Variant
    Dim NewUnit As Variant
    Dim ConversionFactor As Double
    If Target.Cells.Count = 1 And Target.Column = 2 Then
        ' Save new units
        NewUnit = Target
        ' prevent recalling this code when we update cells
        Application.EnableEvents = False
        ' Get old units
        Application.Undo
        OldUnit = Target
        ' put new units back on sheet
        Target = NewUnit
        ' work out your conversion factor based on old and new units
        ConversionFactor = 0.5 ' for testing
        ' update value
        Target.Offset(0, -1) = Target.Offset(0, -1) * ConversionFactor

        Application.EnableEvents = True
    End If
End Sub