Autofilter Criteria with Array - arrays

I have been looking to find a solution and cant find anything online that fully explains what is going on. I looked at some other posts but they all seem to fall a bit short.
When I run this bit of code it works perfectly (as it was recorded).
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, Criteria1:=Array( _
"73578", "78759", "78765"), Operator:=xlFilterValues
But then when I try to make it more robust it fails. I want to change the Criteria1 argument to an already stored array.
I am trying to get the following to work.
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=Array(StoredArray.Values), Operator:=xlFilterValues
I have the array stored and I manipulate it anyway but I have yet to get anything to work. I have also tried to create a string to be exactly like the recorded macro but that does not work either.
Dim StoredArrayString as Variant
StoredArrayString = "73578"", ""78759"", ""78765"
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=StoredArrayString, Operator:=xlFilterValues
Thanks for the help here I have spent lots of time on MSDN trying to research this issue but can't find a solution.

I think your issue is with how you're defining your array. Try this instead.
Dim StoredArrayString As Variant
StoredArrayString = Array("73578", "78759", "78765")
ActiveSheet.Range("$A$1:$AL$1002").AutoFilter Field:=17, _
Criteria1:=StoredArrayString, Operator:=xlFilterValues

Starting with:
and run:
Sub Macro7()
Dim ary(1 To 3) As String
ary(1) = "Alice"
ary(2) = "Boris"
ary(3) = "James"
ActiveSheet.Range("$A$1:$D$22").AutoFilter Field:=3, Criteria1:=ary, Operator:=xlFilterValues
End Sub
will produce:

Related

Syntax check fails on array as parameter to sub

I have two related problems. It seems like I don't have a library attached. I have searched and cannot find what I am missing. This is a call to a sub from a function that is called(referenced) in an Excel cell.
The syntax checker fails when I put an array in as a parameter like this which is taken from this VB Page
Sub EmptyValid210917 _
(RefData As String, RefRow As Integer, PhraseArray As String(,), _
DayIndex As Integer, EmptyCol As Boolean, ValidCol As Boolean)
The second related issue is the syntax check fails when I try to dim an array like this:
Dim ColOffset(,) As Integer
It will accept this:
Dim ColOffset(2, 2) As Integer
But does not accept loading as below which is taken from this VB Guide How To
Dim ColOffset(2, 2) As Integer = {{1,2},{3,4}}
Perhaps this is also related. When I call the max function. I get: Compile Error - Method or data member not found
FirstValidDay = Math.Max(1, 2)
Thank you for your help.

How to copy corresponding cell values from another workbook?

I want to copy cells of a certain colour in "Hacked" workbook to the "Official" workbook. I also want to loop across multiple sheets. Right now I am only testing on one sheet and the loop is already getting stuck.
Sub CopyBasel2()
Dim Hacked As Workbook
Set Hacked = Workbooks.Open("H:\BASEL Reporting - Oliver's Mock\Report Submission\BASEL2_0262CRT30062021G (Password Breaker).xls")
Dim Official As Workbook
Set Official = Workbooks.Open("H:\BASEL Reporting - Oliver's Mock\Report Submission\BASEL2_0262CRT30062021G.xls")
Dim Cell As Range
For Each Cell In Hacked.Sheets("SA-CR.1(CE)").UsedRange.Cells
If Cell.Interior.Color = 13434828 Then
Official.Sheets("SA-CR.1(CE)").Range(Cell.Address).Value = Cell.Value
End If
Next Cell
Debug.Print Hacked.Sheets("SA-CR.1(CE)").Range("C10").Interior.Color
End Sub
Thanks everyone for your guidance, I have managed to get my code to work as below, complete with a loop through an array of sheets.
The reason my earlier code couldn't work was because I was opening the "Official" file at the same time. When I closed it and ran my code, it ran smoothly. Anyone know the logic behind this?
Also, if anyone has a better/more elegant way of doing the array and the loops part, please feel free to share it.
Sub CopyBasel2()
Dim Hacked As Workbook
Set Hacked = Workbooks.Open("H:\BASEL Reporting - Oliver's Mock\Report Submission\BASEL2_0262CRT30062021G (Password Breaker).xls")
Dim Official As Workbook
Set Official = Workbooks.Open("H:\BASEL Reporting - Oliver's Mock\Report Submission\BASEL2_0262CRT30062021G.xls")
With Hacked
Set WSArray = .Sheets(Array("SA-CR.1(CE)", "SA-CR.2(CRM.1)", "SA-CR.3(CRM.2)", "SA-CR.4(RWA)", _
"SA-CR.6(OBS)", "SA-CR.6.1(CD)", "SA-CR.7(Recon)"))
End With
For Each ws In WSArray
For Each Cell In Hacked.Sheets(ws.Name).UsedRange
If Cell.Interior.Color = 13434828 Then
Official.Sheets(ws.Name).Range(Cell.Address).Value = Cell.Value
End If
Next Cell
Next ws
End Sub

Getting Run-Time error '1004' Is my formula perhaps too long or can I fix the coding?

