Home > other >  enable CommandButton if length text is select a value like 10 characters
enable CommandButton if length text is select a value like 10 characters

Time:03-30

I have been searching, but didn't find anything about that ,I am trying to figure out if there is a way to autosave the input data from a textbox to the spreadsheet without the need of click a save button or keyboard .

hint: its just an autosave program I don't want a lot of operations by the data entry

I want some command line like : if length in textbox1 = 10 then start or activate CommandButton1 else dont do anything ?

Provided that after carrying out operations in CommandButton1 pointer which in this case is keyboard back to textbox waiting for another entry

Private Sub CommandButton1_Click()
Dim lastrow As Long
lastrow = WorksheetFunction.CountA(Sheets("data").Range("A:A"))

Sheets("data").Cells(lastrow   1, 1).Value = lastrow
Sheets("data").Cells(lastrow   1, 2).Value = UserForm1.TextBox2.Value
Sheets("data").Cells(lastrow   1, 4).Value = UserForm1.TextBox1.Value
Sheets("data").Cells(lastrow   1, 5).Value = Now()

Private Sub CommandButton2_Click()

UserForm1.TextBox2.Value = ""UserForm1.ComboBox1.Value = ""

CodePudding user response:

Add this to your form:

Private Sub TextBox1_Change()
    If Len(TextBox1.Value) = 10 Then
        CommandButton1_Click
    End If
End Sub

This code will execute each time something causes the value of textbox1 to change, such as typing into it. As soon as the value in textbox1 has 10 characters, the code behind commandButton1's click event will be executed.

Warning, if someone pasted 11 characters into textbox1, and then keeps typing, commandbutton1 code won't execute

  • Related