asdasda d
- User
asdasda d
- Assistant
It seems like you've entered a random string of characters. If you have any questions, feel free to ask, and I'll be happy to help.
- User
Write the code in excel VBA for the following example: I have 2 cells in excel A1 and B1. I wand these cells to automatically calculate the result as follows B1=A1*2 and A1=B1/2 whenewer I write a new value in them. When I type a new value in A1 or B1 I don't want to override the formula in that cell and I want to be able to use the undo function.
- Assistant
You can achieve this functionality using a Worksheet Event in Excel VBA. We will use the
Worksheet_Changeevent to monitor changes in cells A1 and B1.Follow the steps below:
- Open the Excel file and press
ALT + F11to open the Visual Basic for Applications (VBA) editor. - In the editor, double-click on the sheet where you want to implement this functionality (e.g., Sheet1).
- Copy and paste the following code inside the sheet's code window:
Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo ErrorHandler If Not Intersect(Target, Range("A1:B1")) Is Nothing Then Application.EnableEvents = False If Target.Address = "$A$1" Then Range("B1").Value = Target.Value * 2 ElseIf Target.Address = "$B$1" Then Range("A1").Value = Target.Value / 2 End If End If ErrorHandler: Application.EnableEvents = True End Sub- Close the VBA editor.
- Save your workbook as a macro-enabled file ().
- Open the Excel file and press