I'm totally stuck on creating a user defined function in Excel VBA for the following problem. Any help would be greatly appreciated.
My excel file looks like this (only adding a small portion for the sake of brevity):
A B C D
1 Susan Reagan Smith
2 Jill L Taylor
3 Sarah Sullivan Williams
4 Roger J Lopez
I would like a function that determines: If A1 = Susan OR Jill OR Sarah, AND LEN (length of string) of B1 >1 THEN D1 = A1 /2 B1 C1 ELSE A1 /2 C1
So the output in column D would look like:
A B C D
1 Susan Reagan Smith Susan /2 Reagan Smith
2 Jill L Taylor Jill /2 Taylor
3 Sarah Sullivan Williams Sarah /2 Sullivan Williams
4 Roger J Lopez Roger /2 Lopez
I want to use VBA because I have many names to add and don't really want a huge formula in cell D1 with a lot of nested if statements. I've been working on it myself but it's just a mess and I'm too embarrassed to post it here. Thanks guys!
Public Function JoinNames(A, B, C)
If FirstNameMatches(A) And Len(B) > 1 Then
JoinNames = A & " /2 " & B & " " & C
Else
JoinNames = A & " /2 " & C
End If
End Function
Private Function FirstNameMatches(N) As Boolean
Select Case N
Case "Susan", "Jill", "Sarah"
FirstNameMatches = True
End Select
End Function
Have a list of names somewhere else on a sheet.
Use IF(ISNA(MATCH(A1, list_of_names, 0)), "Not found", "Found") to figure out if the name is recognized.
Related
Example of cells below. As you can see some cells have more info like emails, and some don't have a number but they're all in the same column.
Cell 1
Smith, James W
#129432
123 N. Street Road
Libertyville, IL, 60048
(810) 955-9721
claudie.predov#hotmail.com
Cell 2
Evette T Rudnick
#7928253
1308 Stutler Lane
Tidioute, PA, 16351
Cell 3
David C Ponce
#1234567
2855 Retreat Avenue
Frenchboro, ME, 04635
(313) 204-6364
Any help is appreciated. Thank you.
Use MID and SEARCH:
=IFERROR(MID(A1,SEARCH("(???) ???-????",A1),14),"")
What is the actual function to generate a single line output (Column I, #QR) from below fields?
+
A
B
C
D
E
F
G
H
I
1
f_name
l_name
designation
email
landline
mobile
country
web
#QR
2
JohnÂ
DOE
Senior ManagerÂ
john.doe#gmail.com
4422688
444221
Sweden
google.com
BEGIN:VCARD\nVERSION:2.1\nFN:John Doe\nORG:Senior Manager - Google\nADR:Sweden\nTEL;WORK:4422688\nTEL;CELL:444221\nEMAIL:john.doe#gmail.com\nURL:google.lk\nEND:VCARD
There is a function "Textjoin" if you're using Excel 2019 or up:
=TEXTJOIN(" ",TRUE,A1:A2)
This would result into the string "f_nameJohn". You can look up more about this function here: https://support.microsoft.com/en-us/office/textjoin-function-357b449a-ec91-49d0-80c3-0e8fc845691c
You can also use:
=A2&B2
giving JohnDOE
Then you can do:
=A2&" "&B2
which will put a space between: John DOE
if you don't have Excel 2019 or up you can use:
=A2&" "&B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2&" "&I2
or
=CONCATENATE(A2," ",B2," ",C2," ",D2," ",E2," ",F2," ",G2," ",H2," ",I2)
I have a data set like this:
A B C
----------
1 A blue
1 A red
1 B blue
2 A red
3 B blue
3 B green
3 C blue
4 C blue
4 A blue
4 A green
And a separate table like this (this is already auto generated using UNIQUE()):
E F G
-----
1 A
1 B
2 A
3 B
3 C
4 C
4 A
I want to join text from column C and show the result in column G based on a criteria given in columns E and F. The result I am looking for would look like this (all concatenated/joined text should be in column G):
E F G
-----
1 A blue red
1 B blue
2 A red
3 B blue green
3 C blue
4 C blue
4 A blue green
Optional, not needed but would be nice: The delimiter between generated text is a line brake so each line is in a separate line inside the same row.
Thank you.
place your first table (data table/ 1,2,3) in column A to C.
and another table (5,6,7) in column E to G
Enter below Array Formula in G2
{=CONCAT(IF(A2:A11&B2:B11=E2&F2,C2:C11&" ",""))}
you will get your result.
For earlier versions, we need to create UDF
Function ConcatUDF(rng() As Variant, ByVal delim As String) As String
Dim a, i As Long
For i = 1 To UBound(rng, 1)
If rng(i, 1) <> "" Then
ConcatUDF = ConcatUDF & _
IIf(ConcatUDF = "", "", delim) & rng(i, 1)
End If
Next
End Function
and we will get the result.
Edited:
I forgot to absolute the references. Please consider below mnetioned formula.
with Build in CONCAT formula for excel 2016
=CONCAT(IF($A$2:$A$11&$B$2:$B$11=E2&F2,$C$2:$C$11&" ",""))
with UDF for earlier version of excel
=ConcatUDF(IF($B$2:$B$11&$A$2:$A$11=F2&E2,$C$2:$C$11,"")," ")
I am looking for some help with a formula. On my 'data' sheet, I have data in the format below:
A B C D E F G H I J
1 UID RecordType HCode AdmittedDate Forename Surname DOB Sex STDate RDate
2 87962 STAsses STIV1 01/01/2012 Mark Jones 13/07/1978 Male 09/12/2012
3 89658 Transfer GLSI2 01/01/2012 Alison Aitken 20/12/1956 Female 08/07/2013
4 84563 Discharge JHOP1 01/01/2012 David Beckham 09/08/1987 Male 08/07/2013
5 89654 STAsses STGE1 01/01/2012 Andrew Macbeth 27/09/1976 Male 08/07/2012
6 89867 Transfer KIND1 01/01/2012 George Deas 08/05/1989 Male 08/07/2013
7 87962 Transfer STIV1 01/01/2012 Mark Jones 13/07/1978 Male 04/03/2013
8 89654 Transfer STGE1 01/01/2012 Andrew Macbeth 27/09/1976 Male 12/08/2012
On my 'report' sheet, I have the following table set up:
B C D E F
4 HospCode RecordType Jul-12 Aug-12 Sep-12
5 STGE1 Assessments
6 Transfers
7 Discharges
8
I would like to do 2 things.
In D5, E5, F5, a formula is needed to return the number of 'STAssess' records for HCode 'STGE1' from the data sheet, which were completed in the month shown in D4 of the 'report' sheet (Jul-12). The dates for these records are in column I of the data sheet - 'STDate'.
I've tried using SUMPRODUCT which works to an extent, but I'm having difficulty bringing back records only completed in July. I need the formula to be dynamic in that it only brings back results for the month entered in D4 (so a date range built into the formula isn't doing the job).
In D5, E5, F5, I need to bring back the number of records from the 'data' sheet, which have 'Transfer' in column B (RecordType), 'STGE1' in column C (HCode) and a date in column J (RDate) which falls in the month specified on D4 of the 'report' sheet (Jul-12).
Anyone got any ideas? The data sheet is actually going to be an external .csv file which can't be manipulated. I don't mind using other hidden sheets to do any sorting that needs to be done, provided that this can be setup so that when the data is brought in, no extra work has to be done.
Thanks!
For both of your questions you can use COUNTIFS. It is easier to understand (for me) and compatible with Excel 2007 and newer.
First question:
=COUNTIFS(DATA!B:B,"STAsses",DATA!C:C,$B5,DATA!I:I,">="&D$4,DATA!I:I,"<"&DATE(YEAR(D4),MONTH(D4)+1,1))
Second question:
=COUNTIFS(DATA!B:B,"Transfer",DATA!C:C,$B5,DATA!J:J,">="&D$4,DATA!J:J,"<"&DATE(YEAR(D4),MONTH(D4)+1,1))
And these formulas can be dragged across your needed range.
Here is an example formula that you could use for the D5 cell, does that point you in the right direction?:
{=SUM(IF($B$2:$B$8=$B14,IF($C$2:$C$8=$A14,IF($D$2:$D$8=C$13,1,0),0),0))}
This is an array function so you must enter it without the {} at each end and use Ctrl + Shift + Enter to enter the formula.
If your summary table looks like
HospCode RecordType 12-Jul 12-Aug 12-Sep
STGE1 STAsses 1 0 0
STGE1 Transfer 0 1 0
STGE1 Discharges 0 0 0
And your data is in a table called Table1, then in D5
=SUMPRODUCT((Table1[HCode]=$B5)*(Table1[RecordType]=$C5)*(MONTH(Table1[STDate])=MONTH(D$4)))
I'm not sure what you mean by 'bring back', but assume you mean that you want to show it when RecordType = column C and when the RecordType = "Transfer" and the RDate is in the right month. Then
=SUMPRODUCT((Table1[HCode]=$B5)*(Table1[RecordType]=$C5)*(MONTH(Table1[STDate])=MONTH(D$4)))+SUMPRODUCT((Table1[HCode]=$B5)*(Table1[RecordType]="Transfer")*(MONTH(Table1[RDate])=MONTH(D$4)))
I typed this in a cell =VLOOKUP(RANDBETWEEN(1,721),Players,1)
However it only gives me the position but not the corresponding player name and salary. How do I get randbetween to generate the whole row?
Position Name Salary
QB Peyton Manning 10700
QB Aaron Rodgers 10100
WR Calvin Johnson 9500
WR A.J. Green 9500
QB Drew Brees 9200
QB Matthew Stafford 9100
WR Julio Jones 9100
RB Marshawn Lynch 9000
Assuming the number being matched is in the first column of Players and the next 3 columns are the ones you want to pull from:
{=VLOOKUP(RANDBETWEEN(1,721),Players,{2,3,4},1)}
Entered across 3 cells as an array formula (using Ctrl+Shift+Enter)
You could put =RANDBETWEEN(1,721) in its own cell, then refer to that cell from each column. Example formulae:
A B C D
1 =RANDBETWEEN(1,721) =VLOOKUP($A1,Players,1) =VLOOKUP($A1,Players,2) =VLOOKUP($A1,Players,3)
2 =RANDBETWEEN(1,721) =VLOOKUP($A2,Players,1) =VLOOKUP($A2,Players,2) =VLOOKUP($A2,Players,3)
Etc..