How to Extract, Change and Replace Data? - excel

Maybe I have used the wrong phrase to search but I have not been able to find a solution to the following challenge for Excel/VBA:
In Sheet 1 I have a database of order data from cells A1 to F10. Each row contains data for one order. Column A contains the order number.
As first step I, in sheet 2, wish to make a search on the order number and retrieve all data for that order into cells A5 to F5 in sheet 2.
After reviewing the data (and running another macro to recalculate the sales price) I wish to have the revised data replace the original data in sheet 1.
Can anyone help me with this?
Thank you in advance!

The 1st part does not need macro.
vlookup (fkeres magyarul) is your function. If you put this function in A5-F5 fields in sheet 2, looking for a given order number, in Sheet1 $A$1:$F$10, entering the required column number, asking exact match (FALSE), it works nicely. I entered the functions parameters in my previous sentence.
The 2nd part needs macro which makes Copy Paste back.
OR
Look for the requested order row in sheet2 A5 cell with MATCH (HOL.VAN magyarul) function, like =MATCH(A1;Sheet1!A1:A10;0)
and put INDIRECT (INDIREKT magyarul) function, like =INDIRECT("Sheet1!B"&A5;TRUE) in B5, etc.
In this case the Copy-Paste back macro is (starting from Sheet2):
Myorder = Range("A5").Value
Range("B5:F5").Select ' A5 is the row number of order, not the order No
Selection.Copy
Application.Goto Reference:=Worksheets("Munka1").Range("A" & Myorder)
ActiveSheet.Paste
...or very similar

Related

How to create a filtered drop down list based on multiple criteria

