Search code examples
excelexcel-2013vba

(VBA-EXCEL) Automatically change and increment the value of an activex textbox based from a cell value


I have an activex textbox in worksheet1 that needs to automatically change based on the cell value which is in worksheet2 (where my table is).
This value should be incremented, like the concept of ID Number which increments if you add a customer,the incremented value must be displayed at the textbox ready to be saved.

Does anyone have any constructive suggestion concerning the above?


Solution

  • Kindly paste the below code in worksheets(2) of your workbook. Also txtVal is the name of textbox on Worksheets(1)

    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False
        'Assuming A1 is the cell in worksheet 2 and it has numeric value as it should be incremented.
    
        If Target.Count = 1 And Target.Address = "$A$1" Then
            If IsNumeric(Target.Value) Then Sheet1.txtVal.Text = CInt(Target.Value) + 1
        End If
    
        Application.EnableEvents = True
    End Sub
    

    enter image description here enter image description here