Excel VBA: adding a row to a variant array in one line - arrays

I´d like to add a row to a variant array:
Dim arrMod As Variant
arrMod(numberOfRow) = Array(myValue1, myValue2, myvalue3)
The execution of this code results into an exception: Error 13: type mismatch
How can I do it without iterating each column?
Thanks,
Regards

Your variable arrMod is not an array. You need to define it in VBA as an array using parenthesis:
Dim arrMod(0) As Variant
Obviously replace 0 with the maximum number of rows you have, or resize dynamically using redim preserve.

Do you need something like this:
Dim arrMod()
For i = 1 To 5
ReDim Preserve arrMod(i)
arrMod(i) = i
MsgBox Join(arrMod, vbCrLf)
Next i

Related

How do I feed an array of worksheet names into VBA Sheets.Copy?

I have this code that populates an array with worksheet names, given a condition:
Dim lng_Counter As Long
Dim arr_wks() As Variant
lng_Counter = 1
For Each wks In ThisWorkbook.Worksheets
If Left(wks.Name, 2) = "Hi" Then
ReDim Preserve arr_wks(lng_Counter)
arr_wks(lng_Counter) = wks.Name
lng_Counter = lng_Counter +1
End if
Next wks
I would then like copy these worksheets to a new workbook and so I have tried something like this:
Sheets(arr_wks()).Copy
Which isn't working. The only way I can get it to work is to write out:
Sheets(Array(arr_wks(1),arr_Wks(2),...)).Copy
Which isn't useful as the size of the array will change depending on the number of sheets that meet the condition at a given time.
How can I feed the array into a Sheets(arr).Copy type argument?
You end up with an error where the first element (with an index of zero) is empty.
You can fix that with
lng_Counter = 0
, or
ReDim Preserve arr_wks(1 to lng_Counter)
, or
Option Base 1
on top.

How can I assign a range of cells to an array

I'm trying to create a simple 1D array using the following code.
Sub Button3_Click()
Dim search() As Variant
Dim data() As Variant
Worksheets("IO List").Activate
Set searchitems = ActiveSheet.Range("A1", "U1")
Set ExportData = ActiveSheet.Range("A3", "U3")
search = searchitems.Value
MsgBox (search(1))
End Sub
The message box is simply to check the value of the array but I am thrown the error: Runtime error '9': Subscript or of range
When assigning cell values to an array, you always get a 2-D array even if you are only collecting the values from a single column or single row.
Application.transpose will convert row data from a single column into a one-based 1-D array. Using it twice will convert column data from a single row into a one-based 1-D array.
Dim search As Variant, data As Variant
Worksheets("IO List").Activate
search = Application.transpose(Application.transpose(Range("A1", "U1").value))
data = Application.transpose(Application.transpose(Range("A3:U3").value))
MsgBox search(1)
Excel is optimized for working with 2D Variant arrays.
You almost always want Value2 as opposed to Value. (see TEXT vs VALUE vs VALUE2 – Slow TEXT and how to avoid it)
You can also avoid activating the worksheet by using a With block:
Sub Button3_Click()
Dim search() As Variant
Dim data() As Variant
With Worksheets("IO List")
search = .Range("A1", "U1").Value2
ExportData = .Range("A3", "U3").Value2
End With
MsgBox search(1, 1)
End Sub

Generating an array, and using Vlookup to loop through this array (VBA)

