Excel Finding Consecutive Phone Numbers - excel

Is there a way in excel if I have a bunch of phone numbers in 1234567890 format, that I could find ranges and paste in the format of XXXXXXXXXX-XXXX?
I tried with some mid formulas but it's not working quite right.
=IF(A63=A62+1,A62 & "-" & A63,"NO ")
EDIT:
1234567890
1234567891
1234567892
1234567893
1234567897
1234567898
1234567899
5552221212
5552221213
Should look like:
1234567890-7893
1234567897-7899
5552221212-1213
EDIT 2:
If you throw in an odd ball between the mix say, 5555555555, I don't want to make 5555555555 link with any of those numbers.
For example, currently, I am looking at this further down on my spreadsheet: =IF(A209=A208+1,A208 & "-" & A209,A209). Basically, it posts the TN above it with the number after. What happens is going down the list above I'd get
1234567890 - 1234567891
1234567891 - 1234567892
Is there a way to make it show only 1234567890 - 1234567892?

If all the numbers are of the same format and you always want the "-" inserted before the last four numbers this will do the trick
In an excel formula:
=LEFT(A1;6) & "-" & RIGHT(A1;4)
In VBA
Dim text1 As String, text2 As String
For i = 1 To NumberOfRows
text1 = Cells(A,i)
text2 = Left(text1,6) + "-" + Right(text1,4)
Next i

Related

How to add space in a string in excel vba?

how can i add space in this example string?
Input value in cell A1 is ABCDEFGHIJK and will be paste in another cell B1 with a format of ABCDE FG HIJK.
One formula to insert a space:
=LEFT(A1,5)&" "&RIGHT(A1,LEN(A1)-5) to insert a space after the 5th position. (ABCDE FGHIJK)
One to insert 2 spaces as per example:
=LEFT(A1,5)&" "&MID(A1,6,2)&" "&RIGHT(A1,LEN(A1)-7)
Input ABCDEFGHIJK, result ABCDE FG HIJK
In short: Use =LEFT(), =RIGHT() and =MID() to get parts of your string and concatenate the parts and your spaces.
Edit:
In VBA:
Public Function StringWithSpaces(inpStr As String) As String
StringWithSpaces = Left(inpStr, 5) & " " & Mid(inpStr, 6, 2) & " " & Right(inpStr, Len(inpStr) - 7)
End Function

How to concatenate a list of words into a sentence with "and" before last item in Excel?

