How to pass variables to an double match function in VBA - vba

I have a bunch of rows and 25 columns in a worksheet, and need to find the value in the 4th column based on columns B and C using VBA. I am using a combination of index and multiple condition match functions.
I tried to follow along via https://www.mrexcel.com/forum/showthread.php?650832-VBA-Multiple-Criteria-Index-Match and pass an integer variable into vba array formula to no avail.
I made this macro which works:
Sub VariablesInArrayFormula()
SA = "Apples"
C1 = "Oranges"
Range("D27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=b4)*(C2:C27= c6),0),4)"
Range("E27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)"
f = Evaluate("index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)")
Range("G27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27="" & SA & "")*(C2:C27= "" C1 ""),0),4)"
End Sub
I want to assign the value to a variable for future use.
When I assign it to D27, it works because the I refer to cell references b4 and c6.
Assigning it to cell E27 also works, but then I need to refer directly to Apples and Oranges, where as I would prefer to pass in a variables
assigning it to a variable f works when I pass in the words Apples and Oranges
when I attempt to pass a reference to Apples and Oranges (SA and C1 respectively), I receive a #N/A error.
Can anyone suggest a way that I can pass in the variables to this function.
NB I tried using worksheetfunction.index and worksheetfunction.match and kept receiving errors as well. Specifically, I tried:
gr4 = WorksheetFunction.Index(Range("A2:G27"), WorksheetFunction.Match(1, ((Range("B2:B27") = SA) * (Range("C2:C27") = C1)), 0), 4)
which returned a run time error #13: type mismatch.
Odd that using 2 matches failed, as when I use a single column to check with the match function worked
Sub vfhj()
SA = "Apples"
C1 = "Oranges"
gr3 = WorksheetFunction.Index(Range("C2:C27"), WorksheetFunction.Match(C1, Range("C2:C27"), 0))
End Sub

If I understand correctly, I think your syntax was just off slightly - you omitted some ampersands and overlooked the rule about doubling up the quotes. Also no need to Select.
Range("G27").FormulaArray = "=index(A2:G27,match(1,(B2:B27=""" & SA & """)*(C2:C27=""" & C1 & """),0),4)"

May be a bit quicker using memory arrays:
Sub VariablesInArrayFormula()
Dim SA As String
SA = "Apples"
Dim C1 As String
C1 = "Oranges"
With Worksheets("Sheet1") 'Change to your worksheet
Dim DtaArray As Variant
DtaArray = .Range("B2:D27").Value
Dim i As Long
For i = LBound(DtaArray, 1) To UBound(DtaArray, 1)
Dim ans
If DtaArray(i, 1) = SA And dtaaray(i, 2) = C1 Then
ans = DtaArray(i, 3)
Exit For
End If
Next i
.Range("G1").Value = ans
End With
End Sub

Related

How to pass dynamic variables to an equation in VBA [duplicate]

I have a bunch of rows and 25 columns in a worksheet, and need to find the value in the 4th column based on columns B and C using VBA. I am using a combination of index and multiple condition match functions.
I tried to follow along via https://www.mrexcel.com/forum/showthread.php?650832-VBA-Multiple-Criteria-Index-Match and pass an integer variable into vba array formula to no avail.
I made this macro which works:
Sub VariablesInArrayFormula()
SA = "Apples"
C1 = "Oranges"
Range("D27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=b4)*(C2:C27= c6),0),4)"
Range("E27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)"
f = Evaluate("index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)")
Range("G27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27="" & SA & "")*(C2:C27= "" C1 ""),0),4)"
End Sub
I want to assign the value to a variable for future use.
When I assign it to D27, it works because the I refer to cell references b4 and c6.
Assigning it to cell E27 also works, but then I need to refer directly to Apples and Oranges, where as I would prefer to pass in a variables
assigning it to a variable f works when I pass in the words Apples and Oranges
when I attempt to pass a reference to Apples and Oranges (SA and C1 respectively), I receive a #N/A error.
Can anyone suggest a way that I can pass in the variables to this function.
NB I tried using worksheetfunction.index and worksheetfunction.match and kept receiving errors as well. Specifically, I tried:
gr4 = WorksheetFunction.Index(Range("A2:G27"), WorksheetFunction.Match(1, ((Range("B2:B27") = SA) * (Range("C2:C27") = C1)), 0), 4)
which returned a run time error #13: type mismatch.
Odd that using 2 matches failed, as when I use a single column to check with the match function worked
Sub vfhj()
SA = "Apples"
C1 = "Oranges"
gr3 = WorksheetFunction.Index(Range("C2:C27"), WorksheetFunction.Match(C1, Range("C2:C27"), 0))
End Sub
If I understand correctly, I think your syntax was just off slightly - you omitted some ampersands and overlooked the rule about doubling up the quotes. Also no need to Select.
Range("G27").FormulaArray = "=index(A2:G27,match(1,(B2:B27=""" & SA & """)*(C2:C27=""" & C1 & """),0),4)"
May be a bit quicker using memory arrays:
Sub VariablesInArrayFormula()
Dim SA As String
SA = "Apples"
Dim C1 As String
C1 = "Oranges"
With Worksheets("Sheet1") 'Change to your worksheet
Dim DtaArray As Variant
DtaArray = .Range("B2:D27").Value
Dim i As Long
For i = LBound(DtaArray, 1) To UBound(DtaArray, 1)
Dim ans
If DtaArray(i, 1) = SA And dtaaray(i, 2) = C1 Then
ans = DtaArray(i, 3)
Exit For
End If
Next i
.Range("G1").Value = ans
End With
End Sub

Multiple Condition

I have "object doesn't support this method" error when trying to do a comparison of column cross sheets. . If both column A and B in sheet 1 matches both column A and B in sheet 2, it will display the match in sheet 3.
There were a number of issues in your code. In the future, please post the actual code (not screenshot).
Watch out for "And" vs "&" in your if statement.
".value" not ".values" in your vars.
"Dim as string" since we're working with the cell.
"Worksheet." not "Worksheets."
I don't think you need a "Set" for these. (could be wrong)
Try the code below, it works for me. You may need to modify the lines writing to the "match" sheet.
Sub find()
Dim a As String
Dim b As String
Dim c As String
Dim d As String
a = Worksheets("sheet1").Range("a1").Value
b = Worksheets("sheet2").Range("a1").Value
c = Worksheets("sheet1").Range("b1").Value
d = Worksheets("sheet2").Range("b1").Value
If a = b And c = d Then
Worksheets("match").Range("A65536").End(xlUp).Offset(1, 0).Value = c
Worksheets("match").Range("A65536").End(xlUp).Offset(0, 1).Value = c
End If
End Sub

Writing a loop in Excel for Visual Basic

How do i write the following code as a loop. I want to copy values from a table in sheet 4 in a row from range (b:17:L17"). is there a more efficient way to do it with loops ?
ActiveSheet.Range("B17").Value = Sheets(4).Range("G8")
ActiveSheet.Range("C17").Value = Sheets(4).Range("G9")
ActiveSheet.Range("D17").Value = Sheets(4).Range("G10")
ActiveSheet.Range("E17").Value = Sheets(4).Range("G11")
ActiveSheet.Range("F17").Value = Sheets(4).Range("G12")
ActiveSheet.Range("G17").Value = Sheets(4).Range("G13")
ActiveSheet.Range("H17").Value = Sheets(4).Range("G14")
ActiveSheet.Range("I17").Value = Sheets(4).Range("G15")
ActiveSheet.Range("J17").Value = Sheets(4).Range("G16")
ActiveSheet.Range("K17").Value = Sheets(4).Range("G17")
ActiveSheet.Range("L17").Value = Sheets(4).Range("G18")
Yes, there is:
ActiveSheet.Range("B17:L17").Value = Application.Transpose(Sheets(4).Range("G8:G18").Value)
You can, using something like this (VB.Net, but may copy easily to VBA):
Dim cell as Integer, c as Integer
cell = 8
For c = 66 To 76
ActiveSheet.Range(Chr(c) & "17").Value = Sheets(4).Range("G" & cell)
cell = cell + 1
Next
The Chr() function gets the character associated with the character code (66-76), and then this value is concatenated with the string "17" to form a complete cell name ("B17", "C17", ...)
I am also incrementing the cell number for G at the same time.
Use this if you really want to use a loop - but there could be better ways, like the answer given by #A.S.H
Solution explanation:
Establish your rules! What is changing in the range for active sheet? The column is going to grow as the for/to cycle does! So, we should sum that to it. What is the another thing that is going to increment? The Range in the other side of the '=' so, by setting an algorithm, we may say that the row is const in the Activesheet range and the column is the on variable on the other side.
Solution:
Sub Test()
Const TotalInteractions As Long = 11
Dim CounterInteractions As Long
For CounterInteractions = 1 To TotalInteractions
'where 1 is column A so when it starts the cycle would be B,C and so on
'where 7 is the row to start so when it begins it would became 8,9 and so on for column G
ActiveSheet.Cells(17, 1 + CounterInteractions).Value = Sheets(4).Cells(7 + CounterInteractions, 7)
Next CounterInteractions
End Sub
This is probably your most efficient solution in a with statement:
Sub LoopExample()
Sheets("Sheet4").Range("G8:G18").Copy
Sheets("Sheet2").Range("B17").PasteSpecial xlPasteValues, Transpose:=True
End Sub

How do I use a string as a variable in vba?

This is what my cells look like:
This is my code, I'll explain it below.
Sub Macro1()
Dim product as String
Dim group as Long
Dim recordno as Long
dim pol_number as Long
dim plan_name as Long
product = "corp"
group = 1
recordno = 1
pol_number = 1
plan_name = "TTT"
Range("A2").Select
For i = 1 to 5
ActiveCell.Value = Selection.End(xlUp).Value
ActiveCell.Offset(0,1).Select
Next i
End Sub
I want to fill in all of the cells with the variable values. I understand that variables are not case sensitive, and I understand that the code I have will just fill the cell with the text in the upmost cell of the column, but I don't know if there is a function that would take the text of the top cell and convert it to a variable. Is that possible?
Try this to go from variables to cells
Dim values as Variant
'Array 0 to 4
values = Array(product,group,recordno,pol_number,plan_name)
Range("A2").Resize(1,5).Value2 = values
The reverse is
Dim values as Variant
'Array 1 to 5
values = Range("A2").Resize(1,5).Value2
product = values(1,1)
group = values(1,2)
recordno = values(1,3)
pol_number = values(1,4)
plan_name = values(1,5)
If you do something like
someCell.Value = someOtherCell.Value
and someOtherCell.Value is "product" then someCell won't be filled with what you have saved in the variable product but with "product" (I included the quotation marks to emphasize that's it's a string). That's a good thing because otherwise it would mess your code up if you accidentally put in the name of some random variable in your code.
If your requirements are like this:
You have values for PRODUCT etc that you write to write in the row below PRODUCT etc.
The headers are not always in the same order.
You might want to add new variables later on without too much fuss.
Them some kind of keyed list might be what your looking for. That means that rather than referencing the variable by a numerical index, you can reference them using names.
If the order is fixed, you might be better of just using an array where item 1 is the product name, item 2 is the group number etc, like ja72 and Sgdva suggested.
However, if you still want to reference the variables by name, you could use a collection:
Dim coll As New Collection
With coll
.Add "corp", "product"
.Add 1, "group"
.Add 1, "recordno"
'...
End With
Then instead of selecting cells and referencing ActiveCell you should reference the cells directly (using selections and ActiveCell can be avoided most of the times and slows down the macro and can even cause unnecessary errors)
For i = 1 To 5
Cells(2, i).value = coll(Cells(1, i).value)
Next i
An alternative to a collection is a dictionary which offers an easy way to check if a key exists (with a collection you have to catch the error)
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
With dict
.Add "product", "corp"
.Add "group", 1
.Add "recordno", 1
'...
End With
Now you can check if the entry exists first so it won't throw an error:
For i = 1 To 5
If dict.Exists(LCase(Cells(1, i).value)) Then 'note that the dictionary keys are case sensitive
Cells(2, i).value = dict(LCase(Cells(1, i).value))
Else
MsgBox "Entry for " & LCase(Cells(1, i).value) & " not found!"
End If
Next i
Note that when you use dict("somekey") and the entry "somekey" doesn't exist, it won't throw an error but add an empty entry.
Why not an array and then loop through the elements as needed?
Dim ArrayTitles() As Variant 'since strings and numbers are mixed
ReDim Preserve ArrayTitles(5)
ArrayTitles(1) = "corp"
ArrayTitles(2) = 1
ArrayTitles(3) = 1
ArrayTitles(4) = 1
ArrayTitles(5) = "TTT"
Range("A2").Select
For i = 1 To 5
MsgBox (ArrayTitles(i))
I'm thinking what you are trying to accomplish can be solved in this way
for j = 1 to 6 'Or whatever your last column happens to be
if UCase(cells(1, j)) = "PRODUCT" then
if ActiveCell.Column = j then
ActiveCell.Value = "corp"
end if
end if
next j
Something like that?

Speed up declaring variables?

I have a bunch of Variables I need to declare and was wondering if there's any way to shorten the amount of lines needed to do so. Here's the code:
Sub test()
dim comps as New Collection
dim noOfCompanies as Integer: noOfCompanies = 25
dim c1 as New Names 'Names is a class I have made
dim c2 as New Names
... ' in this gap is c3 to c29
dim c30 as New Names
End Sub
I don't know that you can create a variable and do something like the following, can you? (Note: Psuedocode)
dim i as Integer
for i = 1 to 30
Dim "c" & i as New Names
next i
edit:
#rene mentioned using an array - how would I do so, if later I'm going to set parts of the class properties (sorry, I'm learning classes and don't know the proper terms):
c1.companyCode = 10: c1.companyCountry = "USA": c1.companyName = "Batman LTD"
c2.companyCode = 13: c2.companyCountry = "Krypton": c2.companyName = "Superman LLC"
... 'etc until c30.
Here's what I'm trying so far, but to no avail:
Dim tempC As String, tempN As String
For i = 1 To noOfCompanies
c(i) = "c" & i
tempC = c(i)
Debug.Print tempC 'This will correctly print "c1", "c2", "c3", etc.
Dim c(i) As New Names 'This is where I can't figure out how to declare the different array parts as an individual "new Names" class part.
Debug.Print tempN
Next i
edit2:
Here's why I'm trying to create 30 variables. I get a spreadsheet every week that has a column of codes (the codes being that companyCode I am initializing above). If I find a row with any of the 30 codes I am trying to declare, then I need the companyName and companyCountry to be placed in some other cells on that row. My idea was to be able to just do something like this (psuedocode):
dim rng as Range
rng = Range("A1:A30") 'this has the codes in it, i.e. 13, 10, 11, 20...
for each cel in Rng
'here would be code where I just check for IF the cel.Value is anywhere in companyCode,
'return its equivalent companyCountry and companyName
next cel
So, would a dictionary be best? I could do like
if dict.exists(cel.value)
BUT how could I store the companyCountry and companyName in the same dictionary entry, AFAIK I can only store one key per entry?
...of course, if just saving this info in an excel table somewhere (xlsx or csv) and just opening/using that then closing would be best practice, just let me know!
Dim arrNames(1 to 30) as Names, n
for n=1 to 30
Set arrNames(n)=new Names
next n
arrNames(5).companyCountry = "USA"
EDIT: I think storing your code information on a worksheet and accessing it directly is the "best" approach unless you need high-volume/high-performance lookups (even then it will not be bad...)
For example here's a pretty simple function you can call from VBA:
Function CompanyInfo(companyCode, infoType As String)
Dim rng As Range, colNum As Long, rv
Select Case infoType
Case "Country": colNum = 2
Case "Name": colNum = 3
Case Else
CompanyInfo = "InfoType?"
Exit Function
End Select
rv = Application.VLookup(companyCode, _
ThisWorkbook.Sheets("Codes").Range("A2:C100"), _
colNum, False)
CompanyInfo = IIf(IsError(rv), "???", rv)
End Function
Usage:
Dim v, v2
v = CompanyInfo(10,"Country")
v2 = CompanyInfo(10,"Name")
Example using a collection to create 30 instances of a class containing the name.
If it is imperative that they be able to be retrieved using "c1-c30", then you can either use that as a variable in the class (like Name) or as the collection index/key.
For example:
Names Class:
Private pName As String
Private pOther As Integer
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(Value As String)
pName = Value
End Property
Assigning and Printing our 30 Names:
Sub Test()
Dim MyNames As Collection
Set MyNames = New Collection
Dim x
For x = 1 To 30
Dim t As Names
Set t = New Names
t.Name = "c" & x
MyNames.Add t
Next x
Dim y
For Each y In MyNames
MsgBox (y.Name)
Next y
End Sub
In closing, I think your problem is that you want to be able to reference these 30 cnames in your code by name later after having assigned them. That's not going to work and it's a bad coding practice. You shouldn't do:
Dim c1
Set c1 = new Names
c1.Name = "Bob"
Dim c2 '...
There's a reason people don't typically declare 30 variables with incremental numbers. The reason is because there is a better way. That way is typically using a collection of variable types or an array of variable types that you can reference using an index or a loop.
If you're creating 30 instances of a certain data type, and you want to give them each unique values, create a table or even a static array to hold their values and assign them in a loop.
To follow up, if you want to reference them using c & x then add a variable to your class called ID and assign to that.
You might want to look into using a dictionary if you would like to be able to quickly retrieve the ID without looping through and checking ID's.
Edit:
I'm glad you explained your end game. You are absolutely over-complicating this scenario.
A simple VLOOKUP formula and a lookup table would save you from having to code anything in VBA at all.
Example:
Create a named range called LookupTable that contains the company ID's on the far left:
Then, use these formulas to search your table for the ID, and give you the name/location.
Parameter 1 is the value to Lookup
Parameter 2 is our LookupTable
Parameter 3 is the column from our table to return
(1 = ID, 2 = Company Name, 3 = City)
Parameter 4 says we want an exact match only.
=VLOOKUP(A1,LookupTable,2,FALSE)
I'm not sure if I like the use of "Names" as a class name since "Names" already has an Excel VBA meaning, but if that's what you want.
As others have pointed out, an array is probably the way to go. But if you really want to have 30 variables and you don't want to do a lot of typing, you can do something like this:
Sub DeclareVars()
Dim i As Long, v As Variant
ReDim v(1 To 30)
For i = 1 To 30
v(i) = "c" & i & " As New Names"
Next i
Debug.Print "Dim " & Join(v, ", ")
End Sub
Run it once and copy the result from the immediate window into your code. If you know Python you can use a 1-liner in the Python shell and type even less. Just evaluate:
"Dim " + ", ".join('c' + str(i) + " As New Names" for i in range(1,31))
Why don't you store your c1, ... c30 objects properties in a table, an xml file, a csv file, or any other of the multiple types of files? That can store data and be read via VBA.
So, when needed, you can just open the table, and populate an array of your object's properties with the values in the table? If your table/file contains 30 lines, an array of 30 objects will then be created.
By doing this, you will also separate your code from your data, which is usually considered as a best practise.