calling a userform and reverting back

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
I have a section of code below from a much larger piece. The section of code worked when using an input box to acquire a userinput. However, I'd like to have userform replace the inputbox. I've tried just using a userform and inserting the line (as seen below) but that didn't work. I need the rest of the code below to call to the userform, allow the user to input data into textbox1, and then return to the lines of code being run.

thanks for the help

Code:
dim s as string

If s = "" then
'this works
s = inputbox("Please input here",vbokonly)

'I wish this worked instead
call userform17
s = userform17.textbox1.value
unload userform17

'rest of this section of working code
If s <> "" then 
blah blah blah
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This should also be:
Code:
Public Function cInputBox() As String
With New UserForm17
    .Show
    cInputBox = .TextBox1.Value
End With
End Function

Note the .Show.

If it's still not working, post the code from your user form
 
Last edited:
Upvote 0
sorry things got really busy. Anyway, here's the userform code. and I had it commented out just so the current code would work.

Code:
  Private Sub CommandButton1_Click()Me.Hide
Sheets("Notes").Visible = True
Sheets("Notes").Select
Application.Visible = True
End Sub




Private Sub TextBox1_Change()
Sheets("Developer").Range("B34").Value = TextBox1.Value
cInputBox = TextBox1.Value


End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = 1
    Me.Hide
End Sub
 
Upvote 0
My code is very explicit, please read it properly. Have you downloaded the workbook?
 
Upvote 0
Ok. downloaded the workbook. Seems like there's a lot more going on there. Further, doesn't seem that the workbook actually "sets" the input to go anywhere....just the msgbox that pops up with the password. interesting idea
 
Upvote 0
The principle is identical to my first post in this thread. I don't really know what this means:
Further, doesn't seem that the workbook actually "sets" the input to go anywhere

It sets it to the value property of the userform when the OK button is pressed, this is then read by the calling function and returned to the sub
 
Upvote 0
also, here's the coding I went with- so maybe I missed something?

Userform17 itself
Code:
Private Sub CommandButton1_Click()Me.Hide
Application.Visible = True
End Sub
Public Property Let Message(ByVal msg As String)
    Me.Labell.Caption = msg
End Property
Public Property Get Value() As String
    Value = Me.TextBox1.Value
End Property




Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = 1
    Me.Hide
End Sub


Within the thisWorkbook piece
Code:
Public Function cInputBox() As String
With New UserForm17
    .Message = Message
    .Show
    cInputBox = .Value
End With
End Function
AND
Code:
Private Sub Workbook_Open()


'This will show the Userform 1 when starting and hide the excel sheet in the background
'Also checks to see if program has ever been run, and if not, loads program for the first time
Application.Visible = False
Dim s As String
Dim edate As String
Dim namer As String
Dim d As String
Dim ExpirationDate As String


ExpirationDate = edate
d = Sheets("Developer").Range("B39")          'registration key
edate = Sheets("Developer").Range("E37")      'expiration date
namer = Sheets("Notes").Range("N4")           'just a name


s = GetSetting("DemoTest", "Registration", "Username")
If s = "" Then
        Sheets("Developer").Unprotect Password:=Worksheets("Developer").Range("B15:E15").Value
        Sheets("Developer").Range("B34:F34").ClearContents
        's = InputBox("Welcome to the " & namer & " Voyage Reporting System." & vbCrLf & "Please input the appropriate name to initialize the system for the first time." & vbCrLf & vbCrLf & "Note: this information can be modified later by clicking on the [Developer] button.", namer, "Bridge")
        'UserForm17.Show
        s = cInputBox()
        MsgBox s
        
        If s <> "" Then
            's = cInputBox()
            Sheets("Developer").Range("B34") = s
            SaveSetting "DemoTest", "Registration", "Username", s
            Sheets("Notes").Visible = xlSheetVisible
            Sheets("Notes").Select
            Sheets("Developer").Unprotect Password:=Worksheets("Developer").Range("B15:E15").Value
            Sheets("Developer").Range("C36") = Date
            'If s <> "" Then MsgBox "Welcome to the " & name & " Voyage Reporting System." & vbCrLf & "Please input the appropriate data to initialize the system for the first time." & vbCrLf & vbCrLf & "Note: this information can be modified later by clicking on the [Developer] button.", vbOKOnly, name
            Application.Visible = True
        End If


Else:
    'If ExpirationDate > edate Then
        If ExpirationDate < Date Then
            If ActiveWorkbook.name = "Master Voyage Report.xlsm" Then
                UserForm1.Show
            ElseIf ActiveWorkbook.name = "Current Voyage Report.xlsm" Then
                UserForm2.Show
            Else: UserForm3.Show
            End If
        Else: d = Application.InputBox("Your workbook date has expired. Please enter the registration key to renew your license.", namer)
            If d = CStr(Worksheets("Developer").Range("B39").Value) Then
                    Sheets("Developer").Range("C36") = Date
                    MsgBox "Welcome Back " & s, vbOKOnly, namer
            End If
        End If
    'End If
        
        
End If
Application.Visible = True   'inserted just to check workbook
'Protects/Hides sheets on startup
Dim sh As Worksheet
    For Each sh In ActiveWorkbook.Worksheets
        If sh.name = "Notes" Then
            sh.Protect Password:=Worksheets("Developer").Range("B17:E17").Value, UserInterfaceOnly:=True
            End If
        If sh.name = "Ports" Then
            sh.Protect Password:=Worksheets("Developer").Range("B19:E19").Value, UserInterfaceOnly:=True
            End If
        If sh.name = "Developer" Then
            sh.Protect Password:=Worksheets("Developer").Range("B15:E15").Value, UserInterfaceOnly:=True
            End If
    Next sh
    
End Sub
 
Upvote 0
You can simplify to the below:

Userform17
Code:
Private Sub CommandButton1_Click()
    Me.Hide
    Application.Visible = True
End Sub

Public Property Get Value() As String
    Value = Me.TextBox1.Value
End Property




Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = 1
    Me.Hide
End Sub

Calling function:
Code:
Public Function cInputBox() As String
    With New UserForm17
        .Show
        cInputBox = .Value
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,096
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top