vba, Why an un-dimensioned array works with Split() - arrays

I can use an un-dimensioned String array with the Split() function to read fields from a String, but apparently, a String array has to be dimensioned to use it in a loop.
Why is that, and are there other situations where an array does not need to be dimensioned?
Dim field() As String
field = Split(data_line, "~")
Dim pref_line(10) As String
Input #1, pref_line(i)

but apparently, a String array has to be dimensioned to use it in a loop.
When use an un-dimensioned String array with the Split() function, the array is automatically dimensioned and values assigned to it. Also you can use that in a loop using For i = LBound(field) to UBound(field)
Option Explicit
Sub Sample()
Dim field() As String
Dim data_line As String
data_line = "aaa~bbb"
field = Split(data_line, "~")
Debug.Print field(0)
End Sub
Are there other situations where an array does not need to be dimensioned?
Yes. When you do not know how many items needs to be added to an array then you declare and un dimensioned array and Redim Preserve it to add values. For example
Option Explicit
Sub Sample()
Dim field() As Long
Dim n As Long, i As Long
ReDim Preserve field(n)
For i = 1 To 100
field(n) = i
n = n + 1
ReDim Preserve field(n)
Next i
End Sub

Split() returns its own array regardless of what you defined before. You are effectively overwriting the value of your variable.

Dim field() As String declares an array, but does not allocate it.
Dim pref_line(10) As String both declares and allocates it.
field = Split(data_line, "~") both allocates an array and populates it.

Others have already answered your question, and correctly about what you asked for. So I won't comment on that.
On a side note, you should avoid Redimming arrays as much as possible. Do that only when absolutely necessary. Remember that it is just one line of code for us, but a whole lot of work for the runtime engine. First find a new contiguous memory location for the new array; then copy the array items from the old to new one; then discard the old array. So you see one Redim Preserve statement produces how much work for the compiler.
For this reason, redimming inside a loop is "usually" a bad idea, since you know in advance how many times the loop will run and how many elements you will have in the end. So in such cases, redim your array before the start of loop, rather than doing it inside the loop. The end result would be a better performance :)
Taking the code in #SiddharthRout 's reply as an example, notice that the Loop will produce 100 items since you are looping from 1 to 100. So instead of ReDim Preserve field(n) inside the loop, move that outside it before the start of loop. ReDim Preserve field(100) as Long, or simply ReDim field(100) as Long.

Related

Return a Range not an array from a UDF

