All-
I have a userform called frmParameters that allows users of an Excel worksheet to enter three parameters that Excel uses in calculations within the worksheet. Currently, another procedure successfully is saving these parameters into cell P2 on the active worksheet. An example of the saved string:
Parameters:txtSRWraps:4.33;txtSSWraps:3.33;txtDCCWraps:2.33;
The procedure below executes when the userform is opened--if P2 does not contain parameters already, it will set them to a default (this hasn't been written yet)...but...if Parameters are in P2, I want to loop though the string and read the parameters that were saved into the textboxes.
Here's the trick...I want to allow the userform to be easily expanded in the future, so when the userform WRITES to cell P2, it saves the textbox name (Like "txtSRWraps" in the above example), then ":" to seperate, then the textbox value (like "4.33" in the above example), then ";" to seperate, and the next textbox name. This is written in the other procedure to cycle through ALL the textboxes on the form so all are saved (even if new ones are added, without having to add more code).
Now, I want to read the values in from "P2", and the textbox names and values are specified within the string. I get a Runtime Error '13': Type Mismatch when the code hits the "Set ObjTB".... Help! How do I populate the textboxes?
-Tim
I have a userform called frmParameters that allows users of an Excel worksheet to enter three parameters that Excel uses in calculations within the worksheet. Currently, another procedure successfully is saving these parameters into cell P2 on the active worksheet. An example of the saved string:
Parameters:txtSRWraps:4.33;txtSSWraps:3.33;txtDCCWraps:2.33;
The procedure below executes when the userform is opened--if P2 does not contain parameters already, it will set them to a default (this hasn't been written yet)...but...if Parameters are in P2, I want to loop though the string and read the parameters that were saved into the textboxes.
Here's the trick...I want to allow the userform to be easily expanded in the future, so when the userform WRITES to cell P2, it saves the textbox name (Like "txtSRWraps" in the above example), then ":" to seperate, then the textbox value (like "4.33" in the above example), then ";" to seperate, and the next textbox name. This is written in the other procedure to cycle through ALL the textboxes on the form so all are saved (even if new ones are added, without having to add more code).
Now, I want to read the values in from "P2", and the textbox names and values are specified within the string. I get a Runtime Error '13': Type Mismatch when the code hits the "Set ObjTB".... Help! How do I populate the textboxes?
Code:
Private Sub UserForm_Initialize()
'Copy any existing Parameters to the Form as Defaults
Dim ParametersDoNotExist As Boolean
ParametersDoNotExist = InStr(ActiveSheet.Range("P2").Value, "Parameters") = 0 Or IsNull(InStr(ActiveSheet.Range("P2").Value, "Parameters"))
If ParametersDoNotExist Then
MsgBox ("Need to write procedure to use default parameters.")
' Call SetParametersToDefaults 'Need to write this Procedure still.
Else
MsgBox ("Existing parameters Found.")
'----------Get Parameters from cell where they are stored.----------
Dim objTB As TextBox
Dim StartPosition As Double
Dim EndPosition As Double
Dim TransferParameter As String
Dim TransferParamValue As String
Dim SearchString As String
Dim EndOfString As Boolean
'Initialize variables
TransferParameter = ""
TransferParamValue = ""
SearchString = ""
StartPosition = 1
EndPosition = 1
SearchString = ActiveSheet.Range("P2").Value
Do Until EndOfString = True
StartPosition = InStr(StartPosition, SearchString, "txt") + Len(TransferParameter)
EndPosition = InStr(StartPosition, SearchString, ":")
TransferParameter = Mid(SearchString, StartPosition, (EndPosition - StartPosition))
StartPosition = EndPosition ' Reset Start of Search to the end of the Parameter Value
EndPosition = InStr(StartPosition, SearchString, ";")
TransferParamValue = Mid(SearchString, StartPosition + 1, (EndPosition - (StartPosition + 1)))
Stop
MsgBox ("Found Parameter: " & TransferParameter & " " & TransferParamValue)
Set objTB = Me.Controls(TransferParameter)
'objTB.Text = TransferParamValue
MsgBox ("Found Parameter: " & TransferParameter & " " & TransferParamValue)
EndOfString = False 'Will need a way to determine if we are at the end of the string and end the loop if we are.
Loop
End If
'Start user at the top
Me.txtSRWraps.SetFocus
End Sub
-Tim