create a excel search engine using vba and filters - excel

I am trying to create a search engine using vba to filter my rows based on a value entered into a cell/my search bar.
Sub DateFilter()
'hide dialogs
Application.ScreenUpdating = False
'filter for records that have June 11, 2012 in column 3
ActiveSheet.Range("A5:C30").AutoFilter Field:=3, Criteria1:=ActiveSheet.Range("A5").Value
Application.ScreenUpdating = True
End Sub
This macro runs when a user hits my search button and will filter my rows based on the cell value in my cell A5.
However whilst the code is filtering it is showing the wrong result.
I have the following layout in excel:
A B C
White XXX London
Black ZZZ Manchester
Gold TTT England
Silver FFFF Scotland
If I type in my cell A5 the word White it will show silver?
Can someone please show me where I am going wrong? Thanks

As Gary's Student commented, you're currently filtering your rows based on a cell value inside your rows.
Instead of this, filter it based on a value outside, like D1. Then you can put whatever you like into the D1 cell for your filter.

Related

Highlight exceeding limit in VBA

line customer product OrderQty TotalQty
1 A 11111 5 10
2 B 11111 5 10
3 c 11111 5 10
4 A 22222 5 20
5 B 22222 5 20
6 C 22222 5 20
I have a table as shown above.
I want to to highlight the lines when OrderQty is bigger than TotalQty for a product.
OrderQty represents the number of units requested for in that order, TotalQty represents total units available to fulfil all orders.
In this example, I want to highlight lines 1,2,3 as product 11111 OrderQty 5+5+5=15 is bigger than TotalQty 10.
Is there a way to automate this in VBA? I suspect using Sumifs but I can't wrap my head around..
Thanks in advance!
You don't need VBA for this. Assuming your table above starts on row 1 with Line in column A and TotalQty in column E; put a unique list of Product in column F and in cell G2 put the formula:
=IF(SUMIF(C:C,F2,D:D)>VLOOKUP(F2,C:E,3,FALSE), "Over", "Equal or under")
The SUMIF sums OrderQty for each Product, the VLOOKUP returns TotalQty for the first instance of each Product found in the table. You can then use conditional formatting to highlight rows if required.
If you did go the VBA route, I'd probably put the table into an array, create a dictionary of Product with a value for OrderQty, and either loop on the array and sum values, or loop on the dictionary keys and call the sumif worksheet function.
Maybe something like this ?
Sub test()
Range("A:E").Interior.Pattern = xlNone
Range("C1:C7").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True
Set Rng = Range("AA2", Range("AA100000").End(xlUp))
For Each Prod In Rng
Set c = Range("C:C").Find(Prod.Value, lookat:=xlWhole)
Tot = c.Offset(0, 2).Value
Ord = Application.SumIf(Range("C:C"), Prod.Value, Range("D:D"))
If Ord > Tot Then
For Each cell In Range("C2", Range("C2").End(xlDown))
If cell.Value = Prod.Value Then
Range(cell.Offset(0, -2), cell.Offset(0, 2)).Interior.Color = 65535
End If
Next cell
End If
Next Prod
Range("AA:AA").ClearContents
End Sub
Or if you choose without VBA, use Conditional Formatting.
Select the range you want to be highlighted
Click Conditional Formatting ---> New Rule
Choose "Use a formula to determine which cells to format"
Type =SUMIF($C:$C,$C2,$D:$D)>VLOOKUP($C2,$C:$E,3,FALSE) in the formula box
Click Format button, then do the formatting as you want
Click OK, Click OK.
[Good day Kyle,
Is this what you are trying to do. This is only simple solution but can help you.
I used a helping column for your criteria you mentioned. I used sumifs. You can modify it to your style and needs. I used conditional formatting to highlight cells.]1
First, streamline your table, divide into two tables:
line, customer, product, OrderQty in one table.
product, TotalQty other table.
Assuming {product,TotalQty} in 'Sheet 2', and the other table in 'Sheet 1', then add a column in second table (C column in sheet 2) 'OrderQty>TotalQty' and insert formula in the cell below it, as follows:
=IF(SUMIF(Sheet1!$C:$C,$A2,Sheet1!$D:$D)>$B2,"Yes","No")
Drag or copy-paste the formula to remaining product rows.
Screenshots:

Excel look up value in array, return next value

I would like to look up a value in a range and return the value in the next row, but can't quite figure out how to do this. I especially would like to do this with formulas rather than VBA, and preferably with built-in formulas than custom (VBA) formulas, due to macro security issues.
I'm using Excel 2010. My workbook has two worksheets, "assessment" and "lookup". In lookup, I have lookup tables.
"lookup" looks something like:
Column A Column B Column C
1 Sales Engineering Manufacturing
2 Alice Bobbie Charlie
3 Dawn Edgar Frank
4 George Holly Isabel
In "assessment," I have some some drop downs from which users select one name from each column in "lookup." Based on some other criteria, I then rank these and create a new, sorted list (using INDEX() and MATCH()) that produce the selected name and corresponding column name a new sort order
Column A Column B
10 Engineering Edgar
11 Sales Alice
What I'd like is to return the name from the next row.
Column C
10 Holly
11 Dawn
But I'm having real trouble figuring out how to get there.
Assuming lookups is located at B2:D5 (change as required) and the result data is at F2:H3 (change as required) enter this formula in cell H2 then copy down.
=INDEX(
INDEX($B$2:$D$5,0,MATCH($F2,$B$2:$D$2,0)),
1+MATCH($G2,
INDEX($B$2:$D$5,0,MATCH($F2,$B$2:$D$2,0)),0))

Automatically working out the average of filtered results

I have a spreadsheet where column P has a score between 1-6
The cell O4 has the following formula: =AVERAGEIFS(P8:P5000,P8:P5000,"<>6",P8:P5000,"<>0")
This formula searches for the average of the score in column P excluding 6, blanks and 0
Column O has staff names e.g John, Mark, Tim.......
What i want to do is for Cell O4 to automatically calculate the average of the figures shown in column P after i have used the filter function to show only results of a selected staff member.
I was hoping excel might be able to do this automatically however cell O4 appears to still be showing the average of the whole column P regardless of whether i have filtered or not.
I was given the formula below on another forum but it seems to be giving slightly wrong results albeit only by a small amount but i need to have the results exact if possible. Any help appreciated.
=SUMPRODUCT(1-ISNUMBER(MATCH(P8:P100,{0,6},0)),SUBTOTAL(9,OFFSET(P8,ROW(P8:P100)-ROW(P8),0,1)))/SUMPRODUCT(1-ISNUMBER(MATCH(P8:P100,{0,6},0)),SUBTOTAL(2,OFFSET(P8,ROW(P8:P100)-ROW(P8),0,1)))
Maybe
{=AVERAGE(IF((P8:P5000<>6)*(P8:P5000<>0)*SUBTOTAL(103,INDIRECT("O"&ROW(8:5000))),P8:P5000))}
will do what you want. Assuming the Filter is on column O.
The 103 in SUBTOTAL will also exclude if rows are manually hidden. If this ist unwanted and it should only exclude hidden rows, if filtered, then use 3 instead.
This is an array formula. Input it into the cell without the curly brackets and then press [Ctrl]+[Shift]+[Enter] to create the array formula.
I would create a separate table in a new sheet with all unique staff members and then perform the calculation. This way, you can quickly compare values for all staff just by scanning the table instead of having to constantly update the filter to see the values for potentially dozens or hundreds of staff. You would add the staff name range and criteria to your AVERAGEIFS formula.
For your example:
Sheet 2
A B
--- ---
1 | Staff Average
2 | Bob =AVERAGEIFS(Sheet1!$P$8:$P$5000,Sheet1!$O$8:$O$5000,A2,Sheet1!$P$8:$P$5000,"<>6",Sheet1!$P$8:$P$5000,"<>0")
3 | Mary =AVERAGEIFS(Sheet1!$P$8:$P$5000,Sheet1!$O$8:$O$5000,A3,Sheet1!$P$8:$P$5000,"<>6",Sheet1!$P$8:$P$5000,"<>0")
4 | Joe =AVERAGEIFS(Sheet1!$P$8:$P$5000,Sheet1!$O$8:$O$5000,A4,Sheet1!$P$8:$P$5000,"<>6",Sheet1!$P$8:$P$5000,"<>0")