I want to join a list of words in Excel (not in VBA... with an Excel formula in the worksheet) to the following specifications:
Formula should ignore empty cells.
Formula should concatenate the words with "and" before final item if there is more than one item in the array of cells.
Formula should add "," between items if there are more than two items.
Examples:
A1=dog
A2=cat
A3=bird
A4=fish
Result would be: dog, cat, bird, and fish
A1=dog
A2=cat
A3=(empty cell)
A4=fish
Result would be: dog, cat, and fish
A1=dog
A2=(empty cell)
A3=bird
A4=(empty cell)
Result would be: dog and bird
A1=dog
A2=(empty cell)
A3=(empty cell)
A4=(empty cell)
Result would be: dog
Pretty please? I promise I've searched and searched for the answer.
Edit: Thank you, ExcelArchitect, I got it! This was the first time I'd ever used a custom function. You use it just like any other function in the worksheet! This is so great.
Not to push my luck, but how to do I get two cells to concatenate with my result if there is only one word in the result and two other cells if there is more than one word? Example: If the function you made for me returns just "dog", I'd want it to concatenate a cell with the text (B1) "My favorite thing to wear is a " and then "dog" and then another cell (B2) that says " costume." to make the sentence "My favorite thing to wear is a dog costume." But if it returns more than one animal, it would concatenate two other cells like this: Cell C1 "My favorite things to wear are " and "dog, cat, and bird" and Cell C2 " costumes." so that it would say "My favorite things to wear are dog, cat, and bird costumes."
If you're curious, my data really has nothing to do with animals or costumes. I am writing a program that will score a psychological test and then create an interpretive report from the test scores (I'm a psychologist).
-Mary Anne
Mary Anne:
This would be a great time to use VBA! But if you don't want to, there is a way to accomplish your goal without it.
You have to account for all of the possible outcomes here. With 4 different animals that means you have 15 outcomes:
Your equation just has to take into account all 15. It is VERY long and drawn out as a result. As such, if you have more than 4 animals that you'd like to turn into phrases, you should go the VBA route.
Here is my set up:
The formula in A7 is the following:
=IF(AND(A2<>"", A3="", A4="", A5=""), A2, IF(AND(A2="", A3<>"", A4="", A5=""), A3, IF(AND(A2="", A3="", A4<>"", A5=""), A4, IF(AND(A2="", A3="", A4="", A5<>""), A5, IF(AND(A2<>"", A3<>"", A4="", A5=""), A2&" and "&A3, IF(AND(A2<>"", A3="", A4<>"", A5=""), A2&" and "&A4, IF(AND(A2<>"", A3="", A4="", A5<>""), A2&" and "&A5, IF(AND(A2="", A3<>"", A4<>"", A5=""),A3&" and "&A4, IF(AND(A2="", A3<>"", A4="", A5<>""), A3&" and "&A5, IF(AND(A2="", A3="", A4<>"", A5<>""),A4&" and "&A5, IF(AND(A2<>"", A3<>"", A4<>"", A5=""), A2&", "&A3&", and "&A4, IF(AND(A2<>"", A3<>"", A4="", A5<>""), A2&", "&A3&", and "&A5, IF(AND(A2<>"", A3="", A4<>"", A5<>""), A2&", "&A4&", and "&A5, IF(AND(A2="", A3<>"", A4<>"", A5<>""), A3&", "&A4&", and "&A5, A2&", "&A3&", "&A4&", and "&A5))))))))))))))
Here it is via Excel:
Mary Anne - I'm such a nerd that I had to do this. Here is the VBA solution, and you can have as many names as you want! Paste this code into a new module in the workbook (go to Developer -> Visual Basic, then Insert -> New Module, and paste), then you can use it in your worksheet like a regular function. Just give it the range where the names are and you should be good to go! -Matt
Function CreatePhrase(NamesRng As Range) As String
'Creates a comma-separated phrase given a list of words or names
Dim Cell As Range
Dim l As Long
Dim cp As String
'Add commas between the values in the cells
For Each Cell In NamesRng
If Not IsEmpty(Cell) And Not Cell.Value = "" And Not Cell.Value = " " Then
cp = cp & Cell.Value & ", "
End If
Next Cell
'Remove trailing comma and space
If Right(cp, 2) = ", " Then cp = Left(cp, Len(cp) - 2)
'If there is only one value (no commas) then quit here
If InStr(1, cp, ",", vbTextCompare) = 0 Then
CreatePhrase = cp
Exit Function
End If
'Add "and" to the end of the phrase
For l = 1 To Len(cp)
If Mid(cp, Len(cp) - l + 1, 1) = "," Then
cp = Left(cp, Len(cp) - l + 2) & "and" & Right(cp, l - 1)
Exit For
End If
Next l
'If there are only two words or names (only one comma) then remove the comma
If InStr(InStr(1, cp, ",", vbTextCompare) + 1, cp, ",", vbTextCompare) = 0 Then
cp = Left(cp, InStr(1, cp, ",", vbTextCompare) - 1) & Right(cp, Len(cp) - InStr(1, cp, ",", vbTextCompare))
End If
CreatePhrase = cp
End Function
Hope that helps!
Matt, via ExcelArchitect.com
VBA is simpler. A formula is quite complicated, since Excel has no native functions allowing concatenation of a range. However, given that you have written that you would have up to eight animals, it is doable with the following formula which concatenates the contents of A1:A8 according to your rules. You can change those locations in the formula in the obvious locations.
I made one change: I may be wrong, but I believe English rules indicate that the comma preceding the last and should be omitted, so I did so. It could be added in if necessary. EDIT: Further investigation reveals a difference between US and UK rules: US rules are as you requested, UK rules omit the comma before the conjunction. I will modify the formulas and UDF to comply with US conventions.
In the formulas, the modification is to place a comma immediately prior to the and. The change in the UDF is likewise minor.
The formula was constructed from the following sequences:
So putting those formulas together, so as only to refer to A1:A8, we wind up with this monster:
=SUBSTITUTE(IFERROR(SUBSTITUTE(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2),",",",and ",LEN(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2))-LEN(SUBSTITUTE(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2),",",""))),MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2)),",",", ")
Here is a VBA solution which will allow for any number of items; it concatenate according to the same rules as above.
Option Explicit
Function ConcatRangeWithAnd(RG As Range, Optional Delim As String = ", ")
Dim COL As Collection
Dim C As Range
Dim S As String
Dim I As Long
Set COL = New Collection
For Each C In RG
If Len(C.Text) > 0 Then COL.Add C.Text
Next C
Select Case COL.Count
Case 0
Exit Function
Case 1
ConcatRangeWithAnd = COL(1)
Case 2
ConcatRangeWithAnd = COL(1) & " and " & COL(2)
Case Else
For I = 1 To COL.Count - 1
S = S & COL(I) & ", "
Next I
ConcatRangeWithAnd = S & "and " & COL(COL.Count)
End Select
End Function
With the new TEXTJOIN function, this can be done very easily.
Step 1: Use TEXTJOIN function with the ", " delimiter, and set the ignore_empty to TRUE. This will give you comma separated, concatenated string, ignoring the blank values.
Step 2: Count the number of not blank entries in the list using COUNTA function. And subtract 1 from it. You might want to floor the value at 1 using the MAX function at this point.
Step 3: Use the SUBSTITUTE function to replace the last instance of the comma, which was calculated in Step 2, with a " and ".
Putting it all together:
=SUBSTITUTE(TEXTJOIN(", ",TRUE,A1:A14),", "," and ",MAX(1,COUNTA(A1:A14)-1))
Plug in any Range you want instead of A1:A14 in the above formula, and you will get a comma separated concatenate with an and before the last word.
Regarding duplicates:
Firstly, I really love Matt's solution and I've added this to my collection of custom functions.
What I do miss though is the possibility to remove duplicates from the phrase without removing them from the original range.
As you can't create a virtual range (a range that you can just play with in VBA independently from your source data), the solution would probably involve converting the range to an array, running some deduplication code and then creating the phrase from that.
My solution (albeit inelegant) is just to use the UNIQUE and FILTER functions to get a deduplicated list elsewhere on the spreadsheet (can be hidden if it bothers you) and to use Matt's function on that.
=UNIQUE(FILTER(yourRange,yourRange<>""))

