Forum Discussion
action based on cell selection
- Apr 19, 2022
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 FunctionPlease find the attached with codes in place.
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 FunctionPlease find the attached with codes in place.
- drdree73Apr 19, 2022Brass ContributorThat did the trick! Thank you very much !
- drdree73Apr 19, 2022Brass Contributor
One follow up question. Can the same functionally be achieved for the Stockhistory function?
So, Stock History (Cell C1) list is shown/regenerated based on mouse selected Stock in column A.
Thank you
- Subodh_Tiwari_sktneerApr 19, 2022Silver Contributor
You may simply try this...
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub If Target.Column = 1 Then Range("C1").ClearContents Range("C1").Formula2Local = "=STOCKHISTORY(" & Target.Address & ",F1,F2)" End If End Sub