6

I have an array that I have created to store error records and it has the following elements: Serial No,File name, error type, error cell, error cell value

As of now I have declared my array like this and then I populate the values later on.

Dim errorArray() As String

But ideally, I want Serial Number to be a proper integer, but it is getting converted into string. I don't know how to declare this correctly so that I can have Long datatype for the first element and string for the next 3 and variant for the last one.

2 Answers 2

26

Create a private type in your code, like this:

Private Type ErrRecord
    SerialNo As Long
    FileName As String
    ErrorType As String
    ErrorCell As String
    ErrorCellValue As Variant
End Type

And then in your routine, call it like this:

Dim errorArray(0) As ErrRecord
With errorArray(0)
    .SerialNo = 12345
    .FileName = "Test.xls"
    .ErrorType = "Bad error"
    .ErrorCell = "1234"
    .ErrorCellValue = "Test"
End With
Sign up to request clarification or add additional context in comments.

2 Comments

What if I want a dynamic array. Like I am storing data retrieved from DB, so columns may differ in datatype.
Should be the accepted answer. Neat and clean solution.
8

You'll want to create an array of type Variant. An array of type Variant can store any data type in any of its elements.

Dim astrItems(0 To 9)    As String
Dim varItems             As Variant

varItems = astrItems

1 Comment

So obvious, but I missed it. In fact, I was thinking that I declared an array of type Variant, so I was a bit confused when I saw your response. Thanks.

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.