Consulting

Results 1 to 6 of 6

Thread: Const for userform name?

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Const for userform name?

    is it possible to set a userform name as a constant so i can make my code as reusable as possible?

    I ll give an example in the only way i know how to try and explain what im after, hope this makes sense


    Dim formename As String
    formname = this userform
    <----------- need to be able to call this from all other procedures within the userform

    for example

    Private Sub btnFirst_Click()
    DoFill Formname  '(i.e.UserForm1) that will then run the code in the standard module 
    End Sub


    'Code in Standard Module

    Sub DoFill(UF)
    On Error GoTo Err
        UF.TextBox1.value = ActiveCell.value
        UF.TextBox2.value = ActiveCell.Offset(0, 1).value
        UF.TextBox3.value = ActiveCell.Offset(0, 2).value
      Exit Sub
    Err:
     Exit Sub

    Any Ideas?

    Thanks

    Gibbo

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Gibbo,

    Individual instances of Userforms do not actually have names.

    What you can do is to set a reference to the Userform Object.

    Dim myForm As UserForm1
    Set myForm = New UserForm1
    :
    :
    DoFill myForm
    :
    :
    
    
    Sub DoFill(UF As UserForm1)
        UF.Textbox1 etc., etc.
    :
    :
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    thanks for the reply but i dont think that does what im after,

    My problem is I have identical code in several userforms, where i just need to change one line per procedure to reference that userform (i.e. DoFill userform1, DoFill userform2, DoFill userform3) and so on to call the procedure in the standard module.

    Was just looking for a piece of code to avoid me having to make that minor change each time by using a variable or constant but sounds like thats not possible

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    In Form module

    Private Sub CommandButton1_Click()
    DoFill Me
    End Sub

    In Standard module

    Sub DoFill(UF As UserForm) 
        On Error GoTo Err
        UF.TextBox1.Value = ActiveCell.Value
        UF.TextBox2.Value = ActiveCell.Offset(0, 1).Value
        UF.TextBox3.Value = ActiveCell.Offset(0, 2).Value
        Exit Sub
    Err:
        Exit Sub
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    how silly do i feel, didnt think the me command would work

    Learn something new every day

    cheers

    gibbo

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Always learning!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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