Consulting

Results 1 to 5 of 5

Thread: Help with Function

  1. #1

    Help with Function

    ok so I have a request from my boss... I suggested some other methods to do this but he wanted to know if it was possible to do it in the way i'm about to desribe:

    Background:
    We have a spread sheet with test data that are put onto plots. the spreadsheet uses a graph, and PIP to show which "bin" each of the tested parts fall into. However, he wants to combine all the tested parts into one spreadsheet.

    Now the function is defined for the particular part tested. He was curious as to whether the actual name of the function could be a variable

    i.e.

    [VBA]Function SoL_bin_D2(u, v)[/VBA]

    if "SoL_bin_D2" could change so that the code would run based on the part number, and the function would also be defined under this part number.

    I suggested If statements that would just encapsulate the entirety of the function but he doesnt want that for ease of future manipulation.

    Any suggestions?

    Please ask if you need any more info. Thanks.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you mean
    [VBA]
    Test = Sol_bin_ & MyPart(u,v)
    [/VBA]
    I don't think you can do this.

    BTW, you might want to use Select Case rather than multipe IF statements, if you need to go down that road.
    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'

  3. #3
    yeah i didnt think it was possible either. but i figured i would ask anyway, thanks

  4. #4
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    You may be able to use CallByName, but I believe the function that you are calling needs to be in a class or userForm module for this.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    You can pass the part number to a driver sub, which would then .Run a sub based on the passed part number

    [vba]
    Option Explicit

    Sub Drv()
    Call SubDrv("1", "11111111111111111")
    Call SubDrv("2", "22222222222222222")
    Call SubDrv("3", "33333333333333333")
    End Sub

    Sub SubDrv(ByRef p As String, ByRef s As String)
    Call Application.Run("SubName" & p, s)
    End Sub

    Sub SubName1(s As String)
    MsgBox "Sub 1 " & s
    End Sub
    Sub SubName2(s As String)
    MsgBox "Sub 2 " & s
    End Sub
    Sub SubName3(s As String)
    MsgBox "Sub 3 " & s
    End Sub

    [/vba]

    Might be able to simplfy this a bit

    Paul

Posting Permissions

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