So I wrote up an Array formula which works fine on my excel, but when converting it to VBA I'm getting an error: Unable to set the FormulaArray property of the Range class. I'm not sure why I'm getting this issue. Please see the code here:
Selection.FormulaArray = _
"=IF(IF(AND(OR(RC[-14]<>R[-1]C[-14],RC[-15]<>R[-1]C[-15]),Pay_Periods!R2C10<>0),MAX(IF(Pay_Periods!R2C3:R250C3>=Pay_Periods!R1C10+(14*Pay_Periods!R3C10),IF(Pay_Periods!R2C2:R250C2<=Pay_Periods!R1C10+(14*Pay_Periods!R3C10),Pay_Periods!R2C3:R250C3))),MAX(IF((Pay_Periods!R2C3:R250C3>=Sheet1!RC[-10])*(IF(AND(Sheet1!RC[-14]=Sheet1!R[-1]C[-14],Sheet1!R[-1]C[-10]<Sheet1!RC[" & _
"-10]+14),Pay_Periods!R2C3:R250C3<(RC[-10]+14),IF(AND(RC[-14]=R[-1]C[-14],RC[-15]=R[-1]C[-15]),Pay_Periods!R2C3:R250C3<R[-1]C[-10],1))),Pay_Periods!R2C3:R250C3,"""")))=0,"""",IF(AND(OR(RC[-14]<>R[-1]C[-14],RC[-15]<>R[-1]C[-15]),Pay_Periods!R2C10<>0),MAX(IF(Pay_Periods!R2C3:R250C3>=Pay_Periods!R1C10+(14*Pay_Periods!R3C10),IF(Pay_Periods!R2C2:R250C2<=Pay_Periods!R1C10+" & _
"(14*Pay_Periods!R3C10),Pay_Periods!R2C3:R250C3))),MAX(IF((Pay_Periods!R2C3:R250C3>=Sheet1!RC[-10])*(IF(AND(Sheet1!RC[-14]=Sheet1!R[-1]C[-14],Sheet1!RC[-15]=Sheet1!R[-1]C[-15],Sheet1!R[-1]C[-10]<Sheet1!RC[-10]+14),Pay_Periods!R2C3:R250C3<(RC[-10]+14),IF(AND(RC[-14]=R[-1]C[-14],RC[-15]=R[-1]C[-15]),Pay_Periods!R2C3:R250C3<R[-1]C[-10],1))),Pay_Periods!R2C3:R250C3,"""")" & _
")))" & _
""
By the way I copied it exactly how the macro itself recorded it so you may notice some spaces. I'm thinking it could just be too long, but it seems weird that writing it in the cell gets it fine, but not in the vba form. If anyone can help it would be much appreciated! I'm attempting to have this formula self insert by code and copy paste values so the excel doesn't have to keep loading in information.
Here is how the formula looks normally:
=IF(IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),IF(Pay_Periods!$B$2:$B$250<=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))),MAX(IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!G1<Sheet1!G2+14),Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,"")))=0,"",IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),IF(Pay_Periods!$B$2:$B$250<=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))),MAX(IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!B2=Sheet1!B1,Sheet1!G1<Sheet1!G2+14),Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,""))))
Then ctrl+shift+enter naturally
I went ahead and built a custom solution just for you. Here, you'll see I broke up your formula into smaller parts and substitute them back together (just like the good 'ol days back in algebra class).
Also note, that I don't use the Selection object as you do in your question. I recommend working with Ranges directly rather than using .Select, Selection. or .Activate and so on. So In my example below I assume the range you have "Selected" is A1 on the first worksheet.
strFormula = "=IF(IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(W_W),MAX(X_X))=0,""""," & _
"IF(AND(OR(C2<>C1,B2<>B1),Pay_Periods!$J$2<>0),MAX(Y_Y),MAX(Z_Z)))"
strFormulaW_W = "IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3)," & _
"IF(Pay_Periods!$B$2:$B$250<=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))"
strFormulaX_X = "IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!G1<Sheet1!G2+14)," & _
"Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,"""")"
strFormulaY_Y = "IF(Pay_Periods!$C$2:$C$250>=Pay_Periods!$J$1+(14*Pay_Periods!$J$3),IF(Pay_Periods!$B$2:$B$250<=" & _
"Pay_Periods!$J$1+(14*Pay_Periods!$J$3),Pay_Periods!$C$2:$C$250))"
strFormulaZ_Z = "IF((Pay_Periods!$C$2:$C$250>=Sheet1!G2)*(IF(AND(Sheet1!C2=Sheet1!C1,Sheet1!B2=Sheet1!B1," & _
"Sheet1!G1<Sheet1!G2+14),Pay_Periods!$C$2:$C$250<(G2+14),IF(AND(C2=C1,B2=B1),Pay_Periods!$C$2:$C$250<G1,1))),Pay_Periods!$C$2:$C$250,"""")"
With ThisWorkbook.Worksheets(1).Range("A1")
.FormulaArray = strFormula
.Replace "W_W", strFormulaW_W
.Replace "X_X", strFormulaX_X
.Replace "Y_Y", strFormulaY_Y
.Replace "Z_Z", strFormulaZ_Z
End With