on the below Schedule image I am trying to create a Drop Down List in the "Gland (A)" Column. Rather than just creating a list of all available "Glands" I want that list to be filtered based on the data within "CORES / PAIRS", "SIZE mm" and "CABLE TYPE". For this example we will use a "3c 16 BS5467, XLPE/SWA/PVC".
Schedule
To determine the filter for the list, the "ID Ø (mm)" and "OD Ø (mm)" for the select cable need to be taken in to consideration, see Cables image below. As you can see for the example we are using the cable has an "ID" of 15.5 and "OD" of 20.35.
Cables
Finally seen below in the Glands image, the "ID" from above needs to be within the "INNER MIN/MAX" and the "OD" needs to be within the "OUTER MIN/MAX".
Glands
So back to the first image in the "GLAND (A)" columns for row 4 the drop down list should be filtered and only show concatenated values:
151/RAC/B/M25
501/453/UNIV/B/M25
ICG/653/UNIV/B/M25
In two separate formulas I managed to VLOOKUP just the "OD" based on the cable types:
=VLOOKUP(B4&C4&E4,'Cables'!A$2:H$169,8,FALSE)
Then based on the retrieved value LOOKUP the "GLAND SIZE" from within the "OUTER MIN/MAX":
=LOOKUP(2,1/((F4>='Glands'!E$3:E$9 + 1)*(F4<='Glands'!F$3:F$9 - 1)),'Glands'!B$3:B$9)
The problem is I don't know how to include checking the "ID" as well, also to retrieve concatenated cells ("GLAND TYPE" and "GLAND SIZE") and then for them to be a Data Validation Drop Down List.
Any help with this would be greatly appreciated.
Thank you
Ok, this is going to be hard to explain. I'll do my best. Maybe if we wrap this up in a dedicated sheet we won't make mistakes.
PHASE 1: create a new sheet.
Create a new sheet and name it "Calculations". We will put most of the stuff here. First of all we type "Selected row in Schedule" in the cell A1.
PHASE 2: determine what cable number is selected.
Since we have multiple entry of cable in the Schedule sheet, we will need multiple list of possible glades. Creating a dedicated list for each lane or costraining the user freedom would be unpractical. Therefore we need to know what row the user is selecting in the Schedule sheet. We have to use VBA. Right-click on the Schedule sheet name tag and click on "View code". Copy-paste this code in the window that has appeared:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Filling the cell A2 in the sheet Calculation with the row number of the selected _
cell in the scheet Schedule.
Sheets("Calculations").Range("A2").Value = ActiveCell.Row
'Preventing multiple selection in the F column of the sheet Schedule.
If Not Application.Intersect(Target, Range("F:F")) Is Nothing Then
Target.Resize(1, 1).Select
End If
End Sub
This code will report in the cell A2 of the sheet Calculation the row number actually selected in the sheet Schedule. Everytime the selection is changed, the value changes. It also prevent the selection of multiple rows of the F column in Schedule sheet (the column where Glades dropdown list will be placed). You can test the code by changing the selection in the Schedule sheet and looking at the result in Calculations sheet.
PHASE 3: determine what type of cable is selected and its ID/OD.
In the Calculation sheet, type "Selected cable" in range B1. In range B2 type this formula:
=INDEX(Schedule!$A:$F,Calculations!$A$2,2)&INDEX(Schedule!$A:$F,Calculations!$A$2,3)&INDEX(Schedule!$A:$F,Calculations!$A$2,5)
This formula reconstruct the name we will search in the LOOKUP column of the Cables sheet. It's a series of INDEX functions, nothing really complicated.
Now that we know what to look for, we can extract its ID/OD. Type "ID" in the cell C1 and "OD" in cell D1. In cell C2 type this formula:
=VLOOKUP($B$2,Cables!$A:$H,7,FALSE)
In cell D2 type this formula:
=VLOOKUP($B$2,Cables!$A:$H,8,FALSE)
These formulas will search the cables' list in the Cables sheet and extract the ID/OD of the given one.
PHASE 4: create the filtered list.
Your glands' list has its first gland in the third row. So just to make it easier to crosscheck the data, we will place our formulas accordingly. In sheet Calculations type "List stage 1" in cell E2. In cell E3 type this formula:
=IF(AND(C$2>=Glands!C3,C$2<=Glands!D3,D$2>=Glands!E3,D$2<=Glands!F3),ROW(),"")
Drag it all the way down until it will be cover the same number of rows of the glands' list in the Gland sheet. This formula will "highlight" in what rows are the glands we are looking for (if there are any). At this point the list is very long, unsorted and presumably has a lot of blank cells. We need to sort it. In cell F2 type "List stage 2". In cell F3 type this formula:
=IF.ERROR(SMALL(E:E,ROW()-ROW(F$2)),"")
Drag this one down just like the previous one. Now we have a compact list of numbers. We need to translate them into glade's names. In cell G2 type "Filtered gland list". In cell G3 type this formula:
=IF.ERRORE(INDEX(Glands!A:B,F3,1)&"/"&INDEX(Glands!A:B,F3,2),"")
Drag it down again like previously did. We have our list.
PHASE 5: name the list.
We need to create a dynamic reference to the list to cut out all the blank cells. Define a new name calling it Gland_Filtered_List referred to this formula:
=INDIRECT("Calculations!$G$3:G" & ROWS(Calculations!$G$3:$G$1048576) -COUNT.BLANK(Calculations!$G$3:$G$1048576)+2)
PHASE 6: insert data validation.
In the Schedule sheet, create a data validation for the glands column using the list mode and Gland_Filtered_List as origin.
That should do the trick. Right now i have to hurry for work, so i can't check the explanation. Everything should be in order. Try this and ask any question. I'll answer later.

Excel input field that pulls data from range

I'm trying to create an input/search field in an excel sheet that when someone enters data in the field, it starts autopopulating from X Range, when they find the data they want they hit enter and it pulls from Y Range.
Closest I've found is XLOOKUP, but I can't get it to work on a field when someone types into that field. I want this to work through column A of the entire sheet, so when the user goes to a new line it does the same as the line above.
So, input data into A4, while typing it search C:C and auto fills in as I type. Once I find what I like I hit enter and it pulls the cell next to the C cell I found the data on, resulting cell data, so D:D column.
I've tried inputing this into a dropdown validate field, but no go, just errors out:
=XLOOKUP(INDIRECT("a9"), C:C, D:D)

