Consulting

Results 1 to 5 of 5

Thread: Passing a UDT member/element

  1. #1

    Passing a UDT member/element

    Hello! I am trying to pass a member (element) of a UDT and running into difficulties. i can pass a UDT array, but im not able to specify which piece of the UDT i want the sub to focus on. im sure its in my declaration. im declaring it as a string which is probably the problem.

    [VBA]
    Type SpreadData
    name As String
    time As Date
    l As Double
    b As Double
    a As Double
    End Type

    Public NIY(100) As SpreadData

    Sub Start()
    Dim i As Integer
    Dim element As String

    i = 0
    NIY(i).name = "NIY"
    NIY(i).time = Now
    NIY(i).l = 10055
    NIY(i).a = 10050
    NIY(i).b = 10045

    element = "b" 'element (type.member) needs to be dynamic between l, b, and a
    Call f(NIY, num, element)
    End Sub

    Sub f(ByRef NIY() As SpreadData, i As Integer, element As String)
    Worksheets("Sheet1").Rows(1).Columns("A") = NIY(i).&"part" 'the problem for me is passing the element
    End Sub
    [/VBA]

    Thank you so much for your help!

    Happy coding

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    perhaps if you set num to something before executing this line?:
    Call f(NIY, num, element)
    I replaced it with i below and put a Select Case in the function[vba]Sub Start()
    Dim i As Integer
    Dim element As String

    i = 0
    NIY(i).name = "NIY"
    NIY(i).time = Now
    NIY(i).l = 10055
    NIY(i).a = 10050
    NIY(i).b = 10045

    element = "b" 'element (type.member) needs to be dynamic between l, b, and a
    Call f(NIY, i, element)
    End Sub

    Sub f(ByRef NIY() As SpreadData, i As Integer, element As String)
    Select Case element
    Case "b": Result = NIY(i).b
    Case "l": Result = NIY(i).l
    Case "a": Result = NIY(i).a
    End Select
    Worksheets("Sheet1").Rows(1).Columns("A") = Result
    End Sub
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Thank you so much my friend!!! the select case works and that was conceptually holding me back but you were so right on the num too.

    I still wish there was a way to pass the actual member of the UDT (.a or .b in NIY(0).a) and not have to work around it by passing a string to a select case to define the member but oh well. Does anyone know how to do this?

    Thanks again!

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    and why not:[vba]Sub Start()
    Dim i As Integer
    Dim element As String

    i = 0
    NIY(i).name = "NIY"
    NIY(i).time = Now
    NIY(i).l = 10055
    NIY(i).a = 10050
    NIY(i).b = 10045

    f (NIY(i).b)
    End Sub

    Sub f(ZZ)
    Worksheets("Sheet1").Rows(1).Columns("A") = ZZ
    End Sub
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Thanks for your help p45cal! im trying to pass all of the elements in the NIY.b array not just NIY(i).b. It looks like case select is the best bet. Again, thank you so much!

Posting Permissions

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