VBA equivalent to an index small function - excel

Hi I have used this formula many times in excel but would like to be able to make a VBA code for it. Based off the criteria in a column "L" I would like to find the 1st and 2nd matches of name in a separate column G and add and subtract the corresponding values associated to that name (on the same rows) in different columns.
Formula: =IF(L3="1:1",INDEX($D$3:$D$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1))-INDEX($E$3:$E$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1))-INDEX($F$3:$F$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1))-INDEX($I$3:$I$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1)),IF(L3="2:2",INDEX($D$3:$D$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1))+INDEX($D$3:$D$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),2))-INDEX($E$3:$E$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1))-INDEX($F$3:$F$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1))-INDEX($I$3:$I$5000,SMALL(IF($G5=$G$3:$G$5000,ROW($G$3:$G$5000)-ROW($G$3)+1),1)),"")
There may be a different way in VBA, this was working in the worksheet so far but got really long based of all the different criteria that could come up in column "L". My data set is about 4000 lines long with many different combinations
My data is as follows: and the expected answers are in column M using the above formula.

Related

Using two values in a sheet to filter and return values from a table in another sheet

I'm fairly new to coding and i've been googling around for the last few hours trying to solve this problem but it seems to be a little beyond what i'm able to do so i would be very grateful for some help
In Sheet1, I have a table which has columns between M - CV (175 columbs). For each column, i have an "ID number" value in row 3. From Row 6 to the end of the table, i have several "search terms" separated by commas in the column CV
In Sheet2, the corresponding "ID Numbers" are in column B. Column AN contains strings.
For each ID Number value in sheet1, i'm looking to find find all the corresponding cells in sheet2 where the ID number in Column B is the same, and Column AN of sheet2 contains at least one of the "search terms" in column CV
For each ID number, i'm hoping to join the entries in Column AN of sheet2 which match the criteria above and paste them into Row 5 of the respective column in Sheet1
I've gone around in quite a few circles trying to do this and i'm back to square 1 with no code to show for it.
I've tried to research both the autofilter function, and using for loops. The research i've done indicates that for loops are rather slow to run for a large data set.
I'm hoping to find a solution which is as easy to read and understand as possible
I hope i've given enough information for everyone to understand and help
THank you in advance
My Excel subscription has expired an I've started using Google Sheets for most of my spreadsheet work, so I tested this there. Some conversion may be required. I did this using formulas, not VBA also, not sure if that changes things for you.
If I understand correctly, you have two sheets with a shared key column, sheet 1 contains search terms across multiple columns, and sheet 2 contains search terms comma delimited in a single column.
With this setup we want to bring the search term column of sheet 2 into the correct row of sheet 1 by key using VLOOKUP. I made a named range in sheets which contained all my data on sheet 2 and called it "dst". My formula was then =VLOOKUP(A2, dst, 7, true) since my key in sheet 1 was in column A, dst was the range I was searching, my column with my delimited search terms was column 7 in relation to dst, and I had ordered sheet 2 by key. I pasted this formula relatively down all rows as needed.
We want to construct a regex string using our search terms across multiple columns in sheet 1, into a single cell. I used =JOIN("|", B2:E2) on sheet 1 since my search terms were in columns B:E, and this resulted in a regex that looked like this for me: alligator|dog|rabbit|lizard where alligator, dog, rabbit, and lizard, were all search terms in that row. Paste down relative as needed.
We want to run our regex against our search target cell containing the comma delimited search terms. I ran =REGEXMATCH(F2, G2) where F2 was my delimited search terms from sheet 2, and G2 was my constructed regex for the row. Paste down relative as needed.
A screenshot of my completed sheet 1:
Once you know which cells have matches you can do whatever you want.

Matching mulitple criteria across 2 sheets and pulling some data from that match in excel

