Turn nested if formula into VBA - excel
I would like to turn my formula into a macro. I want to look up values on column A and return a different value to column D depending on conditions.
My formula is :
=IF(ISNUMBER(SEARCH("*10*",D2,6)),"word1",IF(ISNUMBER(SEARCH("*15*",D2,1)),"word2",IF(ISNUMBER(SEARCH("*1*",D2,1)),"word3",IF(ISNUMBER(SEARCH("*20*",D2,1)),"word4",IF(ISNUMBER(SEARCH("*30*",D2,1)),"word5")))))
I need to make sure that search for 1 comes after 10 or 15 so that formula doesn't return word3 for all cells containing "1".
Column A contains a text that is at times is misspelled so the only common value is the numbers. That is why I want to search the numbers within text to return word12345.
Below is an example- I get a different excel file with age name and last name every week and need to fill out the program classification manually.
╔═══════════╦═════════╦═══════════╦═════════╗
║ AGE ║ NAME ║ LAST NAME ║ PROGRAM ║
╠═══════════╬═════════╬═══════════╬═════════╣
║ 10 YE OLD ║ ANNE ║ BROWN ║ word1 ║
║ AGE 10 ║ ALLY ║ SMITH ║ word1 ║
║ 15 YO ║ MATT ║ JANES ║ word2 ║
║ 15 ║ DENNIS ║ JOHNSON ║ word2 ║
║ 10Y OLD ║ DIANA ║ WILLIAMS ║ word1 ║
║ 20yr ║ JORDAN ║ BROWN ║ word4 ║
║ 30 YR OLD ║ MELISSA ║ RODRIGUEZ ║ word5 ║
╚═══════════╩═════════╩═══════════╩═════════╝
The data that I need to get the result from is exampled below:
10 YE OLD
AGE 10
15 YO
15
10Y OLD
20yr
30 YR OLD
You could try this, I tested it and it worked for me utilizing whole numbers as you stated (10,15,5,1,30,20) and changed it to the "word1" etc. If you were looking for a range, i.e. 1 but less than 10, the select case can be updated. You have to insert your workbook name and extension as well as worksheet name where indicated in the code.
Sub Select_Case()
Dim RNG As Range
Dim ws As Worksheet
Dim TextTest As String
Dim AllColumn As Variant
Set ws = Workbooks("<EnterYourWorkbookName.Extension_Here>").Worksheets("<EnterYourSheetNameHere>")
AllColumn = ws.Range(ws.Cells(2, 4), ws.Cells(1, 4).End(xlDown).Offset(1, 0)) 'assumes heading in row 1 and no breaks in data
For i = 2 To UBound(AllColumn) 'assumes heading, no headin i = 1
TextTest = ws.Cells(i, 4).Value
Set RNG = ws.Cells(i, 4) 'rows then columns if you need to change it later, this should be where you want to output your data
Select Case TextTest
Case Is = 10
RNG = "word1"
Case Is = 15
RNG = "word2"
Case Is = 1
RNG = "word3"
Case Is = 20
RNG = "word4"
Case Is = 30
RNG = "word5"
'Case else (utilize if you have a catch all just in case it is something not predicted)
'what to do
End Select
Next i
End Sub
Let me know if you need some small tweaks.
Related
How to a create field which lists the headers of previous fields containing "yes"?
I have a table in excel of the following format: ╔════════╦════════╦════════╦════════╦═════════╗ ║ Field1 ║ Field2 ║ Field3 ║ ... ║ Field10 ║ ╠════════╬════════╬════════╬════════╬═════════╣ ║ no ║ no ║ no ║ ... ║ no ║ ║ no ║ yes ║ no ║ ... ║ no ║ ║ yes ║ yes ║ yes ║ ... ║ yes ║ ║ yes ║ yes ║ no ║ ... ║ yes ║ ║ . ║ . ║ . ║ ... ║ . ║ ║ . ║ . ║ . ║ ... ║ . ║ ║ . ║ . ║ . ║ ... ║ . ║ ╚════════╩════════╩════════╩════════╩═════════╝ Where as you can see, each field can have any combination of yes's and no's I am trying to create a single field which is based on the data format previously shown. This field will contain the name(s) of fields which contained a "yes". If a "yes" exists in more than one Field, then it should list those fields separated by a comma. Here is an example of what that desired field might look like: ╔════════╦════════╦════════╦════════╦═════════╦══════════════════════════╗ ║ Field1 ║ Field2 ║ Field3 ║ ... ║ Field10 ║ NewField ║ ╠════════╬════════╬════════╬════════╬═════════╬══════════════════════════╣ ║ no ║ no ║ no ║ ... ║ no ║ ║ ║ no ║ yes ║ no ║ ... ║ no ║ Field2 ║ ║ yes ║ yes ║ yes ║ ... ║ yes ║ Field1, ..., Field10 ║ ║ yes ║ yes ║ no ║ ... ║ yes ║ Field1, Field2, Field10 ║ ║ . ║ . ║ . ║ ... ║ . ║ ... ║ ║ . ║ . ║ . ║ ... ║ . ║ ... ║ ║ . ║ . ║ . ║ ... ║ . ║ ... ║ ╚════════╩════════╩════════╩════════╩═════════╩══════════════════════════╝ I am trying to achieve this with an excel formula, but so far the only solution apparent to me involves including every possible permutation in the excel formula. Of course, this is inefficient and time-consuming to create and to make changes to. Is there any way to achieve this result efficiently?
If you do not have Office 365 Excel then here is a Custom UDF to do what you want: Function JoinField(ttl As Range, srchrng As Range, crit As Variant, Optional sep As String = ",") As String Dim ttlArr() As Variant Dim srchrngArr() As Variant Dim i&,j& ttlArr = ttl.Value srchrngArr = srchrng.Value If UBound(ttlArr, 1) <> UBound(srchrngArr, 1) Or UBound(ttlArr, 2) <> UBound(srchrngArr, 2) Then Exit Function For i = LBound(ttlArr, 1) To UBound(ttlArr, 1) For j = LBound(ttlArr, 2) To UBound(ttlArr, 2) If srchrngArr(i, j) = crit Then JoinField = JoinField & ttlArr(i, j) & sep End If Next j Next i JoinField = Left(JoinField, Len(JoinField) - Len(sep)) End Function Put this in a module attached to the workbook. DO NOT put it in the worksheet code or ThisWorkbook code. It is then called like a normal function: =JoinField($A$1:$J$1,$A2:$J2,"Yes",",") Where the first criteria is the names to concatenate. The second is the Range that has the criteria. The third is the Criteria to find. The forth is optional separation character. Default is ,. If you have the latest Office 365 Excel you can use this formula as an array. =TEXTJOIN(",",TRUE,IF($A2:$J2 = "Yes", $A$1:$J$1,"")) Being an array it needs to be confirmed with Ctrl-Shift-Enter when exiting edit mode instead of Enter. If done correctly Excel will put {} around the formula.
As a demonstration of how tedious and verbose the FORMULA would get using multiple IF formulas, you could wind up with a formula looking like the following: =IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),1)=",",SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),", ","",1),IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,"")) The above formula assumed row 1 as your field headers, and your first field starting column A. The above formula would be placed in K2 and copied down. Now if for some reason you are not allowed to use VBA or save your your worksheet in and .XLSM format, then you would need something like that hideous formula above. With out the scroll bar, the formula looks more like: =IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")& IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")& IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")& IF(J2="yes",", "&J$1,""),1)=",", SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")& IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")& IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")& IF(J2="yes",", "&J$1,""),", ","",1), IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")& IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")& IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")& IF(J2="yes",", "&J$1,"")) Proof of ugliness at work A major down side of this is that if you needed to add another field the editing of the formula would be a royal pain in the butt!
If theres only 10 Columns you could go with sth. like this =IF(A2=1;INDIRECT("A1";TRUE);"") & ", " & IF(B2=1;INDIRECT("B1";TRUE);"") & ", " & .... In this Example a just added 0, 1 as values not the yes/no.
How to filter and list data from another sheet?
I need some help for my Excel formula. I am trying to select specific rows based on the values in those rows. Let's say these are my values: ╔══════════╦═════════╦══════╗ ║ Data ║ Month ║ Name ║ ╠══════════╬═════════╬══════╣ ║ Value 1 ║ january ║ mark ║ ║ Value 2 ║ january ║ mark ║ ║ Value 3 ║ january ║ rick ║ ║ Value 4 ║ january ║ rick ║ ║ Value 5 ║ march ║ mark ║ ║ Value 6 ║ march ║ mark ║ ║ Value 7 ║ march ║ rick ║ ║ Value 8 ║ march ║ rick ║ ║ Value 9 ║ august ║ mark ║ ║ Value 10 ║ august ║ rick ║ ╚══════════╩═════════╩══════╝ The value in A1 = january The value in A2 = mark I want to list all the rows where the month is A1 and the name is A2. This should be my result: ║ Value 1 ║ january ║ mark ║ ║ Value 2 ║ january ║ mark ║ I have tried using INDEX combined with IF, but with no succes. I am looking for either a VBA solution or a formula, whatever is best.
I am assuming that: 'Data' is in Sheet1!A1, 'Month' is in Sheet1!B1 and 'Name' is in Sheet1!C1. On Sheet2!A1 is the value of 'Month' which you want to filter out and on Sheet2!A1 is the value of 'Name' you want to filter out. Make a new column in Sheet1!D1 (beside 'Name') and give it a header, eg. 'Tag'. In the first row, D1 (under 'Tag') write the formula =IF(AND(B2=Sheet2!$A$1,C2=Sheet2!$A$2),MAX($D$1:D1)+1,"") Pull (copy) the above formula down till the last row of your table. On Sheet2, replicate your table headers, this time in B1, C1 and D1 (as A1 & A2 already have your filter values). // now everything is for Sheet2 In B2 write the following formula: =IFERROR(INDEX(Sheet1!A:A,MATCH(ROWS($B$2:B2),Sheet1!$D:$D,0)),"") Paste this formula in the new filter table rows. Change values in Sheet2!A1 (Month) and Sheet2!A2 (Name) to test if the correct rows are being filtered.
VBA to merge cells in excel
I have a excel sheet like below. Consider the sno column as Excel numbering. So all the columns except DateTime are merged. I want to Merge the DateTime cells and want to delete the empty records. I tried a few formulas with no success. Is this possible I tried below formula but its giving weird results becoz of datetime values i think. =INDEX($A$1:$A$<ENDROW>,(B1-1)*2+1,1)&" "&INDEX($A$1:$A$<ENDROW>,(B1-1)*2+2,1) I have this: ╔═════╦════════╦══════════╦═════════╦═════════════╗ ║ sno ║ Action ║ DateTime ║ User ID ║ Name ║ ╠═════╬════════╬══════════╬═════════╬═════════════╣ ║ 1 ║ INSERT ║ 8-Nov-13 ║ childsk ║ Keri Childs ║ ║ 2 ║ ║ 16:06:43 ║ ║ ║ ║ 3 ║ INSERT ║ 8-Nov-13 ║ childsk ║ Keri Childs ║ ║ 4 ║ ║ 16:04:27 ║ ║ ║ ╚═════╩════════╩══════════╩═════════╩═════════════╝ Expected Output: ╔═════╦════════╦═══════════════════╦═════════╦═════════════╗ ║ sno ║ Action ║ DateTime ║ User ID ║ Name ║ ╠═════╬════════╬═══════════════════╬═════════╬═════════════╣ ║ 1 ║ INSERT ║ 8-Nov-13 16:06:43 ║ childsk ║ Keri Childs ║ ║ 2 ║ INSERT ║ 8-Nov-13 16:04:27 ║ childsk ║ Keri Childs ║ ╚═════╩════════╩═══════════════════╩═════════╩═════════════╝
You have two tasks to perform. To merge date and time into a single cell. This is simply done by (Cell C1 contains the formula (as text) that is used in B1.) and formatting B1 as Custom -> d-mmm-yy hh:mm:ss. Then you can copy the results (as values) and format to a destination column. Remove extra rows. You can use the code below (make sure you change the number of repetitions from 10 to whatever you need). . Sub del_skip_rows_repeat_caller() Call del_skip_rows_repeat(1, 10) End Sub Sub del_skip_rows_repeat(nrows As Integer, nrep As Integer) Dim i As Integer For i = 1 To nrep Call del_skip_rows(nrows) Next i End Sub Sub del_skip_rows(nrows As Integer) Dim rng As Range Set rng = Range(Selection, Selection.Offset(nrows - 1, 0)).EntireRow 'rng.Select rng.Delete Shift:=xlUp Set rng = Selection.Offset(1, 0).Cells(1, 1) rng.Select End Sub
Combine multiple columns into one cell with header information
I am trying to create a cell in my worksheet that summarizes all the data in the row. I have found a way to combine all the fields into the first column, but I also need it to include the header name and it needs to ignore fields that are blank. (https://stackoverflow.com/a/13074838) My excel file has over 50 columns and 5000 rows so I think this might be a job for a macro. Any ideas on how to accomplish this? ╔═════════╦══════╦═════╦═══════════╦═══════╗ ║ Summary ║ Name ║ Age ║ County ║ ZIP ║ ╠═════════╬══════╬═════╬═══════════╬═══════╣ ║ ║ Sue ║ ║ Snohomish ║ 98144 ║ ║ ║ Bob ║ 36 ║ Pierce ║ 98335 ║ ║ ║ Pat ║ 32 ║ Spokane ║ ║ ║ ║ Jim ║ 40 ║ King ║ 98101 ║ ╚═════════╩══════╩═════╩═══════════╩═══════╝ Cell A2 would have the following contents: Name: Sue County: Snohomish ZIP: 98144 Cell A3: Name: Bob Age: 36 County: Pierce ZIP 98335 Cell A4: Name: Pat Age: 32 County: Spokane
What you need is a formula that : Uses the IF function to test whether each information cell has data (I'm assuming that you will always have the name). If the cell does, concatenate the character representation of the ASCII code for a carriage return (CHAR(10)) and the data for that cell. Be sure to turn on wrapping for the formula cell, or the text that is displayed won't show the line breaks. The resulting formula looks like this. ="Name: " &B1& IF(C1="","",CHAR(10)&"Age: "&C1)& IF(D1="","",CHAR(10)&"County: "&D1)& IF(E1="","",CHAR(10)&"Zip: "&E1) You could get fancy and indent the data (age, etc.) by the same amount using the LEN function.
How to sum up values between 2 dates in Excel 2007
Lets say I would like to sum up values from January to March. Below is an example ╔═══════════╦════════════╗ ║ Column A ║ Column B ║ ╠═══════════╬════════════╣ ║ 1/30/2011 ║ 1 ║ ║ 1/25/2011 ║ 1 ║ ║ 3/30/2011 ║ 1 ║ ║ 3/25/2011 ║ 1 ║ ║ 5/13/2011 ║ 1 ║ ╚═══════════╩════════════╝ I did some research and found I can use the SUMIFS function =SUMIFS(B1:B5,A1:A5,">="&DATE(YEAR(2011),MONTH(1),DAY(1)),A1:A5,"<="&DATE(YEAR(2011),MONTH(4),DAY(1))) But for some reason instead of returning 4 it returns 0. I would really appreciate if someone could figure out why. Thank you
I don't think Year/Month/Day do what you're expecting, see: http://www.techonthenet.com/excel/formulas/year.php They return the year value, month value, and day value of their argument. Try entering =YEAR(2011) and compare it to =YEAR("1/30/2011") That said, you can get what you want by just putting the dates in the quotes =SUMIFS(B1:B5,A1:A5,">=2011-01-01",A1:A5,"<=2011-04-01") produces 4 in my Excel.