VBA Autofilter Using Multiple Criteria - vba

All,
I am trying to filter on multiple criteria within VBA.
However I cannot find a simple way of doing this. The criteria I am selecting will always be constant but greater than 2 therefore I cannot use the or operator.
Code below;
Selection.AutoFilter field:=10, Criteria1:=Array("Fixtures & Fittings", "Furniture & Equipment", "Land & Buildings", "Motor Vehicles", "Plant & Machinery")
My current solution only filters on the last criteria within the array.
Due to the file set up I am unable to insert a formula in another column.
Thanks

You need Operator:=xlFilterValues.
Selection.AutoFilter field:=10, Operator:=xlFilterValues, Criteria1:=Array("Fixtures & Fittings", "Furniture & Equipment", "Land & Buildings", "Motor Vehicles", "Plant & Machinery")
If the criteria is constant, I would assume that the filter range is consistent as well. You may wish to move away from using Selection in the near future. See How to avoid using Select in Excel VBA.

Related

Set any range with userform

I am currently looking for a way to assign a range via UserForm. I've often encountered the example with the RefEdit, which unfortunately does not fit 100% to my problem.
Is there a way where the enduser can assigns a range with a textbox? The User should be asked in which column in his sheet the relevant numerical values are and on that basis, the macro should adjust the range.
For example:
The user types the range is in column H and there for the macro adjust to:
ws.Range("H2: H" & lastrow)
ws.Range(Var & "2:" & Var & lastrow)

1004 error when trying to run a macro that applies to a table

