Userform initialize (sometimes works, sometimes don't!)

Pepito_child

New Member
Joined
Feb 13, 2016
Messages
49
Hello all,

I have a workbook saved as Excel Macro-Enabled Template and sometimes when I open the file, after the userform is initialize, the rules for the textbox aren't available (the vba tab order and the limitation in writing only numbers).
If I press tab is adding space in the cell, also if I press any button from the keyboard is added to the cell.
Most of the times I don't have any problems when the userform appears to be edited, but if the userform is having problems, my solution is to double click in the worksheet, then on the userform and after that it's working like there are no problems.
Could there be a problem in my initialization code, does the userform interferes with other excel files and needs some restrictions?
I'm using two monitors and when the userform works perfectly is when it appears on the second screen, if it appears in the first monitor, there are also the errors.

Private Sub Workbook_Open()
UserForm1.Show
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox1.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

This are the settings for the text box object:
TabIndex - 1
TabKeyBehavior - False
TabStop - True
WordWrap - False

It's very frustrating because it's look like if I'm lucky, I can open the workbook with no errors.
Is there something that can be done for this userform to work fine?

Kind regards,
Pepito
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hard to tell if there is a problem in the Initialize code without seeing it.:)
 
Upvote 0
This are the codes from the userform:

Private Sub UserForm_Terminate()
ThisWorkbook.Close
End Sub

Private Sub ComboBox1_Change()
Range("A3").Value = Me.ComboBox1.ListIndex + 1
End Sub

Private Sub CommandButton1_Click()
If TextBox1.Value = "" Then
MsgBox "Please add T dimension!"
End If
If TextBox2.Value = "" Then
MsgBox "Please add A dimension!"
End If
If TextBox3.Value = "" Then
MsgBox "Please add R dimension!"
End If
If TextBox4.Value = "" Then
MsgBox "Please add L dimension!"
End If
If TextBox5.Value = "" Then
MsgBox "Please add l dimension!"
End If
If TextBox6.Value = "" Then
MsgBox "Please add H dimension!"
End If
If Range("b38").Value = "" Then Exit Sub
Call DeleteShapes
Call draw1
Call draw2
Call draw3
Call draw4
Call ChangeShapeColor
End Sub

Private Sub CommandButton4_Click()
Dim z As Control
For Each z In UserForm1.Controls
If TypeName(z) = "TextBox" Then
z.Value = ""
ComboBox1.ListIndex = 0
Me.OptionButton1.Value = True
End If
Next z
End Sub

Private Sub Userform_Activate()
AddToForm MIN_BOX
End Sub

Private Sub UserForm_Initialize()
Me.ComboBox1.List = Array("BLOCK", "TWIN", "TRIPLE", "PAIR")
Me.OptionButton1.Value = True
ComboBox1.ListIndex = 0
End Sub

Private Sub CommandButton3_Click()
Call ExportImage
End Sub

Private Sub Label1_Click()
End Sub

Private Sub Label8_Click()
End Sub

Private Sub CommandButton2_Click()
Call MsgBoxValue
End Sub

Private Sub OptionButton1_Click()
Range("B3").Value = 1
End Sub

Private Sub OptionButton2_Click()
Range("B3").Value = 2
End Sub

Private Sub TextBox1_Change()
Sheet1.Range("C3") = TextBox1.Value
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox1.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox2.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox3.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox4.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox5_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox5.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TextBox6.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox2_Change()
Sheet1.Range("D3") = TextBox2.Value
End Sub

Private Sub TextBox3_Change()
Sheet1.Range("E3") = TextBox3.Value
End Sub

Private Sub TextBox4_Change()
Sheet1.Range("G3") = TextBox4.Value
End Sub

Private Sub TextBox5_Change()
Sheet1.Range("F3") = TextBox5.Value
End Sub

Private Sub TextBox6_Change()
Sheet1.Range("H3") = TextBox6.Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,806
Members
449,337
Latest member
BBV123

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