Using Index Match between two sheets in excel

I have two sheets.
This is the first sheet. Named 3
And this is the second sheet. Named centralizare
I have to write in Sheet2 C5 the number of 'Id garantie' from Sheet1 if the 'Categorie' from Sheet1 is 'Done' (Sheet2 B5) and 'Status 1'from Sheet1 is 'In lucru' (Sheet 2 B4)
I tried this
=COUNT(INDEX('3'!$C$2:$C$99,MATCH(B5,'3'!A2:A99,0)))
but it returns only one number if only the 'Categorie' is Done.
And other method with errors
=MATCH('3'!C2:C99,(centralizare!C5='3'!B2:B96)*(D4='3'!F2:F96),0)
=INDEX('3'!$C$2:$C$99,MATCH(D5,'3'!C2:C99,0))
=COUNT(INDEX('3'!A2:A96, MATCH("In lucru",'3'!E2:E96)))
You could try an array formula with sumproduct:
=SUMPRODUCT(--IFERROR((('3'!A2:A5=centralizare!B5)),0))
Must be entered using ctrl + alt + enter. Is should appear like so in your formula bar if done right:
{=SUMPRODUCT(--IFERROR((('3'!A2:A5=centralizare!B5)),0))}
Sample workbook: https://filetea.me/t1sjZdwkvxrRxGePZmKZyB7BQ
EDIT: It also seems that you should never had "finalizat" & "wip" in the same row, if I understand your rules right. WIth the array formula, you can check for both rules & only count those that meet both rules, not just one, and somewhat check data quality this way.:
{=SUMPRODUCT(--IFERROR((('3'!A2:A5=centralizare!B5))*('3'!C2:C5=***choose a cell/range where you put a reference to finalizat/in luru instead of wip/donw***),0))}
In other words, the general format for multiple criterial is:
{=SUMPRODUCT(--IFERROR(((CRITERIA ONE)*(CRITERIA TWO)),0))}

Comparing and counting values

I have a table, let's call it my Individuals Table, much like the one below, containing a column of individuals along with their corresponding codes listed in an adjacent cell. Codes for each individual are all listed within the same adjacent cell next to the individual's name, and separated by a carriage return.
Example table
What I'd like to do is the following:
Run through the code cell for each individual
For each code in the individual's code cell, check if this code exists in a separate Codes Table
If the code exists in the Codes Table, add n+1 to the total count for that code in an adjacent cell and add the individual's name to the list of individuals with that same code in another adjacent cell.
If the code does not exist in the Codes Table, add the code to the Codes Table, add n+1 to the total count for that code in an adjacent cell and add the individual's name to the list of individuals with that same code in another adjacent cell.
Result of running the algorithm on the example table
If a similar program can achieve the same results, then I'm open to that too.
I try to give you a possible solution, by minimizing the use of VBA code.
As starting point I would do is rearranging codes for every individuals. Keeping more codes in a single cell separed by a return it is not as easy to manage like having a single code for each cell. Of course I will keep each code associated with each individual. A way to do it is with your data is by using the formula substitute and replace the returns characters with a semicolon. The formula works this:
=SUBSTITUTE(B2,CHAR(13),CHAR(59))
B2 is the cell where you are converting returns to semicolon. You will then use this formula for all values in your B column.
Once you have replaced returns with semicolon, copy and paste values and then with the function "Text to Columns" in Data tab you will convert each cell to a series of columns (depending on how many codes you had listed in the original cell of your sheet). Now you will be in a situation where the first column you have the names of individuals, and then on the same row in the subsequent columns you have all associated codes, like in the picture here below:
In order to create a complete list of all codes you can easily copy all columns with codes. Paste the codes in a suitable space (I suggest in a new worksheet), and then with some copy and paste jobs put all codes under the same column. Select all codes and with the button "Remove Duplicates" always in the Data tab you will have a list of all unique codes included in your original table.
Then you can copy and paste the column with all unique codes you created under your "Codes" column. Now you can count the codes in the converted table with this formula:
=COUNTIF($B$1:$C$4, D2)
Where first argument of COUNTIF refers to the codes in the converted table and the second argument is a code in your column "Codes" where you pasted the unique codes.
Now as far as I know there is no function in Excel to create a list of names separated by commas (but I would be glad to discover that it exists if anybody knows!!!). Therefore I created a custom one with some VBA code with the name List Individuals:
Function ListIndividuals(refCode, NameRange As Range, CodesRange As Range) As String
'Check size in row number of NameRange and CodesRange is same, otherwise give error
If NameRange.Rows.Count <> CodesRange.Rows.Count Then
ListIndividuals = CVErr(xlErrRef)
Exit Function
End If
result = ""
For Col = 1 To CodesRange.Columns.Count
For n = 1 To CodesRange.Rows.Count
If CodesRange.Cells(n, Col).Value = refCode Then
If CodesRange.Cells(n, Col).Value <> "" Then
If result = "" Then
result = NameRange(n)
Else
result = result & ", " & NameRange(n)
End If
End If
End If
Next
Next
ListIndividuals = result
End Function
So last step is to use this formula under your "Individuals" cells as follows:
=ListIndividuals(D2,$A$13:$A$16,$D$13:$E$16)
Where first argument is the Code, the second one is the list of individuals in the converted table (it should be the first column), then the third one are the columns with the codes in the converted table. As a result of this custom formula you will have the list of individuals separated by commas.
All above works on my computer, but if you need more information, please do not hesitate to contact me.