using the following code i have tried to generate an array based on what is in column "LN", (all values in "LN" are named ranges, corresponding to different cells). Then using vlookup I want it to look at cell d25, and look for a match within every named range of the array.
Sub Find_Value()
Sheets(“Sheet5”).Select
Dim arr() As Variant
arr() = Sheets("Sheet5").Range("LN4:LN" & LRow).Value
Dim I As Long
For I = 1 To Range("LN").End(xlUp).Value
Sheets(“Estimation - Entry”).Select
Range("P40").Value = arr(1)
Range(“E25").Value=WorksheetFunction.Vlookup(Range(“D25").Value,Range((arr(i)),2,0)
End Sub
(this code doesn't work and i am unsure why)

Populate an array without a loop

I am trying to avoid the use of loops for populating arrays since they take a lot of time when managing a lot of data.
Apparently as well, that is possible and easy in VBA but often results in problems.
Here is the code:
sub populate()
'put the whole column in an array
Dim AppArray() As Variant
Dim AppRange As Range
'calculate the last row of the column 1 of sheets
Dim LstRow As Integer
LstRow = Sheets("whole").Cells(Sheets("whole").Rows.Count, "A").End(xlUp).row
'here I calculate the range that I want to pass to the array
Set AppRange = Sheets("whole").Range(Cells(1, 1), Cells(LstRow, 1))
MsgBox ("apprange " & AppRange.Address)
'i dont know if I need to redim or not
ReDim AppArray(1 To LstRow)
'here comes the point. populate the array with the values of the range
AppArray = AppRange.Value
End Sub
This does not work. I also tried application.tranpose(AppRange.Value).
I used:
For i = 1 To LstRow
Debug.Print AppArray(i)
Next
and an error appears, so somehow there is no AppArray(1).
I would be very happy if you can comment on that. More than just arranging the code suggest even other pages (links) to populate arrays with values of ranges when these ranges are not known in advance.
If the case is that looping is very time consuming and that arrays can be populated straight away, I don't understand why 99% of the pages referring to arrays use a loop (or nested loop) to populate an array.
I found the answer.
dim myRange as range
dim myArray() as variant
myRange = range(cells(2,3),cells(10,15))
redeem myArray(1 to 20,1 to 20)
myArray=myRange
It's always much faster to work with variables and arrays than with cells values.

VBA Access 2010 extracting array elements as individual dynamically named variables

I am working through an Access 2010 VBA script. I am pulling information from a temporary table that may have 1 to 10 records in it. I am using GetRows to build a two-dimensional array that looks like this, for example:
**0** **1**
8677229 1
10289183 2
11981680 3
13043481 4
I have tested the array function by debug print, and the array does contain values. I want to split out the array values dynamically into variables for later use. In this example, I would like the 0 column to produce 4 variables named Anum(0) through Anum(3), and the 1 column to produce 4 variables named Pay(0) through Pay(3). However, I keep getting a "Error 9, subscript out of range" where indicated below:
Dim db As Database
Dim rs As Recordset
Dim PayAcct As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT Anum, Pay FROM PayPerAcct")
Dim highval As Integer
Dim i As Integer
i = 0
While Not rs.EOF
PayAcct = rs.GetRows(10)
Wend
highval = UBound(PayAcct, 2)
Dim Anum() As Variant
Dim Pay() As Variant
For i = 0 To highval
'error occurs on the below line
Anum(i) = CStr(PayAcct(0, i))
Pay(i) = CStr(PayAcct(1, i))
Next i
When I manually define Anum and Pay variables and use the Cstr(PayAcct(1,0)) operation, it passes the expected value from the array to the variable, so I don't think that is the problem.
I suspect I am assigning Anum() and Pay() the wrong dimension, because I have seen similar example code in Excel VBA where defining those variables as Range works. I have tried defining Anum() and Pay() as Range (which doesn't work, because this is Access VBA), Variant, and Object.
Any thoughts or tips?
Edit -
The below ended up working, thanks for your help:
Dim Anum() As String
ReDim Anum(0 To highval)
Dim Pay() As String
ReDim Pay(0 To highval)
Dim Anum() As Variant
declares a dynamic array that hasn't any elements yet, so you can't assign values to it. It needs a ReDim to use it.
But since you already know how many elements you need, what you want is:
Dim Anum(0 To highval) As Variant
or if you know that you will only store strings in it,
Dim Anum(0 To highval) As String

Resources