ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   user-defined data type capabilities (https://www.excelbanter.com/excel-programming/436490-user-defined-data-type-capabilities.html)

Walter Briscoe

user-defined data type capabilities
 
It seems I use Microsoft Visual Basic 6.5 from Microsoft Excel 2003.

I've just introduced my first user-defined data type (UDT) to a module.
I found it greatly simplified passing parameters.

Private Type Journey
Depart As String
Arrive As String
End Type

What can I do with such UDTs?
1) Declare functions and scalar, array, and parameter variables.
2) Assign e.g. Dim A as UDT, B as UDT: A = B
3) Not compare variables. e.g. if UDT0 = UDT1 ... gets an error.

What else can UDTs be used for?

When, in the Visual Basic Editor (VBE), I hover over code referencing a
variable or expression of a predefined type (Byte, Boolean, Integer,
....), the VBE displays its value.
Can this be extended to UDTs?
if so, how?

I assume such code would avoid the type mismatch I get if I do "?UDT" in
the immediate window.
--
Walter Briscoe

RB Smissaert

user-defined data type capabilities
 
Look in the VBA help after searching for:
Type Statement

RBS


"Walter Briscoe" wrote in message
...
It seems I use Microsoft Visual Basic 6.5 from Microsoft Excel 2003.

I've just introduced my first user-defined data type (UDT) to a module.
I found it greatly simplified passing parameters.

Private Type Journey
Depart As String
Arrive As String
End Type

What can I do with such UDTs?
1) Declare functions and scalar, array, and parameter variables.
2) Assign e.g. Dim A as UDT, B as UDT: A = B
3) Not compare variables. e.g. if UDT0 = UDT1 ... gets an error.

What else can UDTs be used for?

When, in the Visual Basic Editor (VBE), I hover over code referencing a
variable or expression of a predefined type (Byte, Boolean, Integer,
...), the VBE displays its value.
Can this be extended to UDTs?
if so, how?

I assume such code would avoid the type mismatch I get if I do "?UDT" in
the immediate window.
--
Walter Briscoe



JLatham

user-defined data type capabilities
 
You can pretty much do anything with a UDT that you can with any other
variable. You need to remember that the various predefined types you use as
members of your UDT now become properties of any variables you define, via
Dim, as a UDT. Perhaps these simple examples will help you get a better
picture of how it works. One uses a simple variable defined as a UDT named
myUDT, and the second uses an array defined as the same UDT type.

You need to remember to use the properties when referencing any variable
you've declared as one of your UDTs; that will prevent errors when doing
comparisons and when trying to print their values, i.e.
If UDT1.Property1 = UDT2.Property1 Then
or
? UDT1.Property5
in the Immediate window.

Type myUDT
depart As Date
arrive As Date
distance As Integer
End Type

Sub CalculateMPH()
Dim myTrip As myUDT

myTrip.depart = "1/1/09 12:00:00"
myTrip.arrive = "1/1/09 12:45:00"
myTrip.distance = 38
MsgBox "MPH for the trip was: " & myTrip.distance / ((myTrip.arrive -
myTrip.depart) * 24)
End Sub

Sub CalculateTripLegs()
'an array of user defined type myUDT
Dim myTrip(1 To 10) As myUDT

myTrip(1).depart = "1/1/09 12:00:00"
myTrip(1).arrive = "1/1/09 12:45:00"
myTrip(1).distance = 38
MsgBox "MPH for the trip was: " & myTrip(1).distance / ((myTrip(1).arrive
- myTrip(1).depart) * 24)
End Sub


"Walter Briscoe" wrote:

It seems I use Microsoft Visual Basic 6.5 from Microsoft Excel 2003.

I've just introduced my first user-defined data type (UDT) to a module.
I found it greatly simplified passing parameters.

Private Type Journey
Depart As String
Arrive As String
End Type

What can I do with such UDTs?
1) Declare functions and scalar, array, and parameter variables.
2) Assign e.g. Dim A as UDT, B as UDT: A = B
3) Not compare variables. e.g. if UDT0 = UDT1 ... gets an error.

What else can UDTs be used for?

When, in the Visual Basic Editor (VBE), I hover over code referencing a
variable or expression of a predefined type (Byte, Boolean, Integer,
....), the VBE displays its value.
Can this be extended to UDTs?
if so, how?

I assume such code would avoid the type mismatch I get if I do "?UDT" in
the immediate window.
--
Walter Briscoe
.



All times are GMT +1. The time now is 06:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com