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)
Related
I previously used the following code -
Dim HSArr(2 To 250) As Variant
Dim HSVal As Long
For HSVal = LBound(HSArr) To UBound(HSArr)
HSArr(HSVal) = Cells(HSVal, 1) & " " & Cells(HSVal, 2)
Next HSVal
It was an array that would concatenate column A and B, then the array would be output onto the worksheet in "P2:P250".
Sub SumData()
Const dFormula As String _
= "=IFERROR(SUMPRODUCT(--(P$2:P$250=I2),D$2:D$250,F$2:F$250),"""")"
With ThisWorkbook.Worksheets("Sheet1").Range("K2:K250")
.Formula = dFormula
.Value = .Value
End With
End Sub
This code does what I want to an extent but requires "P2:P250" to contain the array output, but I don't want to output anything onto the worksheet.
There's a piece of information that I do not understand here, how I can introduce those values from the array into the SUMPRODUCT (instead of "P2:P250"), even if its not by methods of array, as AFAIK I can't use that as a range without it being on the worksheet itself. Any idea?
Building an array with VBA then trying to use it with a formula in a worksheet without placing that information into a worksheet generally wouldn't work, however here you can use the source data in the formula instead:
=IFERROR(SUMPRODUCT(--(A$2:A$250&" "&B$2:B$250=I2),D$2:D$250,F$2:F$250),"""")
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.
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
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
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