Forum Discussion
HotBlue
Jul 30, 2020Copper Contributor
Multiple actions of an IF test
Is there a way to implement 2 actions based on the results of an IF test? For example: IF($G6=$K6,... for a true result I would like to implement both copying $G6 to $M6 and set $K6="". I have tr...
- Jul 30, 2020
This is not possible with a formula. A formula returns a value in the cell that contains the formula; it cannot perform other actions. What you want requires VBA:
- Right-click the sheet tab.
- Select 'View Code' from the context menu.
- Copy the code listed below into the worksheet module.
- Close the Visual Basic Editor.
- Save the workbook as a macro-enabled workbook (.xlsm).
- Make sure that you allow macros when you open the workbook.
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Range("G6,K6"), Target) Is Nothing Then If Range("G6").Value = Range("K6").Value Then Application.ScreenUpdating = False Application.EnableEvents = False Range("M6").Value = Range("G6").Value Range("K6").ClearContents Application.EnableEvents = True Application.ScreenUpdating = True End If End If End Sub
HansVogelaar
Jul 30, 2020MVP
This is not possible with a formula. A formula returns a value in the cell that contains the formula; it cannot perform other actions. What you want requires VBA:
- Right-click the sheet tab.
- Select 'View Code' from the context menu.
- Copy the code listed below into the worksheet module.
- Close the Visual Basic Editor.
- Save the workbook as a macro-enabled workbook (.xlsm).
- Make sure that you allow macros when you open the workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("G6,K6"), Target) Is Nothing Then
If Range("G6").Value = Range("K6").Value Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Range("M6").Value = Range("G6").Value
Range("K6").ClearContents
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End If
End Sub