Forum Discussion

drdree73's avatar
drdree73
Brass Contributor
Apr 19, 2022
Solved

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)

 

 

 

  • drdree73 

     

    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.

     

  • drdree73 

     

    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.

     

    • drdree73's avatar
      drdree73
      Brass Contributor
      That did the trick! Thank you very much !
      • drdree73's avatar
        drdree73
        Brass Contributor

        Subodh_Tiwari_sktneer 

         

        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

         

Resources