referencing sheets by number instead of name in cells

Lets say
sheet3.name = "d"
Is there a way I could put in a cell on sheet2 the formula =sum(sheet3!b:b) where sheet3 is being substituted with the actual sheet3 name?
I can only get =sum('d'!b:b) to work so far.
I could use VBA for this probably but I'm curious how to do this in a cell so I don't have to run a macro each time.
If you can use a UDF User Defined Function that will return the sheet name
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function
then a formula like
=SUM(INDIRECT(SHEETNAME(3) &"!B:B"))
will return the sum from column B on sheet 3.
SHEETNAME(number) returns the sheet name of the number which is index.
So Sheet(1) returns Sheet1, etc
Use below formula anywhere in the sheet to get the sheet name - the sheet must have a filename for this to work:
=REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),"")
You can either reference that cell using Indirect:
=SUM(Indirect("'"&A1&"'!B:B"))
or, if you don't want to have a second cell, you can combine the two formulas into one:
=SUM(INDIRECT("'"&REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),"")&"'!B:B"))
For anyone not concerned with the order of the sheets, the post by Biff here on mrexcel.com works well.
In Excel 2013, go to the Formulas tab in the ribbon and make a defined name:
Name: SheetNames
Refers to: =GET.WORKBOOK(1)&T(NOW())
Then use a formula like this example:
=INDIRECT("'"&INDEX(MID(SheetNames,FIND("]",SheetNames)+1,255),A3)&"'!A1")
where A3 refers to the index number in a cell in the current sheet, and A1 refers to the location of the value to be retrieved from the other sheet. I.e., in the current sheet, if A3 = 2, then the formula will point to cell A1 in the second sheet of the workbook. I just use a column of index numbers in my current sheet, then drag this formula down and it fills in values from all of my other sheets.
You will need to save as a macro-enabled file (.xlsm).
I'm not sure if this is a good idea but it's the first one I could think of.
I would add additional function to your VBA project which will return actual name of your Sheet3:
Function Sheet3Name()
Sheet3Name = Sheet3.Name
End Function
Next, when you create sum formula of column B:B in Excel cell you need to do it in this way:
=SUM(INDIRECT(Sheet3Name()&"!A:A"))

Resources