I have an existing table. I want to add a new column next to it, with a formula in it so that the formula adds a new column and fills in all the values.
This is a two-page worksheet. Table1 is on another page than the table we're working with, but has the master data it draws from.
I recorded a macro of the formula I used to produce this, but when I try to run it, I get
Run time error '1004': Application-defined or object-defined error.
The code I'm using is below:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for sale"",""seller""," & _
"IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for lease"",""landlord""," & _
"IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for sale or lease"",""seller / landlord""" & _
"X(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Base Rent/Mo"",Table1[#Headers],0))>0,""landlord"",""seller""))))"
Range("B1").Select
Things I've checked so far:
All the names match (i.e. Table1 is a valid name, and all the headers are correctly named)
The formula works exacly as I want to if I just type it in and hit 'enter'
I've looked for similar issues here, and the most similar seem to be some problems with Pivot tables, but the solutions don't seem to be applicable to my problem
(The reason I'm using VBA for this is that this is one part of a multi-step process that I'm trying to automate to make it simpler to run a complicated report from a large set of data.)
EDIT: Additional fixes I tried:
Moving the master data table to the same sheet as my 'target' table to see if it would work if the two were on the same worksheet. No go.
When you need to use quotes inside quotes like in "MATCH(""Status"", which I suppose you are trying to output Match("Status" try using "MATCH("&Chr(34)&Status&Chr(34)
Chr(34) outputs the " symbol. Otherwise it would return MATCH(Status (without quotes)
There were two separate issues.
First, I was recording the macro and apparently, Excel will not record formulas longer than a certain length properly. This limit is greater than 407 (the longest formula I got to auto capture), but shorter than 467 characters. A similar problem was discussed on this Mr. Excel post. This also was why part of the "INDEX" was missing, as BruceWayne pointed out.
In addition, it was necessary to switch from .FormulaR1C1 to .Formula to get it to work correctly, as R3uK had suggested.
The final code ended up as follows:
Range("B2").Select
ActiveCell.Formula = _
"=IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for sale"",""seller"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for lease"",""landlord"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for sale or lease"",""seller / landlord"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Base Rent/Mo"",Table1[#Headers],0))>0,""landlord"",""seller""))))"
Range("B3").Select

How do I link to another sheet in Excel with VBA?

I'm trying to generate a list of hyperlinks in excel and link them dynamically to another cell in a different sheet. Could someone explain how I need to format the reference? Currently, it looks like this:
'p2r is one sheet
'sh is another
'Subaddress is currently linking to the correct location, wrong sheet.
'(It's linking to [p2r]'s cells, not [sh]'s cells)
p2r.Hyperlinks.Add_
Anchor:=p2r.Cells(p2rIndex, 1), _
Address:="", _
SubAddress:=sh.Cells(round2, 2).Address, _
TextToDisplay:=PrevRow
Thanks for the help! I've seen others use a format like: Sheet!A1, but when I tried something like:
SubAddress:=sh & "!" & Cells(round2, 2).Address
I got no results of value.
There are two modifications you could use to get your code working:
You could use
SubAddress:="'" & Replace(sh.Name, "'", "''") & "'!" & Cells(round2, 2).Address
in order to generate something of the form SubAddress:="'Sheet name'!$A$1".
(The bits in the formula dealing with 's are to allow it to still work if there are spaces, etc, in the sheet name. If you know that won't occur, e.g. because you defined the sheet names and no-one else will be changing them, you could simplify it to SubAddress:=sh.Name & "!" & Cells(round2, 2).Address)
You could use
SubAddress:=sh.Cells(round2, 2).Address(External:=True)
which would generate something of the form SubAddress:="'[Book1.xlsx]Sheet name'!$A$1"

Declaring last row references in Excel formulas through VBA

In an attempt to take one of my rudimentary programs, and develop it more I am in need of help referencing last row references in Excel formulas as well as dragging formulas down consistently to the last row of a document.
To elaborate I am attempting to reference the last row in an ever expanding mapping table in my vlookup formula. The reason I need this is because as this mapping table expands when I am no longer coding this program, I need my vlookup formula which will be generated every time I run the program to adapt to the ever changing size.
Also my more pressing issue is in regard to taking that vlookup formula and being able to drag it down to the last row of a worksheet. The worksheet will be static and the last row will range anywhere from 70,000 rows to 90,000 rows. I am trying to avoid loops in this scenario as this will already be a very demanding formula and I would hate to loop through each row.
Currently my rudimentary code looks like this (this was built as a proof of concept, I understand the current method isn't the most ingenious but it served its initial purpose).
Ath.Cells(1, x) = "Business"
Ath.Cells(2, x).Select
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],Mapping!R2C4:R264C5,2,0),VLOOKUP(LEFT(RC[1],2),Mapping!R1C3:R264C5,3,0))"
Ath.Range("d2").Copy
Range("d2:d90000").Select
ActiveSheet.Paste
Calculate
I namely want to change this
Ath.Range("d2").Copy
Range("d2:d90000").Select
ActiveSheet.Paste
and
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],Mapping!R2C4:R264C5,2,0),VLOOKUP(LEFT(RC[1],2),Mapping!R1C3:R264C5,3,0))"
Perhaps these?
Ath.Range("d2").Copy
Range("d2:d" & Range("D" & Rows.Count).End(xlUp).Row).Paste
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],Mapping!R2C4:R" & Sheets("Mapping").Range(ActiveCell.Offset(Rows.Count, -1)).End(xlUp).Row & "C5,2,0),VLOOKUP(LEFT(RC[1],2),Mapping!R1C3:R" & Sheets("Mapping").Range(ActiveCell.Offset(Rows.Count, -1)).End(xlUp).Row & "C5,3,0))"
Combining Dans answer with work of my own I have developed a solution.
Sub Formulas()
Dim Map As Worksheet
Dim Ath As Worksheet
Dim last As Long
Set Ath = Sheets("Athena IBT TEST")
Set Map = Sheets("Mapping")
last = Map.Cells(Rows.Count, "D").End(xlUp).Row
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],Mapping!R1C1:R" & last & "C4,4,0),IFERROR(VLOOKUP(LEFT(RC[1],3),Mapping!R1C2:R" & last & "C4,3,0),VLOOKUP(LEFT(RC[1],3),Mapping!R1C3:R" & last & "C4,2,0)))"
End Sub

repost: copy pasting data instead of formula excel VBA

ok i have to repost this as my original thread was tagged as a duplicate with "the original" thread not having anything to do with my issue and the person who tagged it as a dupe cannot provide the answer.
there's a reason why i'm using activecell in my code as it does something with interaction with outlook and validation of certain conditions from other cells.
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1)
now i tried :
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1).pastespecial
but i'm getting error 1004.
if someone will point me here: how to paste only values in excel
please tell me which of the 4 answers will actually work, because none of them is working on my end.
Reverse the procedure and pass over the value directly.
wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1) = ActiveCell.Cells(1, 37).value
The .Value is the raw underlying value in a cell or the result from a formula if the cell contains a formula. .Value2 strips the value further where a date like 18-Feb-2015 becomes 42,053. If you have used number formatting in the cell (e.g. 18-Feb-2015) and wish to retain it, use .Text in place of .Value.