Insert text after 4 decimal variable number in Excel VBA

I have been working on a processing file in Excel VBA to take raw data from our very old database software and arrange the .txt output into an Excel database. The problem I am running into is that I need to add a delimiting marker after a number sequence to tell Excel to split this part of the text string into two columns. The number sequence ranges from 0.0000 to 999.9999 and I need to keep this number intact. The number is always represented with four decimal points.
Could someone please give me some direction on how to accomplish adding a character (like Æ) after a variable number sequence for all cells within a column?
Raw data:
A1: Hello World, This is a test of 22.5010 and is only a test
A2: Hello World, This is a test of 0.1250 and is only a test
A3: Hello World, This is a test of 0.0000 and is only a test
Final Result:
A1: Hello World, This is a test of 22.5010Æ and is only a test
A2: Hello World, This is a test of 0.1250Æ and is only a test
A3: Hello World, This is a test of 0.0000Æ and is only a test
Thank you in advance!
Don't forget to add the proper references to the project (in tab Tools>References):
Add "Microsoft VBScript Regular Expressions 5.5"
Now add a button onto your excel page and double click on the button to open the proper macro page. Add his code to the macro page it just opened:
Sub Button1_Clic()
'
' Button1_Clic Macro
'
'This sub will scan through the first column (A) and add Æ to the end of
'numbers with the proper format.
On Error Resume Next 'Needed incase the line doesn't have a number
Dim i As Integer
Dim NumberStr As Object
Dim RegEx As RegExp
Set RegEx = New RegExp
With RegEx
.IgnoreCase = True
.Global = False
.Pattern = "\d{1,3}\.\d{4}"
End With
For i = 1 To 500 'Change last value to the last row in the column
Set NumberStr = RegEx.Execute(Range("A" & i).Value)
Range("A" & i).Value = RegEx.Replace(Range("A" & i).Value, NumberStr(0).Value & "Æ")
Next i
End Sub
This regex "\d{1,3}\.\d{4}" reads :
- Numeric character between 1 to 3 times repeated \d{1,3}
- Dot \.
- Numeric character repeated 4 times \d{4}
More info on Regexes.
This will work even if a period occurs after the number.
This won't work if the period occurs before the number
Before:
A1: Hello World, This is a test of 0.1250 and is only a test
A2: Hello World, This is a test of 0.1250 an.d i.s on.ly a te.st
A3: Hello W.orld, This is a test of 0.1250 and is only a test
After:
A1: Hello World, This is a test of 0.1250Æ and is only a test
A2: Hello World, This is a test of 0.1250Æ an.d i.s on.ly a te.st
A3: Hello W.orldÆ, This is a test of 0.1250 and is only a test
Code:
For cell = 1 To 3 ' ENTER MAX ROW
Range("A" & cell).Value = Left(Range("A" & cell).Value, InStr(Range("A" & cell).Value, ".") + 4) & "Æ" & Right(Range("A" & cell).Value, (Len(Range("A" & cell).Value) - 4) - InStr(Range("A" & cell).Value, "."))
Next cell

