简体   繁体   中英

How to update an input cell in excel

Ok.. I have tried to solve this problem all day and now I am asking for help

What I Have: In excel, I input a value of 101 in cell A1. Next to it, in cell B1, I have a drop down list that shows the units of A1. Now it is Pa. So I have (101 | Pa)

What I want : I change B1 drop down list to atm, and I want it to automatically update the value in cell A1 to 1 | atm.

I have a workaround that requires another cell for the user to input, and then update according to B1 but this is not so clean and professional.

I hope you can understand my question.

You input is appreciated.

Thanks

You will need some VBA for this.

Using a Change event to detect when the units are changed, update original values

Put this in the Workbook module:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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