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
Related
I'm trying to search for a substring within an array using the filter function and then creating a new array of strings which include that substring. When I try, I get a type mismatch runtime error 13 message. I've read that filter can only be used on a 1-dimensional array and that populating an array with a range automatically creates a 2-dimensional array, but I thought I'd read I could get around this problem if I transposed the array when creating it. I think I might be wrong about that as it still doesn't seem to work for me.
Before the bug strikes, the headerTitles array is populated with the following strings:
vaccination_date
year
month
day
site
dose
vaccine
vaccination_setting
This is all using Excel 2010.
Thanks for any help.
Sub dateInHeaderTitle()
Dim ws As Worksheet
Dim lastCol As Integer
Dim headerTitles As Variant
Dim dateTitles As Variant
Set ws = ThisWorkbook.Worksheets(1)
With ws
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
headerTitles = Application.Transpose(.Range(.Cells(1, 1), .Cells(1, Lastcol)).Value2)
End With
dateTitles = Filter(headerTitles, "date", , vbTextCompare)
Set ws = Nothing
End Sub
In order to obtain a 1D array, starting from a column range, you should use:
headerTitles = Application.Transpose(Application.Transpose(.Range(.cells(1, 1), .cells(1, lastCol)).Value2))`
Debug.Print Join(headerTitles, "|") 'just to see it...
I have some data in terms of a column that I want to store into an array using VBA. After storing it, I will reference the element in the array and make a comparison.
Dim tRange As Range
Set tRange = wb.Sheets("wbname").Range("A1:A5")
Lets say I want to store column A with 5 row into the array in VBA. May I know the way?
Here is one way:
Sub Dave()
Dim tRange As Range, wb As Workbook, cell As Range
Dim i As Long
Set wb = ThisWorkbook
Set tRange = wb.Sheets("wbname").Range("A1:A5")
ReDim arr(1 To tRange.Count)
i = 1
For Each cell In tRange
arr(i) = cell.Value
i = i + 1
Next cell
End Sub
NOTE:
This technique does not depend on the "shape" of the range. It will work if the range is a piece of a column, or a piece of a row, or a rectangle of cells, or even a disjoint set of cells.
You can just declare a Variant data type and make it equal to the range.
Dim DirArray As Variant
DirArray = Range("a1:a5").Value
This was answered in a previous question by #vacip coincidentally for exactly the same range!
Creating an Array from a Range in VBA
I need to have the unique values from a column (column c from sheet1) and have the unique values in an array so that I can reuse them again from that array.
I'm new to stak so, please help me.
This code is just to get you started. To see how it works, first select your range of data and then step through this code. Note that after sorting, it puts the range into a variant (a) and then just to confirm that it's in there, the variant is placed into a range next to the original. You can then use the variant as needed.
Sub sort()
Dim r As Range, a As Variant
Set r = Selection
With ActiveSheet.sort
.SetRange r
.Apply
End With
a = r
r.Offset(0, 1) = a
End Sub
you can use Dictionary object
here's a Function that retrieves all unique values from a passed Range reference
Function uniqueValues(columnRng As Range) As Variant
Dim cell As Range
With CreateObject("Scripting.Dictionary")
For Each cell In columnRng.SpecialCells(xlCellTypeConstants)
.Item(cell.Value) = 1
Next
uniqueValues = .Keys
End With
End Function
This function may be exploited in your "mian" code as follows:
Sub main()
Dim values As Variant
values = uniqueValues(Columns("C"))
End Sub
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)
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