Finance

Charts

Statistics

Macros

Search

Creating a Custom Data Type in Excel VBA

First, a suitable custom data type is created for this data:

Type Person
    FirstName As String * 20
    LastName As String * 20
    City As String * 20
    BirthDate As Date
    Salary As Single
End Type

The * 20 suffix for the String data type ensures that the associated variable is not a variable-length string, as we have seen before, but a fixed-length string. If a shorter value is assigned to such a variable, trailing spaces are automatically appended. If a longer value is assigned, excess characters at the end are truncated.

Fixed-length strings are now rarely used but are introduced here for this specific purpose.

All data types inside a user-defined data type must have a fixed memory size. The total length of a record of this type is 72 bytes, calculated as follows:

  • Three fixed-length strings of 20 bytes each (total 60 bytes),
  • One Single type of 4 bytes,
  • One Date type of 8 bytes.

When opening a file with random access, the length of each record must be specified. For three records, the total file size would be 216 bytes (3 × 72 bytes).

Knowing the file size allows you to determine the number of records contained within the file.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx