Results 1 to 14 of 14

Thread: [RESOLVED] Userform initialize (sometimes works, sometimes don't!)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

    Resolved [RESOLVED] Userform initialize (sometimes works, sometimes don't!)

    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 UserForm_Initialize()
    Me.ComboBox1.List = Array("BLOCK", "TWIN", "TRIPLE", "PAIR")
    Me.OptionButton1.Value = True
    ComboBox1.ListIndex = 0
    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Is this a VB.NET question or VBA?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

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

    Hello,

    This is a VBA question, sorry if I posted on the wrong thread.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    I shall ask the mods to move this thread to the Office forum, which is for VBA and other Office development. Please don't double-post.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

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

    Thank you so much!

    I wish you a great weekend,
    Pepito

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

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

    Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

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

    try forcing the position of the userform to the second monitor, by setting the top or left as appriate
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

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

    Name:  When the userform don't work.jpg
Views: 3047
Size:  29.1 KBName:  When the userform works.jpg
Views: 2869
Size:  15.9 KB

    Hello,

    please find attached two captures from the excel workbook. In the first picture the userform is opening in monitor #1. When the userform is opening in this monitor, after I introduce the information in textbox 1 and press tab to move on the next textbox it's adding space into the textbox.
    I believe it's something wrong into the activation code because if I double click into the worksheet and then on the userform then the tab order is working and I can move on every textbox.

    Private Sub Workbook_Open()
    UserForm1.Show
    End Sub

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


    In the second capture is when the userform works perfectly, the situation when the userform automatically appears on the second monitor.

    This situation is going me crazy because sometimes it's working sometimes not.

    Kind regards,
    Pepito

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

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

    you could try using application.ontime to see if it helps, sometimes code run during workbook_open can have problems, trying to process while the workbook is opening

    Code:
    Private Sub Workbook_Open()
      Application.OnTime Now + TimeSerial(0, 0, 1), "openuserform"
    End Sub
    in a module
    Code:
    Public sub openuserform()
      UserForm1.Show
    end sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

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

    Thank you for this alternative but the problems remains exactly the same. If I open the workbook sometimes it appears on the second monitor and works perfectly, if I reopen the workbook template and the userform appear on the first monitor I have the same errors.
    Name:  Capture12121.jpg
Views: 2802
Size:  29.8 KB

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

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

    did you try setting the position of the userform by using it properties?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

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

    Name:  USERFORM.JPG
Views: 2738
Size:  65.7 KB

    This is what I have set on the userform.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

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

    try moving the code from initialize to activate, if the form may be activated multiple times, use a static boolean
    like
    Code:
    Private Sub UserForm_Activate()
    Static activated As Boolean
    If Not activated Then
    'do stuff here only the first time
    activated = True
    End If
    ' do other stuff here every time
    End Sub
    this probably is not required, if the userform is modal, but won't hurt anything
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    21

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

    I think my adrenaline has grow ))

    Thank you so much!
    Pepito

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width