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

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)))

Related

How to combine INDEX, IFERROR and VLOOKUP formulas

I would like to Index a columns info, run a vlookup of a particular text(s) and then perform an if error to ignore things not like the "lookup value". I have data in column V called "Biscuits" amongst other items of different names. "Biscuits" has to be replaced with a different name. But the name change will not be the same for each occurrence. I would like to maintain the original column V's data and Index that particular columns values into another column. Once I have created a "copy" of the original data named column AV, I then need to run a vlookup on column AV. I have a column AW that has manual information of different names inputted in this column. In the "Indexed" Column AV I would like to run vlookup on "Biscuits" ONLY, skipping other entries and replace "Biscuits" with the contents of column AW (all entries in Column AW for name changes are on the same lines as "Biscuits"
Example
Original Column, Indexed Column, Manual input column and final result
=INDEX(V2, ,)-this is the formula I wrote to Index column V, thus creating column AV
=IFERROR(VLOOKUP("Biscuits",V2:AW2,28,FALSE),"")-This is a formula I wrote to look for "Biscuits" and replace it with the contents of column AW
Please help on this.
Thank you!
According to OP It worked!, the formula being:
=IF(AW2="",V2,AW2)
Ie return the contents of V2 if AW2 is empty and the contents of AW2 otherwise. This can be copied down to apply to other rows, with references adjusting automatically. Neither the 'index column' nor any of INDEX, IFERROR or VLOOKUP were required.

Creating a Dynamic Hyperlink in excel

I have three columns of data (columns A, B, and C).
Column A contains a number
Column B contains a name
Column C contains a URL
In cell E1, I have a drop down list that references the cells in column A.
I need a function that generates a hyperlink based on the number picked from the drop down menu using the cells that are adjacent in columns B and C. In otherwords, if E1 = any cell in column A, then create a hyperlink with the adjacent cells in columns B and C... more generally:
=IF(E1=[Any cell in column A],HYPERLINK([same row column C],[same row column B],"")
Is it possible to create a dynamic formula that will do this? I have found similar questions, but none that ask precisely this, and I cannot seem to piece together my searches to come up with a solution.
thanks to any who help.
enter image description here
I hope you looking for like this
formula:
=HYPERLINK(VLOOKUP(E1;A1:C5;3;0);VLOOKUP(E1;A1:C5;3;0))
I believe (if i'm mentally imagining your spreadsheet right) that you'd be better off using the VLookup function to retrieve the URL. If i'm wrong and it's reversed, the HLookup function may be what you need. They're similar, just depending on the pivot of whether you're aiming for a value in a row (Vlookup) or Column (Hlookup).
This article does a pretty good job of explaining HLookup:
https://support.office.com/en-us/article/VLOOKUP-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1
You can then use the lookup value for the HYPERLINK function to set the display text and the URL.

Dynamic reference in excel formula

I have the following array formula which works for what I want to do but I'm trying to change the formula when a user selects a value.
=INDEX($A$2:$B$70,SMALL(IF($A$2:$B$70=$A$121,ROW($A$2:$B$70)),ROW(1:1))-1,1)
It's used for a monthly report and the user will choose from a drop down the day of the month, e.g 1,2,3 - 31.
So if the user selects 1 from the drop down menu I want the formula to use the above formula.
If they select 2 for example I want the formula to move over a column so it would change to
=INDEX($A$2:$C$70,SMALL(IF($A$2:$C$70=$A$121,ROW($A$2:$C$70)),ROW(1:1))-1,1)
and so on moving over a column at a time.
It this possible at all or can it even be done without VBA?
I have an example of what I want done on the following link
https://docs.google.com/spreadsheets/d/1MDOzoQxYLgW-UOyljZsMwSu8zyAB7O2k1V-bTNP5_F0/edit?usp=sharing
All the data is on the first tab called staff. Each employee has a row and the duty assigned under the corresponding day column.
On the Roster tab it summarises each day. So what I am trying to get to happen is when you choose the day of the month (or preferably the actual date) the sheet changes to reflect the data.
At the moment the code I have working does for just Day 1 because the column references are coded into the formula. I was hoping to somehow choose 6 for example from the drop down and then the formula will map chosen day to the corresponding range in the raw data and update the formula and change the formula from Staff!$A$2:$B$68 to Staff!$A$2:$G$68.
If the formula finds no more entries if shows #NUM! but I intended to use the function ISERROR() to replace #NUM! with "".
This is what I'm trying to achieve it if makes sense?
There are a few issues here/ You are returning the value from column A so the first range can be $A$2:$A$70 and that means you don't need the 1 to specify the column_num. The IF statement was covering A2:C70 when you really only want either B2:70 or C2:C70 depending on the 1 or 2.
Assuming that A122 has either a 1 or 2 in it then,
=INDEX($A$2:$A$70, SMALL(IF(INDEX($B$2:$C$70, 0, $A$122) = $A$121, ROW($1:$69)), ROW(1:1)))
Standard non-array alternative,
=INDEX($A$2:$A$70, SMALL(INDEX(ROW($1:$69)+(INDEX($B$2:$C$70, 0, $A$122) <> $A$121)*1E+99,, ), ROW(1:1)))

how to count cells in excel with conditional statement?

I want to create a VBA script that can count the number of cells in two columns , for example column A and B, that have values "yes". Moreover, if one of the two cells or both in the same row have value "yes", I want the script to count only 1 time.
For example, if A2 and B2 have value "yes", it is counted 1 time. If A2 or B2 has value "yes", it is also counted 1 time.
Please suggest a solution.
Try this formula.
=SUMPRODUCT((--((A:A="yes")+(B:B="yes"))>0)*1)
I would not advise using whole column references with sumproduct, though, since with over a million rows in Excel 2007 and later, calculation might take a while. You may want to build range names that grow and shrink with the populated rows and use these instead of the column references.
Note that whole column references in SumProduct only work in Excel 2007 and later. For earlier versions of Excel you will need to ringfence a range.

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.