Forum Discussion

kimmie2430's avatar
kimmie2430
Copper Contributor
Oct 27, 2022
Solved

convert excel formula to vba code

Hi,   appreciate if anyone could help me to convert the formula below to vba code:   =IF(ISBLANK(I40),"",IF(I40<1,K40,K40+I40-1))   If cell I40 is blank, then M40 is empty, if cell I40 is <1, t...
  • OliverScheurich's avatar
    OliverScheurich
    Oct 28, 2022

    kimmie2430 

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("I:I"), Target) Is Nothing Then
            
            If Target.Count > 1 Then
                Exit Sub
            End If
            ' clean the value
            Debug.Print Target.Address
            
            If Trim(Target.Value) <> "" Then
            
                If Range("I" & Target.Row).Value < 1 Then
                    Range("M" & Target.Row).Value = Range("K" & Target.Row).Value
                Else
                    Range("M" & Target.Row).Value = ((Range("K" & Target.Row).Value) + (Range("I" & Target.Row).Value) - 1)
                End If
            Else
            Range("M" & Target.Row).Value = ""
                        
            End If
        End If
    End Sub

    This code works in my sheet.

Resources