I have created a UDF that merges various value, range and array inputs into a single array.
Hitting ctrlshiftenter enters it as an array formula meaning VALUE(F4:G4) returns an array {1,#VALUE}.
My formula stitches values (5, "cat"), ranges ("F6","B2:D6") and even arrays like the one returned by Value(), and outputs an array. In the image example that is
{0.283982519688569,0.161633595994901,0.521865473646119,0.675542592903341,0.119984734979722,0.842918190377968,0.882045093468071,0.57708164295789,0.305844376489788,0.365360349735221,0.131686453379672,0.557018723742854,0.511032693705543,0.746174410924489,0.863516943921978,5,"cat","Dog",1,#VALUE!}
Here's the code:
Public Function PROBABLY(ParamArray inputArray() As Variant) As Variant
'Takes a list of parameters, the first of which is an integer, and
Dim inElement As Variant
Dim outputArray() As Variant
Dim subcell As Variant
'convert ranges to values
'creating a new array from the mixture of ranges and values in the input array
ReDim outputArray(0)
For Each inElement In inputArray
'Normal values get copied from the input UDF to an output array, ranges get split up then appended
If TypeName(inElement) = "Range" Or TypeName(inElement) = "Variant()" Then
For Each subcell In inElement
outputArray(UBound(outputArray)) = subcell
ReDim Preserve outputArray(UBound(outputArray) + 1)
Next subcell
'Stick the element on the end of an output array
Else
outputArray(UBound(outputArray)) = inElement
ReDim Preserve outputArray(UBound(outputArray) + 1)
End If
Next inElement
ReDim Preserve outputArray(UBound(outputArray) - 1)
PROBABLY = outputArray
End Function
That was just to show the versatility of the formula with various inputs, in reality I do not want to use this in array formulae. =PROBABLY(B2:D6,5,"cat") could be entered normally, without ctrlshiftenter. But the issue is that this returns a Variant() -specifically an array*, which makes it hard to use with non array formulae, which often require a Range input. So =CONCAT(PROBABLY(B2:D6,5)) works fine, but SUMIF(PROBABLY(B2:D6,5),">.5") does not work, as SUMIF() requires a range input. Same reason SUMIF(VALUE(1,2),">.5") doesn't work, like my function, value returns an array.
So the question: can I trick Excel into thinking my array is a range, format it as a Range in some way. Obviously I could take the code out of the UDF and into a sub, then paste my array into a temporary spreadsheet range and tell excel to reference that, but I was hoping to keep the UDF wrapper. (I don't think I can edit the worksheet from within a UDF, although is there some hidden space where I can paste the array and access it from a formula?
Thinking aloud there, any better insights?
*hence () after Variant

Can I Use a Variable to ReDim an Array?

I am not entirely sure why I am getting the error message of
Expecting a dynamic array var
with this code:
Option Explicit
Sub ArrayTest()
Dim i As Integer, BankList(0) As Variant, x As Integer
For i = 0 To UBound(ScreenArray)
If ScreenArray(i) Like "TR=SUB*" Then
Debug.Print ScreenArray(i)
ReDim Preserve BankList(x) '<<< ERROR LINE
BankList(x) = ScreenArray(i)
x = x + 1 'Raise the value for the next occurrence, if needed.
End If
Next
End Sub
Basically I am attempting to move specific strings from one array to a new array, if certain criteria are met. It's difficult to determine how many strings will be in the new array until running this For...Next statement.
If you can't tell from the code, the original array is ScreenArray and the new array is BankList.
To create a dynamic array, do not specify the size in the original declaration.
So use BankList() As Variant instead of BankList(0) As Variant.

Array in excel vba

I want to have an array list in vba, hence I have a variant declared in excel vba like:
Dim Students(10) as variant
Now I want to store numbers in Students list. the numbers are not continuous. Sometime like:
Students(2,7,14,54,33,45,55,59,62,66,69)
How can I do this in vba? also how can I access the list items?
Students must be declared as a dynamic array. That is, an array whose bounds can be changed. Dim Students(10) gives an array whose bounds cannot be changed and cannot be loaded from an array.
Dim Students() As Variant
To load Students:
Students = Array(2,7,14,54,33,45,55,59,62,66,69)
To access the elements:
Dim Inx As Long
For Inx = LBound(Students) to UBound(Students)
Debug.Print Students(Inx)
Next
LBound (Lower bound) and UBound mean that the for loop adjusts to the actual number of elements in Students.
This is too complex for you right now, and you'll probably never run into a situation where you'll need this, but:
I use the following method for forming more memory-efficient arrays (because Variant uses the most memory of any variable type) while still having the convenience of declaring the array contents in one line. To follow your example:
Dim Students() As Long
Dim Array2() As String
Array2() = Split("2,7,14,54,33,45,55,59,62,66,69", ",")
ReDim Array1(0) As Long
For Loop1 = LBound(Array2()) To UBound(Array2())
ReDim Preserve Array1(0 To (UBound(Array1) + 1)) As String
Array1(Loop1) = Array2(Loop1)
Next Loop1
ReDim Preserve Array1(0 To (UBound(Array1) - 1)) As Long
Erase Array2
An example of accessing it would be something like:
For Loop1 = LBound(Students) to UBound(Students)
Msgbox Students(Loop1)
Next Loop1
I learned this from here: http://www.vbforums.com/showthread.php?669265-RESOLVED-VBA-Excel-Assigning-values-to-array-in-a-single-line&p=4116778&viewfull=1#post4116778
You can add values to an Array like this...
For i = 1 to 10
Students(i) = i
Next i
Or like this
Students = Array(2,7,14,54,33,45,55,59,62,66,69)
Then you can access the values in the same manor. Note if you use the second option you'll need to declare it as follows:
Dim Students() As Variant
Well,
That depends on how you would supply the values for the array, would you get the values from Worksheet.Range or from TextBox or ListBox , But basically the code would be something like that :
Dim students(10) as Integer
Dim Carrier as Integer
For i = LBound(students) To UBound(Students)
'some code to get the values you want to from whatever is your source
'then assign the value to Carrier
students(i)=Carrier
Next i
It is not good practice to dim an array as Variant when you certainly know that you are going to use integers only, as it will eat alot of memory that is not needed in the first place.
You also should be aware of the bounds of the numbers that are going to be assigned, if it exceeds the Integer limit then you should use Double or Float.
This is my first participation in the site,Cheers.

Excel VBA: Range to String Array in 1 step

I know you can easily take a range of cells and slap them into a Variant Array but I want to work with a string array (because it's single-dimensional and takes less memory than a Variant array).
Is there any way to automatically convert a range into a string array?
Right now I am using a function that will take the range and save the values in a variant array, then convert the variant array to a string array. It works nice , but I'm looking for a way to go directly from the range to string array. Any help would be greatly appreciated.
Function RangeToArray(ByVal my_range As Range) As String()
Dim vArray As Variant
Dim sArray() As String
Dim i As Long
vArray = my_range.Value
ReDim sArray(1 To UBound(vArray))
For i = 1 To UBound(vArray)
sArray(i) = vArray(i, 1)
Next
RangeToArray = sArray()
End Function
UPDATE:
It's looking like there is no way to skip the step of throwing the data into a variable array first before converting it to a single-dimensional string array. A shame if it's true (even if it doesn't take much effort, I like to ultra-optimize so I was hoping there was a way to skip that step). I'll close the question in a few days if no solution presents itself. Thanks for the helpful comments, guys!
UPDATE2:
Answer goes to Simon who put in great effort (so did everyone else) and utlimately pointed out it's indeed impossible to go from range to string array in one shot. Thanks, everyone.
You actually can go directly from a range to an array using the functions Split, Join and a delimiter not in the text.
Assuming you have already assigned a 1D range of values as SrcRange
Dim Array() As String: Array = Split(Join(Application.Transpose(SrcRange), "#"), "#")
How about...
Public Function RangeToStringArray(theRange As Excel.Range) As String()
' Get values into a variant array
Dim variantValues As Variant
variantValues = theRange.Value
' Set up a string array for them
Dim stringValues() As String
ReDim stringValues(1 To UBound(variantValues, 1), 1 To UBound(variantValues, 2))
' Put them in there!
Dim columnCounter As Long, rowCounter As Long
For rowCounter = UBound(variantValues, 1) To 1 Step -1
For columnCounter = UBound(variantValues, 2) To 1 Step -1
stringValues(rowCounter, columnCounter) = CStr(variantValues(rowCounter, columnCounter))
Next columnCounter
Next rowCounter
' Return the string array
RangeToStringArray = stringValues
End Function
Function RangeToStringArray(myRange as range) as String()
ReDim strArray(myRange.Cells.Count - 1) As String
Dim idx As Long
Dim c As Range
For Each c In myRange
strArray(idx) = c.Text
idx = idx + 1
Next c
RangeToStringArray = strArray
End Function
If you don't mind altering the contents of the clipboard then:
COPY the range to the clipboard with the Copy method:
MyTargetRange.Copy
Copy the contents from the clipboard to a string variable (search this site or elsewhere for functions to transfer strings to/from the clipboard).
SPLIT the string into a variant array:
MyStringArray = Split(MyClipboardText, vbCrLf)
OPTIONAL: The array will have one additional blank element because there is always an additional Return (vbCrLf) at the end of the text you just copied to the clipboard. To remove simply resize the array:
Redim Preserve MyStringArray(Ubound(MyStringArray) - 1)
Very simple and quick!!!
Drawbacks are that the clipboard may change when you least expect it (during a recalculation) and that it only produces arrays of strings (not Doubles or other numerical value types).
This would be EXTREMELY HELPFUL if you are working with lots of repetitive functions (thousands) that use the same data (thousands of data points). The first time your function is called, do all the intermediate calculations on the ranges of data that you need but save the results in static variables. Also save a string copy of your input ranges via the clipboard. With each subsequent call to your function, convert the input ranges to text, again via the clipboard, and compare with the saved copy. If they are the same you may be able to bypass allot of your preliminary calculations.
Named ranges used in VBA are already arrays. So first make the range into a named range, then refer to it and delete the named range.
For example:
ThisWorkbook.Names.Add Name:="test_array", RefersTo:=Sheet1.Range("A4:B10")
a = Sheet1.Range("test_array")
ThisWorkbook.Names("test_array").Delete

How do I declare an array variable in VBA?

I need to add the var in array
Public Sub Testprog()
Dim test As Variant
Dim iCounter As Integer
If test = Empty Then
iCounter = 0
test(iCounter) = "test"
Else
iCounter = UBound(test)
End If
End Sub
Getting error at test(iCounter) = "test"
Please suggest some solution
Generally, you should declare variables of a specific type, rather than Variant. In this example, the test variable should be of type String.
And, because it's an array, you need to indicate that specifically when you declare the variable. There are two ways of declaring array variables:
If you know the size of the array (the number of elements that it should contain) when you write the program, you can specify that number in parentheses in the declaration:
Dim test(1) As String 'declares an array with 2 elements that holds strings
This type of array is referred to as a static array, as its size is fixed, or static.
If you do not know the size of the array when you write the application, you can use a dynamic array. A dynamic array is one whose size is not specified in the declaration (Dim statement), but rather is determined later during the execution of the program using the ReDim statement. For example:
Dim test() As String
Dim arraySize As Integer
' Code to do other things, like calculate the size required for the array
' ...
arraySize = 5
ReDim test(arraySize) 'size the array to the value of the arraySize variable
Further to Cody Gray's answer, there's a third way (everything there applies her as well):
You can also use a dynamic array that's resized on the fly:
Dim test() as String
Dim arraySize as Integer
Do While someCondition
'...whatever
arraySize = arraySize + 1
ReDim Preserve test(arraySize)
test(arraySize) = newStringValue
Loop
Note the Preserve keyword. Without it, redimensioning an array also initializes all the elements.
Further to RolandTumble's answer to Cody Gray's answer, both fine answers, here is another very simple and flexible way, when you know all of the array contents at coding time - e.g. you just want to build an array that contains 1, 10, 20 and 50. This also uses variant declaration, but doesn't use ReDim. Like in Roland's answer, the enumerated count of the number of array elements need not be specifically known, but is obtainable by using uBound.
sub Demo_array()
Dim MyArray as Variant, MyArray2 as Variant, i as Long
MyArray = Array(1, 10, 20, 50) 'The key - the powerful Array() statement
MyArray2 = Array("Apple", "Pear", "Orange") 'strings work too
For i = 0 to UBound(MyArray)
Debug.Print i, MyArray(i)
Next i
For i = 0 to UBound(MyArray2)
Debug.Print i, MyArray2(i)
Next i
End Sub
I love this more than any of the other ways to create arrays. What's great is that you can add or subtract members of the array right there in the Array statement, and nothing else need be done to code. To add Egg to your 3 element food array, you just type
, "Egg"
in the appropriate place, and you're done. Your food array now has the 4 elements, and nothing had to be modified in the Dim, and ReDim is omitted entirely.
If a 0-based array is not desired - i.e., using MyArray(0) - one solution is just to jam a 0 or "" for that first element.
Note, this might be regarded badly by some coding purists; one fair objection would be that "hard data" should be in Const statements, not code statements in routines. Another beef might be that, if you stick 36 elements into an array, you should set a const to 36, rather than code in ignorance of that. The latter objection is debatable, because it imposes a requirement to maintain the Const with 36 rather than relying on uBound. If you add a 37th element but leave the Const at 36, trouble is possible.
As pointed out by others, your problem is that you have not declared an array
Below I've tried to recreate your program so that it works as you intended.
I tried to leave as much as possible as it was (such as leaving your array as a variant)
Public Sub Testprog()
'"test()" is an array, "test" is not
Dim test() As Variant
'I am assuming that iCounter is the array size
Dim iCounter As Integer
'"On Error Resume Next" just makes us skip over a section that throws the error
On Error Resume Next
'if test() has not been assigned a UBound or LBound yet, calling either will throw an error
' without an LBound and UBound an array won't hold anything (we will assign them later)
'Array size can be determined by (UBound(test) - LBound(test)) + 1
If (UBound(test) - LBound(test)) + 1 > 0 Then
iCounter = (UBound(test) - LBound(test)) + 1
'So that we don't run the code that deals with UBound(test) throwing an error
Exit Sub
End If
'All the code below here will run if UBound(test)/LBound(test) threw an error
iCounter = 0
'This makes LBound(test) = 0
' and UBound(test) = iCounter where iCounter is 0
' Which gives us one element at test(0)
ReDim Preserve test(0 To iCounter)
test(iCounter) = "test"
End Sub
You have to declare the array variable as an array:
Dim test(10) As Variant
David, Error comes Microsoft Office Excel has stopped working. Two options check online for a solution and close the programme and other option Close the program
I am sure error is in my array but I am reading everything and seem this is way to define arrays.
The Array index only accepts a long value.
You declared iCounter as an integer. You should declare it as a long.

Resources