Results 1 to 3 of 3

Thread: Relationship from 2 sheets with one form

  1. #1

    Thread Starter
    Addicted Member sergeos's Avatar
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    162

    Question Relationship from 2 sheets with one form

    how best to organize the relationship there are 2 sheets with some functions and there is one form with a button.
    from sheet1 I call the form, and there I click the button. the event of this button should call the code from sheet1.
    the code from the second sheet2 also calls this form, however, clicking on the button should already call some procedure placed in the sheet2 code.

    Name:  Screenshot 2023-08-15 204814.jpg
Views: 444
Size:  25.6 KB

    the form does not run at the same time. only one caller sheet.
    Ten Years After - 01 You Give Me Loving

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,349

    Re: Relationship from 2 sheets with one form

    That is quite confusing. Are you wanting to have one form with one button that does different things depending on which sheet initiated the display of the form? Please supply more details on exactly what you are wanting to accomplish.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,143

    Re: Relationship from 2 sheets with one form

    Should be easy with a "ParentSheet"-Property of the Form.
    Then it's an easy Select Case/If then Else


    In Sheet1
    Code:
    Public Sub S1()
        Debug.Print "Hello from S1 in Sheet1"
    End Sub
    
    Sub CallForm()
        Load MyForm
        Set MyForm.ParentSheet = Me
        MyForm.Show
    End Sub
    In Sheet2
    Code:
    Public Function F2()
        Debug.Print "Hello from F2 in Sheet2"
    End Function
    
    Sub CallForm()
        Load MyForm
        Set MyForm.ParentSheet = Me
        MyForm.Show
    End Sub
    In UserForm
    Code:
    Public ParentSheet As Worksheet
    
    Private Sub CommandButton1_Click()
        If ParentSheet Is Sheets(1) Then Sheets(1).S1
        If ParentSheet Is Sheets(2) Then Sheets(2).F2
    End Sub
    Last edited by Zvoni; Aug 25th, 2023 at 05:58 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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