+ Reply to Thread
Results 1 to 7 of 7

open another workbook - close - see again original form

Hybrid View

  1. #1
    Registered User
    Join Date
    11-18-2012
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    open another workbook - close - see again original form

    Hi all,

    A question please.

    I have a master workbook. On a form is a commandbutton that opens a second file.
    When the second workbook opens, my form disappears (automatically), the form from the second file appears.
    I have placed this event in workbook.open.

    When I close the second file, I see again the first workbook, but with no form.
    I need the orginally form to be shown.

    Can someone help please?



    Private Sub Commandbuttonfile_Click()
    
    Dim bn As String
        bn = Application.GetOpenFilename("Excel-files,*.xls*", _
            1, "Select One File To Open", , False)
        If TypeName(bn) = "Boolean" Then Exit Sub
    
    TextBox2 = bn
    
    Dim BackSlash As Integer, Point As Integer
    Dim FilePath As String, FileName As String
    Dim i As Integer
      
    FilePath = bn
    For i = Len(FilePath) To 1 Step -1
    If Mid$(FilePath, i, 1) = "." Then
         Point = i
         Exit For
        End If
      Next i
      If Point = 0 Then Point = Len(FilePath) + 1
      For i = Point - 1 To 1 Step -1
        If Mid$(FilePath, i, 1) = "\" Then
          BackSlash = i
          Exit For
        End If
      Next i
      FileName = Mid$(FilePath, BackSlash + 1, Point - BackSlash - 1)
      
    TextBox3 = FileName
    
    end sub
    
    
    Private Sub Commandbuttonfileopen_Click()
    
    TextBox1 = ActiveWorkbook.Name
    
    Dim strb1 As String
    Dim strb2 As String
    
    strb1 = TextBox1
    strb2 = TextBox3
    Unload frmstart
    
        Workbooks.Open strb2
    
    end sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: open another workbook - close - see again original form

    Hi, MJR,

    maybe you can use the Private Sub Workbook_Activate-event in ThisWorkbook for that like
    Private Sub Workbook_Activate()
    frmstart.Show
    End Sub
    As that event would alos fire when you just have switched to another workbook you may consider using a boolean variable to decide whether to show the form or loop through the forms to see if itīs open.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    11-18-2012
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: open another workbook - close - see again original form

    Hi Holger,

    Thank you for your respons!
    I placed your line in the workbook activate event.
    No result.

    After closing the second file there is no form appearing from the first.

    Any other solution?
    Mario

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: open another workbook - close - see again original form

    Hi, Mario,

    you placed the code in the workbook where the userform is located? The sample code works fine for me - please see a workbook attached (UserForm Button will create a new workbook, if you close that the form will be brought back up again).

    Ciao,
    Holger
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-18-2012
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: open another workbook - close - see again original form

    Hi Holger,

    Indeed your file works very fine.
    Thank you for all your attention and work!

    I stille have the problem in my case.
    I have added two files (simplefied).

    What am I doing wrong?

    Thanks in advance.
    Mario
    Attached Files Attached Files

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: open another workbook - close - see again original form

    Hi, Mario,

    I didnīt expect that the other workbook had a form as well and should be closed via that form. In that case the event which is triggered if a workbook is closed "as usual/normal" doesnīt get fired. A workaround for that is to call the macro to show the first form again before the workbook is closed:

    Private Sub cmbquit_Click()
    Unload aanvang
    ActiveWorkbook.Save
    If Application.Workbooks.Count = 1 Then 'quit Excel if only one workbook open
        Application.Quit
    Else
        Application.Run "test1.xlsm!start"
        ActiveWorkbook.Close
    End If
    End Sub
    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    11-18-2012
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: open another workbook - close - see again original form

    Hi Holger,

    Thank you for your patience and efforts!
    And for your solution!
    This works fine. I can surely work with this.


    Thanks again.

    Ciao,
    Mario,

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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