Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA How to declare an array with elements with different datatype

Tags:

excel

vba

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.

like image 881
Siraj Samsudeen Avatar asked Oct 25 '12 12:10

Siraj Samsudeen


People also ask

Can array have different data types VBA?

A VBA array in excel is a storage unit or a variable which can store multiple data values. These values must necessarily be of the same data type.

Can array have different data type?

No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.

Do arrays need to have the same data type?

Description: Array: collection of fixed number of components (elements), wherein all of components have same data type.

How do I assign a dynamic array in VBA?

Create a Dynamic Array in VBAFirst, declare an array with its name. After that, the elements count left the parentheses empty. Now, use the ReDim statement. In the end, specify the count of elements you want to add to the array.


1 Answers

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
like image 177
LittleBobbyTables - Au Revoir Avatar answered Oct 21 '22 05:10

LittleBobbyTables - Au Revoir