Excel Formula with concatenate

I am trying to generate a customer number using the first three letters of the customers last name, the first name initial and middle initial, followed by the last four of their phone number. How would I do this? All I need is the formula.
First_Name Middle_Initial Last_Name Street_Address City State Zip Phone
Nathaniel E. Conn 6196 View Ct Lancing TN 37770 567-273-3956
Something like this (assuming a table with [structured-references], fill in the actual cell names if not):
=LEFT([LastName] & "---", 3)
& LEFT([FirstName] & "-", 1)
& LEFT([MiddleInitial] & "-", 1)
& RIGHT([PhoneNumber] & "----", 4)
I have used dashes ("-") to fill in any spaces where the field might be smaller than the number of characters you need from it. You can change them to any fill character that suits you.
Well, it depends on if each piece of data has its own column, looks like it does.
You can use the left/right functions to parse the data out of your columns.
=CONCATENATE(RIGHT(C1,3) & LEFT(A1,1) & LEFT(B3,1) & RIGHT(H1,4))
I would do:
=MID(CELL_LAST_NAME;1;3)&MID(CELL_FIRST_NAME;1;1)&MID(CELL_MIDDLE_NAME;1;1)&MID(CELL_PHONE;LEN(CELL_PHONE)-3;4)

String conversion with TEXT formula character limit?

I am attempting to format a single number stored as a text value.
For example, I would like to convert:
5145350002005000080
To:
5145-350002-00500-0080
The formula I am using is:
=text(A1,"0000-000000-00000-0000")
The output I am receiving is:
5145-350002-00500-0000
Why are the last 4 characters "0000" instead of "0080" as I would expect? Is there a character limit, or is my formula incorrect?
Quote from Large Number Arithmetic:
The limit in Excel is 15 significant digits in a number. Enter a 16
digit credit card number and 1234567890123456 will become
1234567890123450.
Actually, even 5145350002005001111 will result in 5145-350002-00500-0000.
Moreover, take a look at formula bar when your input cell is selected - for my Excel 2007 I see:
Hope that was helpful)
EDITED:
As a solution to solve the task - keep your numbers formatted as text and use the following formula:
=LEFT(A1,4)&"-"&MID(A1,5,6)&"-"&MID(A1,11,5)&"-"&RIGHT(A1,4)
Here is a custom function. Place it in a regular code module of the workbook and you can call it in the cell by =FormatLargeNumber("A1")
Public Function FormatLargeNumber(val As String)
'This function parses extremely large numbers per your example.
' Modify as needed.
FormatLargeNumber = Left(val, 4) & "-" _
& Mid(val, 5, 6) & "-" & _
Mid(val, 11, 5) & "-" & _
Right(val, 4)
End Function

Resources