Search code examples
excelvbauserform

Making vba userform with inputs and command button


I made a macro (commandbutton), which is working (probably not the best solution, but it's working):

Private Sub CommandButton1_Click()
 Dim myval As Variant
 Dim ws As Worksheet
  myval = InputBox("Zadaj číslo riadku:")
  Set ws = ActiveSheet
If StrPtr(result) = 0 Then
 ElseIf myval = vbNullString Then
Else
  ws.Range("F3").Value = myval
End If
 ws.PrintOut From:=1, To:=1
End Sub

Now I want to make a USERFORM. I made this:

screen

But now I'm stuck:

  1. I don't know how to make an input from textbox. (there would be 3 inputs on Page1)

  2. Problem is the loop. I want to make a loop - input2 to input3. So for example: Input2.value = 1, Input3.value = 60, then I want a loop from 1 to 60.


Solution

  • A Textbox contains Text but a loop expect numbers. Try something like this:

    Dim lngText1 as Long
    Dim lngText2 as Long
    Dim i as Long
    If IsNumeric(Me.Controls("textbox2").Value) And isNumeric(Me.Controls("textbox3").Value) Then
      lngText1 = CLng(Me.Controls("textbox2").Value)
      lngText2 = CLng(Me.Controls("textbox3").Value)
    
      For i = lngText1 To lngText2
        'Your code
      Next i
    Else
      MsgBox "Please insert numbers"
    End If