Find which rows has longest Cell in a given column? - vba

Currently I have a function in VBA that finds the longest cell length in a given column. it inserts a column next to whatever given column, inserts LEN(A1) (whatever the desired column address is) and fills that down, then It uses the MAX function to find the greatest value.
Until now that's been good enough but now the user wants to know which row that greatest value is on as well. is there a way to do that without sorting or filtering?
The function is run once by the user and loops across each column, and there are about 150000 rows, so it would take a while to filter / sort 15 times.
any suggestions appreciated

=match(max(column),column,0) will give you the row number where the max occurs.
You could then use
=index(othercolumn,match(max(column),column,0)) to get some other column value for that row

Assuming values in A2:A6
This will give you row number, relative to the data range
=MATCH(MAX(LEN(A2:A6));LEN(A2:A6);0)
This will give row number relative to the worksheet
=ROW(A2)-ROW($A$1)+MATCH(MAX(LEN(A2:A6));LEN(A2:A6);0)
And this will give you the value
=INDEX(A2:A6;MATCH(MAX(LEN(A2:A6));LEN(A2:A6);0))
All those are array formulas, you need Ctrl+Shift+Enter to enter them

thanks to everyone who posted, I also found this formula: =CELL("address",OFFSET(A1,MATCH(MAX(A1:A10),A1:A10,0)-1,0)) (assuming data is A1 - A10) which will give the cells actual address as well if anyone ever wants it. heres the site for that: https://support.microsoft.com/kb/139574

Related

How to use vba to filer a column using value from a specific cell

I want to use a macro to filter columns in a table. I want to filter for values that are higher than a value I want to put in cell, to be able to easily change the filter. Does someone have a trick for doing this with vba?
Many thanks, Bram
Record a macro whilst filtering a table on a column value. You would right click on the table column header of interest whilst recording the code and select Number_Filters > Greater Than and enter your desired number. That would give you the outline code. You can then amend the code to pick up the desired value from a specified cell. If applying filter to multiple columns record macro whilst doing this process over several columns.
Thank you for you answer. I tried this already, but I could not get the macro to pick from a specific cell. If I stored the value of the specific cell under as 'value' and put that in the outlined code, it would just do Greater Than value.. DO you have shortcut for this?
Thanks!

Excel formula not working as expected

I have a sheet that shows max values spent anywhere. So I need to find most expensive place and return it's name. Like this:
Whole sheet.
Function.
Function in text:
=IFS((A6=MAX(D2:D31)),(INDEX(C2:C31,MATCH(A6,D2:D31,0))),(A6=MAX(H2:H31)),(INDEX(G2:G31,MATCH(A6,H2:H31,0))),(A6=MAX(K2:K31)),(INDEX(K2:K31,MATCH(A6,L2:L31,0))))
Basically I need to find a word left to value, matching A6 cell.
Thanks in advance.
Ok.. Overcomplicated!
Firstly, why the three rows? it's a lot easier if you just have one long row with all the data (tell me if you actually need 3 I'll change my solution)
=LOOKUP(MAX(D2:D31);D2:D31;C2:C31)
The MAX formula will lookup the biggest value in the list, the Lookup formula will then match it to the name.
Please note: If more than one object has the maximum price, it will only return the first one. The only way I can think of to bypass that would be to build a macro.
EDIT:
Alright.. Multi Column solution is ugly and requires extra columns that you can just hide.
As you can see you'll need 2 new columns that will find the highest for each row, 2 new columns that will find the value for each of these "highest" (in this case tree and blueberries) and then your visible answer will simply be an if statement finding out which one is bigger and giving the final verdict. This can be expanded with an infinite number of columns but increases complexity.
Here are the formulas:
MAX(H2:H31)
LOOKUP(A5;H2:H31;G2:G31)
MAX(L2:L31)
LOOKUP(C5;L2:L6;K2:K6)
IF(A5>C5;B5;D5)

Count number of visible rows with criteria

In the beginning I used this formula SUBTOTAL(3;(A14:A150>0)) and it works perfectly but now I need to add a condition that I must count the number of rows where the value in the column F is greater than 0.
This formula COUNTIF(F14:F150;">0") return the right result, but when I filter I don’t have the right one.
I tried also SUMPRODUCT(SUBTOTAL(3;F14:F150)/A1*(F14:F150>0)) it return the same thing as the COUNTIF.
You can add another column with formula:
=IF(SUBTOTAL(103;F14))
Because you are using filters this formula will help you determine if your cell is hidden or not.
Now you can use, assuming that latter formula is in column G:
=COUNTIFS(F14:F150;">0";G14:G150;">0")
Array-entered using Ctrl+Shift+Enter:
=SUMPRODUCT(--(F14:F150>0),IF(SUBTOTAL(3,OFFSET(F14:F150,ROW(F14:F150)-MIN(ROW(F14:F150)),,1)),F14:F150,""))/A1
I don't get exactly your needs but you may want to use
=SUMIFS(F14:F150,F14:F150,">0",A14:A150,">0")
or
=COUNTIFS(F14:F150,">0",A14:A150,">0")

Determining which value in a row, if any, is greater

I have a large table of data where I need to find out if, along one row, any values are greater than a threshold number. If it finds a number, I need it to either return that number, or return the column header so I can find that number.
One problem that may occur is that there may be multiple values that exceed this threshold.
The data can't be sorted in ascending or descending order because that may mess up other orders, or take them out of the necessary column they need to be in.
Below is a small snippet of data that I'm dealing with. I need to know if the numbers are ever greater (more positive) than -50. I need to know which numbers exceed it and/or which column it's in.
I'd rather it not be vba or macro, but if needs be then such is life.
Help? Two Sample data rows
From the Home Tab choose Conditional Formatting
Select the Range that you what Conditionally Formatted. Enter the formula that what evaluated. In this case >-50.
With data in A1 through E12, in F1 enter the array formula:
=MIN(IF(A1:E1>-50,A1:E1))
and copy down:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
Note:
If all the values are less than -50, the formula will return 0

How do I look backwards for the most recent match of a text string in a column?

I have an Excel sheet with various data entries that are in date order going down the page, with the dates in column A. I need a formula that will take a text string from an adjacent cell, then look back up a neighbouring column for the most recent match then return the date from column A.
Currently I have this formula in cell H100: =LOOKUP(G100,E100:E$5,A100:A$5).
I want it to look for the text in G100 in column E, going backwards to find the most recent example and then return the corresponding date from column A but despite the LOOKUP command being in reverse it always returns the first example in date order, not the most recent.
I would really appreciate some help from an expert, which I am not!
I am not certain to understand the question, but try
=OFFSET($A$1, 1+MATCH(G100, E$5:E100, 0)0,1,1)
this should catch the first (higher in the sheet) instance of the lookup match.
I would bring it into an Access database where that kind of data manipulation is easy.
But then, I know how to use Access to do those kinds of things and I think it's much harder to do in Excel.