Excel - Multiline Textbox offset each line onto the next column?

I need some Excel help here.
I am trying to create a database to store customers information in my shop.
The customer will have a name and address, something like this:
MR SMITH
123 FLAT A, 321 ROAD,
LONDON,
AB12 3AB
UNITED KINGDOM
This address has 5 rows. I want to just copy and paste everything into my userform which has a Multiline Textbox. I need it to offset each row into each column of my spreadsheet.
So it ends up looking like this:
CELL A1 = MR SMITH
CELL B1 = 123 FLAT A, 321 ROAD,
CELL C1 = LONDON,
CELL D1 = AB12 3AB
CELL E1 = UNITED KINGDOM
I know I can create a 5 single line Textboxes in my userform, but I want to save time by copying everything into 1 Textbox instead of 5.
Please help.
Thanks!
The data I have stored in the cell H5, but you can use a Var or a Textbox like source...
If you want to save in the cells, use:
Range("A1:E1").Value = Split(Range("H5").Value, Chr(10))
if you want an array to manipulate (put value in separate Textbox), use
myarr = Split(Range("H5").Value, Chr(10))

excel delete row if column contains value from to-remove-list

Let's say that I've got a sheet - number one - with over 5000 rows (say, columns 'A' - 'H' each).
In another sheet - number two - I have a "to-remove-list" - a single column 'A' with 400 values, each containing alphanumerical string (example: xxx1234).
I have to remove every entire row from sheet number one, if column 'E' contains any value from "to-remove-list" (from column 'A' of sheet number two).
By removing the entire row, I mean delete the row and move it up (not leaving the blankspace)
How do I achieve that? Any help would be much appreciated.
Given sheet 2:
ColumnA
-------
apple
orange
You can flag the rows in sheet 1 where a value exists in sheet 2:
ColumnA ColumnB
------- --------------
pear =IF(ISERROR(VLOOKUP(A1,Sheet2!A:A,1,FALSE)),"Keep","Delete")
apple =IF(ISERROR(VLOOKUP(A2,Sheet2!A:A,1,FALSE)),"Keep","Delete")
cherry =IF(ISERROR(VLOOKUP(A3,Sheet2!A:A,1,FALSE)),"Keep","Delete")
orange =IF(ISERROR(VLOOKUP(A4,Sheet2!A:A,1,FALSE)),"Keep","Delete")
plum =IF(ISERROR(VLOOKUP(A5,Sheet2!A:A,1,FALSE)),"Keep","Delete")
The resulting data looks like this:
ColumnA ColumnB
------- --------------
pear Keep
apple Delete
cherry Keep
orange Delete
plum Keep
You can then easily filter or sort sheet 1 and delete the rows flagged with 'Delete'.
I've found a more reliable method (at least on Excel 2016 for Mac) is:
Assuming your long list is in column A, and the list of things to be removed from this is in column B, then paste this into all the rows of column C:
= IF(COUNTIF($B$2:$B$99999,A2)>0,"Delete","Keep")
Then just sort the list by column C to find what you have to delete.
Here is how I would do it if working with a large number of "to remove" values that would take a long time to manually remove.
-Put Original List in Column A
-Put To Remove list in Column B
-Select both columns, then "Conditional Formatting"
-Select "Hightlight Cells Rules" --> "Duplicate Values"
-The duplicates should be hightlighted in both columns
-Then select Column A and then "Sort & Filter" ---> "Custom Sort"
-In the dialog box that appears, select the middle option "Sort On" and pick "Cell Color"
-Then select the next option "Sort Order" and choose "No Cell Color" "On bottom"
-All the highlighted cells should be at the top of the list.
-Select all the highlighted cells by scrolling down the list, then click delete.
For a more modern answer, bring the data into powerquery, merge the 2nd sheet into the first with a left outer join. Expand. Use drop down filter to remove any rows that don't match as null. Remove test column and file close and load back to excel
New Answer 9/28/2022
Now you can use FILTER function that simplifies it.
=FILTER(A3:B7, ISNUMBER(MATCH(A3:A7,D3:D4,0)))
Note: The question requires to modify the original data sheet, this is in a general not recommended, because you are altering the input, better to have a working sheet with the transformations required.

Resources