+ Reply to Thread
Results 1 to 6 of 6

Unload and placement wont work in user form code

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2011
    Location
    Brisbane
    MS-Off Ver
    Excel 2003
    Posts
    14

    Unload and placement wont work in user form code

    I have two problems that I cannot work out.

    I have two user forms - the first 'frmCalendar' is opened after a new sheet is added to my workbook and the user then has to click a date to move on.

    The click event works fine,and the date is entered into the sheet. However the form will not unload. (I can get it to hide, but once it has been used it is not required again in this process, therefore I want to unload it from memory)

    I don't know why it will not unload, but it stays on the screen until the 'unload' code of the next form is activated via a command button click event.


    Sub Calendar1_Click()
    ' Transfer date selected on calendar to active cell
    ' and close UserForm.
        ActiveCell.Value = Calendar1.Value
        ActiveCell.Offset(0, -2).Select
        
        Unload frmCalendar
        frmNewOrder.Show
        
        
    End Sub
    The second form which is opened from the click event of the calendar also has a problem in that I cannot get it to place at the precise point that I want, and I think it has something to do with the calendar, which also is placed precisely using the code below

    
    Private Sub UserForm_Initialize()
         
        
         With frmCalendar
            .Top = Application.Top + 125 '< change 125 to what u want
            .Left = Application.Left + 325 '< change 25 to what u want
        End With
        
           
    If IsDate(ActiveCell.Value) Then
            Calendar1.Value = DateValue(ActiveCell.Value)
        Else
            Calendar1.Value = Date
    End If
    
    End Sub
    and this is the NewOrder form code

    Option Explicit
    
    Private Sub UserForm_Initialize()
    
    With frmNewOrder
            .Top = Application.Top + 25 '< change 225 to what u want
            .Left = Application.Left + 325 '< change 25 to what u want
        End With
    
    'initialises the order form so that it can capture the previous order no on the
    'sheet to the left and then add one in the txtOrderNo box, and
    'finally pass the new order No to the sheet
    
    If ActiveSheet.Index > 6 Then
            txtOrderNo.Value = Worksheets(ActiveSheet.Index - 1).Range("C16") + "1"
     
     Else
     
            txtOrderNo.Value = "1"
     
    End If
    
    End Sub
    As can be seen the placement code is the same. It works for the calendar, but not for the new order form.

    Hope someone can help -- its got me beat.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Unload and placement wont work in user form code

    Instead of an extra userform I'd prefer a datepicker-control in the userform.

    Private Sub UserForm_Initialize()
       Top = 25 
       Left = 325
    
       txtOrderNo.Value = "1"
       if ActiveSheet.Index > 6 then txtOrderNo.Text=txtOrderNo.txt +sheets(ActiveSheet.Index - 1).Range("C16")
    End Sub



  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Unload and placement wont work in user form code

    FYI, re point 1 as soon as you show the other form, the code in the first form stops running and at that stage it has not finished the Calendar1_Click sub so the form cannot actually unload.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    03-20-2011
    Location
    Brisbane
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Unload and placement wont work in user form code

    snb

    when you say a 'datepicker' control on the main user form - that sounds good, but I am not understanding what you mean by a datepicker? can you elaborate please.

    Romperstomper -- if I place the unload code before the code to show the second form wouldn't that just stop the sub, so that the second form would not show?

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Unload and placement wont work in user form code

    No it won't. The form won't unload until the sub finishes, but that was not what I was suggesting.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Unload and placement wont work in user form code

    In the attachment a file containing a userform containing a datepickercontrol
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1