VLOOKUP with button click - vba

I'm trying to write some code for a button click to perform a VLOOKUP.
Sheet1 = Payment Form, Sheet2 = Global, Sheet3 = Details
Button will be on Sheet "Payment Form".
This would be the code for Cells in Global Sheet,
O1 = =VLOOKUP(BA,Details!A:H,8,0)
P1 = =VLOOKUP(BA,Details!A:H,6,0)
Q1 =VLOOKUP(BA,Details!A:H,5,0)
I need this to loop through all rows as the amount can change each month, if a match is found the perform the VlookUp, is no match is found, the delete the row from the Details Sheet.
For Example: Global, Cell B1 = 27801. In Details match found, then do the above codes from Columns O, P & Q.
Global, B2 = 27802. In Details no matching record found, row deleted. Continue to row 3 & 4 ......

what I suggest is you put all the value in table will be more efficient (not simply enter in the excel cell), you need create the table by INSERT ->Table. It will look like this :
Do this also for the Details :
Back to global worksheet, just need enter 1 row of formula, the rest of the rows in the same column will have the same formula "style"
Column O :=VLOOKUP([Column BA],Table2[[#All],[Column1]:[Column8]],8,FALSE)
Column P :=VLOOKUP([Column BA],Details!A:H,6,FALSE)
Column Q :=VLOOKUP([Column BA],Details!A:H,5,FALSE)
To remove the unwanted row, just filter out the blanks value in that columns will do.

Related

Need to copy/paste row (x number of times) when cell value equals X

I'm trying to use Excel for a Jewelry Order Form.
In the order form (sheet1), a user may select from a cell that is formatted into a drop-down list, a number representing the number of stones in a piece of jewelry. For example, if there are 10 stones in a ring, then the user selects 10 from the drop-down list.
The details for each of the 10 stones needs to be captured in the order form (Sheet1). For example, each stone will have 4 data elements... a stone type, weight, color, cut... So I created the desired formatted row of data (in Sheet2) where each cell is a drop-down for a user to select from.
I want to create a control button to do the following actions:
Delete rows 19:150 in Sheet1
This will clear out any prior stone details that may be displayed.
Find the value in cell C13 in Sheet1
This value will be used to determine how many rows should be pasted/displayed
Copy row, range A2: D2 in Sheet2
This is template row data where each cell in the row is its own drop-down list.
Paste row in B19 in Sheet1
This is the template row pasted into an order form.
4a) Paste as many rows as the value in step (2) above.
For example, if the value in step 2 from above is "3", then the stone details row will need to be pasted 3 times in the order form.
The furthest I've been able to get is the creation of the control button, and the delete clause...
Private Sub CommandButton1_Click()
Sub deleteMultipleRows()
Rows("19:150").Delete
End Sub
For the delete statement, you should probably use Sheet1.Rows("19:150").Delete as that will ensure that excel knows which sheet to delete those rows from.
You can declare a variable and assign it a value like so:
Dim rowCount as Integer
rowCount = Sheet1.Range("C13")
If you try recording a macro for the copy and pasting, you should see some sample code.
Note:
If the result looks something like this:
Sheet1.Activate
Row(2).Select
Selection.Copy
You can combine such statements like so:
Sheet1.Row(2).Copy
Since most .Activate commands can be ignored, and .Select and Selection. commands can be combined (and should be in most situations). The Sheet1. added before the .Row(2) tells Excel to specifically use Row 2 from Sheet1. Without the qualifier, i.e. just Row(2), Excel would use Row 2 from whichever sheet happens to be currently active.
You can then use a variable and the value from the sheet to loop through either pasting or copy/paste combo like so:
Dim counter as integer
For counter = 1 to Sheet1.Range("C13")
'Add code for copy/paste here
Next counter
Or if you have the rowCount variable declared and assigned, you could use it here like this:
Dim counter as integer
For counter = 1 to rowCount
'Add code for copy/paste here
Next counter

Search a range with 3 tables according to dropdown value

I'm quite new to macros/formulae of excel. I have a sheet (Sheet1) exported which has the approvers list.
I also have 3 tables ( A, B, C)
In Sheet2 which has like below values:
Table A: {aaa,bbb,ccc}
Table B: {xxx,yyy,zzz}
Table C: {d12,e12,c12}
I need to search Column Q in sheet1 with either one table as per the dropdown menu.
If I select Table A, then I need to have all rows with text aaa OR bbb OR ccc in Column Q. I used the below formula, but I can search only one table at time.
I can't select through dropdown menu.
=SUMPRODUCT(--ISNUMBER(SEARCH(A[ID],Q51)))>0
This returns true or false. I have to keep changing the formulae for each table. Can I do that through drop down menu.
Use INDIRECT : place INDIRECT(Right(myRange, 1) & "[ID]") in the place of A[ID].
myRange is the address of the cell where the drop-down is placed; i.e. could be D1 for example, and the formula becomes:
=SUMPRODUCT(--ISNUMBER(SEARCH(INDIRECT(Right(D1, 1) & "[ID]"),Q51))) > 0

look for Column heading and sum up

I have been working with a excel file with a lot of data, which is arranged in 2 sheets.
I would like to get data from sheet 1 to sheet 2 with reference to the column headings.
For example:
So if I want to find the sum of function 1 person A with criteria 1, the command have to go and find the heading "sum of function 1" in sheet 1 and choose the data that are only under criteria 1 and sum it up in sheet 2 cell D5. (By using column heading reference instead of cell reference).
The table range is A2 : U80.
Thanks.
First you have to format your data as table (select the data -> Menu Insert -> Table). Then you rename your table, for example Table1. Let's say one of the columns you want to sum on the sheet2 is called ColumnName.
On the sheet 2 you write a formula
=SUM(Table1[ColumnName])
The result will be what you are after.
You should try it by SUMIFS(). Syntax will be
=SUMIFS(AgeRange from sheet1,NameRange Sheet1, Name cell Sheet2, PlaceRangeSh1, Place Cell Sh2)
Tell me if requires further help.

excel macro to change values in a column

I have a file coming every month which has few columns with more than 50K rows. I usually need to replace values in column B with different values such as if any field in column B contains ASD , replace it with XYZ. And there is a list of value that needs to be replaced. Also as mentioned above old value can be more than once in column B and needs to be replaced .
Any help would be great.
Thanks
Record a macro [View > Macros > Record Macro]
Do the find+replace operation
Stop recording.
Now view Macros > Edit.
You will see the newly recorded Macro which does the find+replace, and you can edit this to your exact requirements.
Try that:
Sub test()
'this will get you the last row number in column "B"
lastRow = Sheets("SheetName").Cells(Sheets("SheetName").Rows.Count,"B").End(xlUp).Row
'loop through all cells in coulmn "B" and replace text as needed
For i = 1 To lastRow
'you can add multiple lines for each values that needs to be replaced
Sheets("SheetName").Cells(i, 2).Value = Replace(Sheets("SheetName").Cells(i, 2).Value, "ABC", "XYX")
Next i
End Sub
Where 'SheetName' is your sheet's name. Hope that is what you are looking for!

Searching and comparing various values using VBA

I have to create a macro which will:
Get the value from the cell A1 search this value in column C.
If the value in cell A1 exists in column C, the macro needs to be compare the value in cell B1 with values in column D.
If the value in cell A1 exists in column C AND the value in cell B1 exists in column D, then the text "Values found" should appear in cell E1.
The above needs to happen for all non empty rows in column A.
I was trying to use the following formula:
=IF(ISERROR(MATCH(A2,$C$2:$C$138,0)),"Load number not found","Load number found")
But it not working as I want. I have limited access to internet so I can't check all web sites. Would you please help me. Thanks a lot.
To check if A1 is in column C and if B1 is in column D (in the same row A1 was found in column C), then you need to do the following:
=IF(ISERROR(MATCH(A1,$C:$C,0)),"Load number not found",IF(B1=INDEX($D:$D,MATCH(A1,$C:$C,0),1),"Load number found","Load number not found"))
The first IF checks if A1 is in column C, the second IF checks if B1 is in column D (in the same row A1 was found in column C)
It will return "Load number found" only if both conditions are true. Otherwise it will return "Load number not found".
You can write a macro to do the same thing. But the easier way is to lock the cells in column E only and protect the sheet so that users will not accidentally change any of the formulas.
Update:
Since Column C can have duplicates, need to use the following array formula:
=IF(ISERROR(MATCH(1,(A1=$C:$C)*(B1=$D:$D),0)),"Load number not found","Load number found")
When you paste this formula to E1, make sure to press CTRL + Shift + Enter instead of just pressing the Enter key.
If I understand, a conventional solution with formulae is to concatenate your C and D column data and then search that. If you insert a new columnC with:
=D2&E2
copied down to suit you could apply (but say in ColumnF rather than ColumnE) your existing formula with only slight modification:
=IF(ISERROR(MATCH(A1&B1,$C$2:$C$138,0)),"Load number not found","Load number found")
subject to quite what is in which row.