I have two sheets of data, one is a list of 4000+ companies and some data about the company (including a CUSIP and an issue date). The other lists stock prices per day for a said list of companies spanning multiple years.
I need to match the CUSIP and the issue date from the first sheet with that of the second and extract a number from sheet 2 where both where a match and put it in sheet one in a colomn next to the other data from that company.
Sheet 1
Sheet 2
I tried =VLOOKUP(E1076&O1076;Sheet1!A:Sheet1!K;11;FALSE) but all this did was give me a #NAME error same for when I tried to do this on the same sheet
I tried =INDEX(W:AP,MATCH(1,(X:X=D5)*(AE:AE=N5),0),42) but that just tells me it isn't a formula to begin with
Combined Sheets
In Column R:
{=INDEX(AG,MATCH(D2&N2, U&AB,0))}
will work for you but will likely be slow (make sure to enter with ctrl+alt+enter)
You can try and mitigate the lag by using defined ranged (e.g. U2:U4000&AB2:AB4000) but since your list is ever growing I'll guess that the lag will come back pretty quick.
To keep things faster, I suggest you use a helper column where you concatenate U and AB. Let's say column AC:
=U2&AB2
(copied all the way down)
You can then use a simple INDEX/MATCH:
=INDEX(=INDEX(AG,MATCH(D2&N2, AC,0))
You could also concatenate D and N to another column and use that column as your lookup value.

VBA code required to loop through different sized rows of data and return MAX value

I am currently automating a dashboard creation and I've hit a bit of a roadblock. I need some code that will go through about 7000 rows of data and return the highest value in a certain column for each specific item. The data is copied from a pivot table and so is broken down into row sections, I have attached a mock of what it looks like.
I need the highest value in Column G for each portfolio, and will need to use the portfolio code (e.g. XY12345 - They are always 7 characters) to map that value to the dashboard.
My issue is, each portfolio has a different number of rows for the values, and some have blank cells between them, and therefore I am stumped. I was hoping to use Column J to count the number of rows for each portfolio (as there are no breaks for the portfolios in this column) and then use a loop to loop through each portfolios rows of values, based off the Column J count, and then return the highest row value for each portfolio. Problem is I'm new to VBA and have been teaching myself as I go, and I've yet to use a loop.
Many thanks,
Harry
If I understand correctly, you're looking for the largest value in Column G.
I'm not sure why you think you would need VBA for this.
Get the maximum value of a column
You mentioned that you're concerned about each column not having the same number of cells but that's irrelevant. as SUM ignores blank cells, so just "go long", or - find the maximum of the entire column.
To return the largest number in Column G you could use worksheet formula :
=MAX(G:G)
The only catch is that you can't place that formula anywhere column G or else it would create a circular cell reference (trying to infinitely add a number to itself). let's pit that formula in cell F1 for now (but anywhere besides column G would do fine).
Find the location of a value
Now that you know the largest value, you can determine where it is using a lookup function such as MATCH or VLOOKUP. Like with so many things in Excel, there are several ways to accomplish the same thing. I'll go with MATCH.
Replace the formula from above (in F1) with:
=MATCH(MAX(G:G),G:G,0)
This will return the row number of the first exact match of the maximum value of Column G.
As for the third part of question: returning the code like X12345 where the value exists, will be a little tricky since your data is not organized in a logical tabular style (tabular meaning, "like a table").
Your data is organized for humans to look at, not for machines to easily read and manipulate it. (See: Office Support: Guidelines for organizing and formatting data on a worksheet)
Basically, when organizing data in rows, all relevant information should be on the same row (not a subjective number of rows behind). Also, you have the number combined with other information.
My suggestion for a quick fix:
Right-click the heading of Column C and choose Insert to insert a blank column.
In C2 enter formula: =IF(B2="",C1,LEFT(B2,7))
Copy cell C2
Select cells in column C all the way to the "end" of your data, where ever that is (not the end of the worksheet). For example maybe you would select cells B2:B1000)
Paste the copied cell into all those cells.
Now, you can again modify the formula in F1:
=INDEX(C:C,MATCH(MAX(G:G),G:G,0))
This will return the value from Column C in the same row that the maximum value of Column G is located.
This is known as an INDEX/MATCH formula.
Hopefully this works for you in the interim until you can organize your data more logically. There's lots of related information and tutorials online.

Excel Formula to Lookup multiple columns

I tried reading other questions on this matter, and everything is very specific to that person so it got pretty confusing...
Anyways, I was looking for a way to use VLOOKUP or Index Match to do the following (I've been trying but I keep running into errors or weird...long....formulas...):
I have 2 excel Sheets. One excel sheet has something like this:
(This is incomplete. I haven't put in prices yet, but I'm working on the formatting and formulas foremost. Just pretend there are prices in those empty cells :) )
The second excel sheet is this:
In the 2nd Sheet, F Column, I was trying to get a formula to match Sheet2ColumnE with Sheet1 Columns A,F,K,P,U & Z and return [depending on whether cell D is B or S] the single or bulk price for the specific item.
I've used the Index Match formula before, but it got tricky when I had to index multiple columns and match multiple columns. Is there a simple way to get this going? Or will I be looping IFs?
Thanks for assistance.
Here is the workbook. Or a sample of it...
Test.xml

Sum the product of multiple vlookup or index/match results

I have 2 worksheets, one with multiple columns of data where rows are given years and the other columns are values under the headers of names, and the second sheet lists rates by year (column) and by name (row).
I am trying to take each value in a given column of sheet1 and multiply it by the relevant rate (found by matching the year of each row and the name of its column to the table in sheet2) and then sum the products by name, but can only use a single cell to do all this.
So far I've tried SUMPRODUCT and got to =SUMPRODUCT(O$24:O$514,INDEX(Rates!$A$2:$X$200,MATCH(O$23,Rates!$A$2:$A$67,0),MATCH($D$24:$D$514,Rates!$A$2:$X$2,0))) but this seems to fall down at the MATCH($D$24:$D:$514,Rates!$A$2:$X$2,0) part, even entering as an array.
I've currently resorted to a clumsy series of SUMIF(2016)*INDEX/MATCH(2016 rate)+SUMIF(2015)*INDEX/MATCH(2015 rate) etc, but would really like to have forward compatibility without relying on formulae being updated!
slightly simplified image of the workbook, thanks for any help!

Resources