Forum Discussion
drdree73
Apr 19, 2022Brass Contributor
action based on cell selection
Hi, is there a method to have a cell execute based on manual selected cell?
See below example: (mouse) selected cell is A2, giving 8 in cell C1--> (A2+5)
Now, if i (mouse) select cell A3, cell C1 should update to 11 --> (A3+5)
To achieve that, place the following codes on Sheet Module and to do that follow these steps...
1. Right click on Sheet Tab Name and choose View Code.
2. Paste the codes given below into the opened code window.
3. Save your file as Macro-Enabled Workbook.
Sheet Module Codes:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub Dim NumToSum As Long If Target.Column = 1 And Target.Row > 1 Then Range("C1").ClearContents If Target <> "" And IsNumeric(Target.Value) Then NumToSum = getNumber(Range("B1").Value) If NumToSum <> 0 Then Range("C1").Value = NumToSum + Target.Value End If End If End If End Sub Function getNumber(ByVal str As String) As Long Dim Matches As Object With CreateObject("VBScript.RegExp") .Global = False .Pattern = "\d+" If .test(str) Then Set Matches = .Execute(str) getNumber = Matches(0) End If End With End Function
Please find the attached with codes in place.
- Subodh_Tiwari_sktneerSilver Contributor
To achieve that, place the following codes on Sheet Module and to do that follow these steps...
1. Right click on Sheet Tab Name and choose View Code.
2. Paste the codes given below into the opened code window.
3. Save your file as Macro-Enabled Workbook.
Sheet Module Codes:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub Dim NumToSum As Long If Target.Column = 1 And Target.Row > 1 Then Range("C1").ClearContents If Target <> "" And IsNumeric(Target.Value) Then NumToSum = getNumber(Range("B1").Value) If NumToSum <> 0 Then Range("C1").Value = NumToSum + Target.Value End If End If End If End Sub Function getNumber(ByVal str As String) As Long Dim Matches As Object With CreateObject("VBScript.RegExp") .Global = False .Pattern = "\d+" If .test(str) Then Set Matches = .Execute(str) getNumber = Matches(0) End If End With End Function
Please find the attached with codes in place.