VBA Drag down Vlookup - vba

All,
I have a data sheet of around 1.000 values which need a matching amount (the amount of valueschanges every day) . These amounts can be found in another tab "Data".
so using a Vlookup code in VBA should help me. The code I'm using is:
Sheets("Data").Range("E2") = Application.WorksheetFunction.VLookup(Sheets("Data").Range("D2"), Sheets("Blocked").Range("C:D"), 2, False)
Result should appear in column E responding with the row of the lookup_value which can be found in column D.The table and column index don't change being Sheets("Blocked").Range("C:D")and 2
This code gives me the result I wanted but as I tried to drag down the formula with this function:
Range("E2").AutoFill Destination:=Range("E2:E440")
How can I drag down this formula without needing to create a seperate vlookup for each row?

You should use the Formula as an R1C1 reference and then u will be able to drag down
Sheets("Data").Range("E2").FormulaR1C1 = "=VLookup('Data'!R[-1]C,'Blocked'!C[-2]:C[-1],2,0)"

Related

Find number of rows in an already filtered Column A in Excel

I have got an Excel spreadsheet. This spreadsheet has just one tab in it. The Tab name is dynamic in nature and changes every week also the number of rows.
I have column A filtered already with a VBA macro. A1 has the header.
Now, I wanna find how many rows are there in this already filtered column A.
I am looking for any VBA function.
I have tried using Subtotal function.
=Subtotal(103,A2:A1345)
But I don't know the end range. As of now the end range is A1345. It will change every time in future if the new rows are added.
I tried multiple things but those did not work. I am quite new to VBA.
If A1 will never be blank, you could use (in a column other than A)
=Subtotal(103,A:A)-1.
Or, if there will be data below your table not to be counted, then format your table as a Table and use structured references (and this formula could go into column A)
=SUBTOTAL(103,Table1[column_header])
You can put the formula in column A if you use another column's last populated cell as the demarcation point.
If column B contains numbers then,
=subtotal(103, a2:index(a:a, match(1e99, b:b)))
If column B contains text then,
=subtotal(103, a2:index(a:a, match("zzz", b:b)))

How to match multiple columns and get values in Excel [duplicate]

This question already has answers here:
Vlookup using 2 columns to reference another
(2 answers)
Closed 5 years ago.
I need to match Column A and Column B values in Sheet 1 with Column A and Column B values in Sheet 2. If both are same then Copy C values from Sheet 2, and paste in Sheet 1 in C. I will enter values manually in sheet 2 Column C.Here each country will have 2 or more Number. So, both Column A and Column B must match.
I used the formula below. But not working. Most of the Column A and B values are not in order. Help me
=INDEX(Sheet2!$C:$C; MATCH(Sheet1!$A2:B2; Sheet2!$A:$B; 0);COLUMNS($A:B))
You can use the following formula to return what you're looking for. It is an array formula so will need to be entered with Ctrl+Shift+Enter
=INDEX(Sheet2!$C$2:$C$22; MATCH(1; (Sheet2!$A$2:$A$22=Sheet1!$A2)*(Sheet2!$B$2:$B$22=Sheet1!$B2);0))
As it is an array formula I recommend defining your ranges from beginning to end instead of just selecting the whole column. Non-array formulas Excel actively finds the beginning and end of the range and only calculates that subset; however, with array formulas it considers the whole range (even if there's nothing in it) so it can suddenly take a very long time even when there isn't much being calculated
Use the following as a matrix formula:
=INDEX(Sheet2!$C:$C; MATCH(Sheet1!$A2&$B2; Sheet2!$A&$B; 0))
Paste this into cell C2:
=INDEX(C2:C22,MATCH(G2&H2,A2:A22&B2:B22,0))
and use Ctrl+Shift+Enter instead of Enter since it's an array formula.
Then copy that cell as many rows down as needed.
Here are some more examples.

Macro sum only filtered cells in table and copy to clipboard

I'm trying to do some data entry and was wondering if I could copy the sum of FILTERED data onto clipboard with a macro?
I have a macro that copies sum of selected cells, but when in a table format and filtering it seems to sum everything in that range too. Please let me know.
There are 1665 Rows total in my data table starting at 22.
Thanks!
No need to really use a macro, you can use the SUMIFS function https://support.office.com/en-in/article/SUMIFS-function-9dd6179e-cced-41dd-ac38-08fdf5b929e5 Basically you will use the criteria for filtering as the criteria for summing the values. I am not sure what your filtering criteria is, but let's you are filtering value that are great than 0 in column A rows 1 to 100.
SUMIFS(A1:A100, A1:A100, ">0")
Right click on the table and click Table->Totals Row to show a row at the bottom of the table for totals. It will only sum the visible rows of that column.

Excel VBA - selecting the range from first row to the last row

I have a problem with VBA code. I have a sheet with a lot of data around the cells I want. I need to select data in column F, but the position of the first and last cells between the range is to be selected is changing up and down. I created the non empty cells in row X where there is no data do the LastRow function has any refernece but now I dont know how to select the first row
thx for help
If F1 is empty, this selects the first cell with data in the F column
Worksheets("Sheet1").Range("F1").End(xlDown).Select
If F1 may be non-empty you can add a check to see whether it contains text.

Get Unique data from excel by button click?

I am new to excel macros. need some help from you.I have a excel file with 2 sheets.
in sheet one i have some data
In second sheet i need a button which will fetch the distinct C column(Mname) and their ID.
Like most Excel problems, this can be done with a macro, but could also be done with just formulas. Macros are often more trouble than they're worth, so here's the formula option. There are some interesting downsides to using formulas in this way, mainly that if you do too much in a single workbook, calculation will be very slow. But as long as you're dealing with less than a few thousand rows, it shouldn't be a problem.
On your primary worksheet, enter this formula in D2, and then drag it to auto-fill down however many rows you forsee using. When you're done, you can hide column D, if it suits you.
=IF(COUNTIF($C:$C,$C2)<2,MAX($D$1:$D1)+1,0)
The COUNTIF formula looks for how many elements in column C match the contents of $C2 (where $C2 is a (partially) relative reference, so the row number will change when you auto-fill into other rows). If the count is less than 2 (the element in $C2 is unique), then the IF formula returns an index number: 1 for the first unique element in the column, 2 for the second, so on. All cells in column D where the corresponding cell in C is not unique are filled with 0.
On your second sheet, you will fill columns A and B with 'lookup' formulas, searching for positive values in column D of the primary sheet. Most people use VLOOKUP() for lookups, I prefer a combination of INDEX() and MATCH(). And here's a great example of why, since our 'unique index' is not in the first column of the primary sheet (so VLOOKUP wouldn't work!).
=IFERROR(INDEX('Primary Sheet'!$A:$A,MATCH(ROW(A2)-1,'Primary Sheet'!$D:$D,0)),"")
and for the 'Mname' column of the second sheet,
=IFERROR(INDEX('Primary Sheet'!$C:$C,MATCH(ROW(B2)-1,'Primary Sheet'!$D:$D,0)),"")
These lookup formulas are based on their own row number. So the formula in cell A2 is looking for the row in column D of Primary Sheet that contains the unique index 2-1=1. The formula in A3 looks for the index 3-1=2, etc. The IFERROR() formula that is wrapped around the lookup formula ensures that if the sought-after index is not found, the formula returns an empty string (""), which looks like a blank cell. This way you can prepare a large number of rows on your secondary sheet with these lookup formulas. And if there are only a small number of unique Mnames on Primary Sheet, your secondary sheet won't have columns full of #N/A errors.