Long Array VBA Issue

I'm trying to enter a long-array Formula into VBA that is >255 characters. I have followed past suggestions to dim both halves of the formula and merge them later. I am still having errors getting the array to function properly and was hoping someone could help review the code.
Here's the original code that exceeds the character limit I'm trying to get working:
Sub TestMacro()
Range("AZ7").Select
Selection.FormulaArray = _
"=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],[#Diameter],[#[Year Installed (Coating)]])=CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88]),HCA!R26C[-36]:R13642C[-36]))"
End Sub
Here is my latest attempt to split the code in half following past advice: https://www.mrexcel.com/forum/excel-questions/853889-long-array-visual-basic-applications-issue.html
http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/
Sub LongArrayFormula()
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String
theFormulaPart1 = "=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],[#Diameter],[#[Year Installed (Coating)]])""X_X_X)"")"
theFormulaPart2 = "=CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88]),HCA!R26C[-36]:R13642C[-36]))"
With ActiveSheet.Range("AZ7")
.FormulaArray = theFormulaPart1
.Replace """X_X_X)"")", theFormulaPart2
End With
Any help is appreciated, thanks.
You should keep your truncated formula syntactically correct. Try it like this:
theFormulaPart1 = "=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],
[#Diameter],[#[Year Installed (Coating)]])=X_X_X,HCA!R26C[-36]:R13642C[-36]))"
' ^^^^^
theFormulaPart2 = "CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88])"
With ActiveSheet.Range("AZ7")
.FormulaArray = theFormulaPart1
.Replace "X_X_X", theFormulaPart2
End With
Here I inserted X_X_X (could be anything else) in the place of some "closed expression" in the formula. That keeps the truncated formula correct from a syntax point of view, so the statement .FormulaArray = theFormulaPart1 can accept it. Replacement can then proceed in the second step.
You can also try this(
please don't run it from the VBE, try to run it from sheets environment. Go to Developer-Macros-Your Macro -Run or Run it from a button or shortcut and it will work without problem):
Range("AZ7").Select
Selection.Formula = _
"=SUM(IF(CONCATENATE(R3C3,[#Route],[#[Assumed Coating Type]],[#Diameter],[#[Year Installed (Coating)]])=CONCATENATE(HCA!R26C[86]:R13642C[86],HCA!R26C[-48]:R13642C[-48],HCA!R26C[87]:R13642C[87],HCA!R26C[-19]:R13642C[-19],HCA!R26C[88]:R13642C[88]),HCA!R26C[-36]:R13642C[-36]))"
SendKeys "{F2}"
SendKeys "^+{ENTER}"

Excel VBA - How to search array and return value?

I have some data that looks like this (starting in A1):
Batman
Carl Reiner
Ford
Sushi
But I have a two dimensional array, storing those names, and their type:
Dim Names()
ReDim Names(4,1)
Names(0,0) = "Batman"
Names(0,1) = "Superhero"
Names(1,0) = "Carl Reiner"
Names(1,1) = "Comedian"
Names(2,0) = "Ford"
Names(2,1) = "Car Manufacturer"
Names(3,0) = "Sushi"
Names(3,1) = "Food"
Now, I would like to loop through column A and replace those data points with their equivalent type. I thought there would be a relatively simple way to essentially search Names(x,0) for "Batman" then just replace it with Names(x,1). What's the best way to do so?
I found somewhere that you could possibly use Vlookup with this, so I tried this, to no avail:
Evaluate(WorksheetFunction.Vlookup("A1",Names,1,False)
With my thought being it would look for "Batman" and return the value in Names in the "1" dimension...But that keeps giving me an error (Type 13 Mismatch).
Thanks for any ideas/help!
edit: I found this post and seem to be getting somewhere with Application.Match("A1",Application.Index(Names,0,1),0) which returns the position of "A1" in the array. Now I just need to return the offset and get the "1" position of that...
Edit 2: I think I got it. Using MATCH to find the position of "A1" then just use the array to find it:
Names(Application.Match("A1",Application.Index(Names,0,1),0),1)
So, when A1 is Batman, it finds that position in the index, then it finds the equivalent return in the second dimension.
(Sorry for the lack of semantics, I don't have a very good idea of how to refer to this, so please let me know if I can clarify).
It seems like this would be easier using a Dictionary.
Sub testing()
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")
With dic
.Add "Batman", "Superhero"
.Add "Carl Reiner", "Comedian"
.Add "Ford", "Car Manufacturer"
End With
MsgBox "Batman is a " & dic.Item("Batman")
End Sub
I think if you want to use worksheet functions, you have to put the data into a worksheet (e.g. a column or pair of columns) instead of an array. This is pretty easy to do, though, and you can use a different worksheet, or even another workbook, and there are solutions to hide these, so your users don't have to see that data cluttering the other worksheets.

Resources