1

I am studying at excel with vba macro. I am trying to create a type as MessageID. This type has four variable as string such as Message_ID, Message_Name, Message_Byte and Message_Bit. A Message has just one Message_ID, however; a Message_ID has more than one Message_Name and every name has and just one byte and bit. In the other word, I need a Type has some variables. Because of this, I am trying to make the code above work. But these variables need to keep some variables. But, of course, it does not work.

How can I solve this? Thank you.

Public Type Message

  Message_ID As String

  Public Type Message_CH

    Message_Name As String
    Message_Byte As String
    Message_Bit As String

  End Type

End Type

2 Answers 2

1

You need to un-nest them, and then use the sub-type as a member of the super-type:

Public Type SubMessage
    Message_Name As String
    Message_Byte As String
    Message_Bit As String
End Type

Public Type Message
    Message_ID As String
    Message_CH As SubMessage
End Type

You would then access the sub-type from the super-type like this:

Sub Foo()
    Dim x As Message
    Debug.Print x.Message_CH.Message_Name
End Sub

I suspect, though, that you would be better served by writing a class and using that instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I am just improving myself in vba and i have just learnt subtype thanks to you. Thanks again.
1

Just wanted to add on Comintern's answer. Order matters. Subtype needs to be defined before the type. If you put the Type text first, and SubType after, you'll get an error message.

1 Comment

"Invalid Forward Reference" if I recall correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.