简体   繁体   中英

Excel: Edit Column A if Column B equals value

I have two columns in my excel file. Column A is numeric values and Column B is units (kg/g). I need to convert all values to g (like in column c). Here's an example:

A        B     C
0.75     kg    750.00
0.80     kg    800.00
700.00   g     700.00
500.00   g     500.00

I was looking at VBA scripts but since I haven't used them before I'm not able to edit them to my needs at all. Any ideas?

If you didn't want to use VBA (and you shouldn't need to, really), the formula above from @tom_preston is almost correct.

In "C2", you can use the formula =IF(B2="kg",A2*1000,A1) and just copy it down the line.

If you want it written in VBA as the question above asked here is the code that would be placed in a worksheet module.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cellChanged As Range, c As Range

Const myVal As String = "kg"

Set cellChanged = Intersect(Target, Columns("B"))

     If Not cellChanged Is Nothing Then
        For Each c In cellChanged
             If UCase( c.Value) = UCase(myVal) Then
                c.Offset(, 1).Value = c.Offset(, -1).Value / 1000
             else
                c.Offset(, 1).Value = vbnullstring
            End if
        Next
      End if
End Sub

How that helps have a great day!

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