VBA, inline array - arrays

Looking to create an inline array in Visual Basic for Applications
Something like this would be cool:
Dim a() as Integer
set a = {1,2,3}
In Java, this would be the equivalent functionality:
int a[] = {1,2,3};
Also, bonus points if you can tell me how to find its length afterwards (without needing to hard code it, as all the examples my Googling have uncovered)
(please don't tell me to Google it. I normally don't use vb, and I'm discovering that every result for a vb question on Google is answered terribly. ex, hard coding values)

Dim a() As Variant
Dim Length As Integer
a = Array(1,2,3)
Length = UBound(a,1) - LBound(a,1) + 1

Related

For control variable already in use compile error

I am trying to write a nested for loop in visual basic macro which runs on excel.
Here is my simplified code
Dim intVar(2) As Integer
For intVar(1) = 0 To 4
For intVar(2) = intVar(1) To 4
Var = Var + intVar(1) + intVar(2)
Next intVar(2)
Next intVar(1)
When I try to compile the code "For control variable already in use" compile error is thrown. Is there any solution for using array variables for For Loop or should I declare different variables for each For Loop?
There are some questions with the same tag but none of them for the array type control variables. If you help me I would be glad.
Thank you for your interest

Excel v.16 VBA SeleniumBasic array.length equivalent in Selenium

I have an array, and it used to work just fine in VBA. I am now using SeleniumBasic and am trying to find the equivalent of the command ".length"
Dim i As Object
Dim elmCollection As Object
Set elmCollection = WDrv.FindElementsByTag("table")
For i = 0 To (elmCollection.Length)
It bombs out on the "For" statement. So, is there an equivalent to the ".length" in SeleniumBasic?
In visual basic it's ubound(array) to get the upper boundary of an array.
You'll want to use:
For i = 0 To ubound(elmCollection)
Selenium basic WebElements collections IIRC start at 1 and the appropriate property is .Count. You are not working with an array.
Therefore your loop should in fact be a For Each, e.g.
Dim i As Object: For Each i In elmCollection
to enumerate through the WebElements collection; if using the slower method of enumeration with objects then you want For i = 1 to elmCollection.Count

How to obtain 2 dimensional arrays of Single in VB.NET exactly like in VBA?

I have some code written in VBA that uses a function of the particular environment I programmed in, which given some empty, not fixed-dimensional Single arrays, gives them back 2 dimensional and filled with data. It also produces a Variant as output.
The VBA code is:
Dim vDummy As Variant
Dim RealLev1() As Single, ImagLev1() As Single
vDummy = FFPOL1Array(RealLev1, ImagLev1)
Now, I know for sure that FFPOL1Arrayis a routine written in FORTRAN, but I cannot access to its code by any means.
I successfully managed to address the same routine in a VB.NET piece of code by writing a workaround that "links" my code to the environment mentioned above and uses its own scripting routines.
My VB.NET code would be:
Dim vDummy As Object
Dim RealLev1(,) As Single, ImagLev1(,) As Single
vDummy = NSI.FFPOL1Array(RealLev1, ImagLev1)
NSI is the "scripting routines object", which is working with many other functions and subroutines.
Sadly the code above does not work because (according to the debugger) of a type conflict. So I checked the Classes-Explorer and found out that the FFPol1Array class is defined as:
get_FFPOL1Array(ByRef System.Array, ByRef System.Array) As Object
set_FFPOL1Array(ByRef System.Array, ByRef System.Array, ByRef Object)
Thus I tried to Dim my arrays as System.Array instad of Single but this failed too always because of a type conflict. What am I doing wrong?
OK this was a little ridiculous but I managed to understand that I eventually had to initialize the Arrays, because the FORTRAN function did not do that:
Dim RealLev1 As Array = Array.CreateInstance(GetType(Single), 1, 1)
Dim ImagLev1 As Array = Array.CreateInstance(GetType(Single), 1, 1)
This did the job. Even better was:
Dim RealLev1(,) As Single = Array.CreateInstance(GetType(Single), 1, 1)
Dim ImagLev1(,) As Single = Array.CreateInstance(GetType(Single), 1, 1)
As #Nathan_Sav suggested, get_FFPOL1Array is returning an Object so you need to use Set.
Set vDummy = NSI.FFPOL1Array(RealLev1, ImagLev1)

Populating 2D-Arrays from CSV (without m*n-Loops)

while migrating an Excel-VBA project to Visual Basic 2010, I came across a problem when populating arrays.
In Excel-VBA I would do something like
Function mtxCorrel() As Variant
mtxCorrel = wsCorr.UsedRange
End Function
to read an m*n-matrix (in this case n*n), that is conveniently stored in a worksheet, into an array for further use.
In VB2010 I obviously won't use an Excel-Worksheet as storage. csv-Files (see below) seem like a decent alternative.
I want to populate an 2d-array with the csv-contents without looping n*n-times. Let's assume I already know n=4 for demonstration purposes.
This suggests that what I want to do cant be done.
Nevertheless I still hope something like the following could work:
Function mtxCorrel() As Object
Dim array1(4, 4) As String
Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\cm_KoMa.csv")
With ioReader
.TextFieldType = FileIO.FieldType.Delimited
.SetDelimiters(";")
' Here I want to...
' A) ...either populate the whole 2d-array with something like
array1 = .ReadToEnd()
' B) ... or populate the array by looping its 1d-"rows"
While Not .EndOfData
array1(.LineNumber, 0)= .ReadFields()
End While
End With
End Using
return array1
End Function
Notes:
I'm mainly interested in populating the array.
I'm less interested in potential errors with determining which csv-line belongs into which 1d-"row", and also not interested in checking n.
Appendix: sample csv-File:
1;0.5;0.9;0.3
0.5;1;0.6;0.2
0.9;0.6;1;0.1
0.3;0.2;0.1;1

Array already dimensioned - Class module

For certain, technical, reasons, we cannot use styles in word. In an effort to speed up applying global properties over and over, I've created a class that can read from a simple xml style-sheet. The sheet contains different "paragraphs." Each paragraph simply stores the paragraph properties that we use the most.
I'm used to C++ where I can use dynamic memory and I'm trying to replicate the behavior of a dynamically allocated array. However, when I attempt to re-dim I get the error message "Array arleady dimensioned."
My research on the MSDN suggests that in order to ReDim the array has to be Global or in the "general declaration context" This makes me think it might simply not be possible to do it in a class.
Excerpt from MSDN:
"You can use ReDim only at procedure level. Therefore, the declaration
context for the variable must be a procedure; it can't be a source
file, a namespace, an interface, a class, a structure, a module, or a
block."
I have attempted to search stack overflow for "Word VBA Array already dimensioned" and went through all 3 pages of results with no avail.
private type pStyle 'Definition removed because it's not needed
private Paragraphs(0) As pStyle 'Initially an empty array of paragraphs
later I have the following function
Public Function AddEmpty()
'Create space
count = count + 1
ReDim Preserve Paragraphs(count)
AddEmpty = count
End Function
Please let me know if any ideas. I would prefer to not have to "estimate" the number of paragraph styles we will need for each style sheet as every file is different.
Private Paragraphs(0) As ...
This is not an empty array, rather it is a fixed length array with 1 element.
For a dynamic array - one you will later redimension - just declare it as:
Private Paragraphs() As ...
Dim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))
Above code will throw array-already-dimensioned. we can do like
ReDim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))

Resources