Extract and add sub strings into strings - string

The context here is that I'm trying to identify phone number patterns of a big messy column, and format them as this:
(CC) NNNN-NNNN
CC being two digit area code
There may be two or more numbers in the same cell(They need to remain in the same cell unfortunately), and need to be as follow :
(CC) NNNN-NNNN / (CC) NNNN-NNNN
The numbers are just raw digits, no spaces or characters, but are as TEXT and need to remain so because of the 15 digit limit on Excel
Now, I'm having problem with two cases
Case 1:
Two phones and one area code (18 digits)
Example : CCNNNNNNNNNNNNNNNN
What I need is a function that takes the first two characters from this string and add them at the eleventh spot of said string, resulting in a 20 char string
Case 2:
One phone and two same area code(12 digits)
CCCCNNNNNNNN
This one just need to remove the first two characters
Tried this way for case 1, but ended up with a 22 string char? not sure where I went wrong
s1 = Mid(Cells(j, 3), 1, 2)
s2 = Mid(Cells(j, 3), 3, 10)
s3 = Mid(Cells(j, 3), 11, 18)
s4 = s1 & s2 & s1 & s3

The big caveat here is the formatting of the existing data.... how do you know what digits are "area code" and what digits are phone number? You can do what you are trying to do here with Excel formulas without any VBA... For Case 1 you can use the formula below, replacing "A1" with the appropriate cell reference.
="("&MID(A1, 1, 2)&") "&mid(A1,3,4)&"-"&mid(a1,7,4)&" / ("&MID(A1, 1, 2)&") "&mid(A1,11,4)&"-"&mid(a1,15,4)
For Case 2 you can use the formula below.
="("&MID(A1, 1, 2)&") "&mid(A1,5,4)&"-"&mid(a1,9,4)
Does your solution have to be in vba?

The third parameter in Mid is the length of the extracted substring, not its final index. If it is omitted - the selection goes to the end of the string. The selection also goes to the end of the string if the length parameter exceeds the number of possible characters. s2 = Mid(Cells(j, 3), 3, 10) was thus of length 10 rather than the expected 8. Your case 1 would look something like this:
Sub test()
Dim s As String, t As String
s = "CCNNNNNNNNNNNNNNNN"
t = Mid(s, 1, 10) & Mid(s, 1, 2) & Mid(s, 11)
Debug.Print t
End Sub
Which has output:
CCNNNNNNNNCCNNNNNNNN

For your two cases:
=IF(LEN(A1)=18,REPLACE(A1,11,0,LEFT(A1,2)),IF(LEN(A1)=12,MID(A1,3,10)))
Not knowing the other formats or desired results, it is hard to expand on the formula. The formula will return FALSE if the length is other than 12 or 18.

Related

Need assistance with my Code for Length with Trim in VBA

I have my primary key in column F - 4 length char
I have entity name in column X - 1 to 40 length characters
I need to concatenate both columns and produce result in column CD.
But the length of the column CD should be less than 20 characters.
My below code works fine when total characters are more than 20, but it throws error when the concatenation is less then 20.
Can someone suggest where I am going wrong ?
Range("CD1") = "Standard_Beneficiary"
For a = 2 To Cells(Rows.Count, "F").End(xlUp).Row
Cells(a, "CD").Value = Cells(a, "F").Value & "_" & Left(Cells(a, "X"), Len(Cells(a, "X").Value) - 10)
Next a
Left(Cells(a, "X"),Len(Cells(a, "X").Value) - 10) Only works when the length of Cells(a,"X").Value is larger than 10 characters, otherwise the number of characters you want from the left side is negative, which is impossible of course.
You can either use an if-statement, or use something like
application.worksheetfunction.max(1,Len(Cells(a, "X").Value) - 10)
instead of Len(Cells(a, "X").Value) - 10, to make sure it's always a positive number.

Split Cell by Numbers Within Cell

I have some fields that need to be split up into different cells. They are in the following format:
Numbers on Mission 21 0 21
Numbers on Mission 5 1 6
The desired output would be 4 separate cells. The first would contain the words in the string "Numbers on Mission" and the subsequent cells would have each number, which is determined by a space. So for the first example the numbers to extract would be 21, 0, 21. Each would be in its own cell next to the string value. And for the second: 5, 1, 6.
I tried using a split function but wasn't sure how to target the numbers specifically, and to identify the numbers based on the spaces separating them.
Pertinent to your first case (Numbers on Mission), the simple solution could be as shown below:
Sub SplitCells()
Const RowHeader As String = "Numbers on Mission"
Dim ArrNum As Variant
ArrNum = Split(Replace(Range("A1"), RowHeader, ""), " ")
For i = 1 To UBound(ArrNum)
Cells(1, i + 2) = ArrNum(i)
Next
Cells(1, 2) = RowHeader
End Sub
The same logic is applicable to your second case. Hope this may help.
Unless I'm overlooking something, you may not need VBA at all. Have you tried the "Text to Columns" option? If you select the cell(s) with the information you would like to split up, and go to Data -> Text to Columns. There, you can choose "delimited" and choose a space as a delimiter, which will split your data into multiple cells, split by where the space is.
edit: Just realized that will also split up your string. In that case, when you are in 3rd part of the Text to Columns, choose a destaination cell that isn't the cell with your data. (I.E. if your data is in A1, choose B1 as destination, and it'll put the split info there. Then just combine the text columns with something like =B1&" "&C1&" "&D1)
I was able to properly split the values using the following:
If i.Value Like "*on Mission*" Then
x = Split(i, " ")
For y = 0 To UBound(x)
i.Offset(0, y + 1).Value = x(y)
Next y
End If

Macro that sorts call signs containing letters and numbers

All the call signs will be in column A and when the macro is run should sort them. The sort is case insensitive usually in all caps. A call sign consists of 1-2 letters(prefix), 1-2 numbers(numbers), then 1-3 letters(suffix) I want to sort each sign by the number, suffix, then prefix in that order.
W9K, BB3C, W9GFO, AB8VN, G3G, A77Bc, KB8HTM, K9DOG, W8AER, K1ZZ, W4BFT, W0CQC, WA6FV, W6TRW, AA5B, W4IY, N4C, K5UZ, K4LRG
I will bite. Half the fun of coding is solving a problem for the simple pleasure of knowing you figured it out.
Here is a user defined function (Formula) that you can use to convert the call sign into the format for sorting. Note the numeric portion is zero padded so ones and tens do not sort together before twos and twenties.
Option Explicit
Public Function FormatCallSign(aCell As Range)
Dim Nbr As String
Dim i As Integer
Dim tmp As String
Dim vList As Variant
For i = 1 To Len(aCell.Value)
If InStr(1, "1234567890", UCase(Mid(aCell.Value, i, 1))) > 0 Then
Nbr = Nbr & Mid(aCell.Value, i, 1)
tmp = tmp & ","
tmp = Replace(tmp, ",,", ",")
Else
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase(Mid(aCell.Value, i, 1))) > 0 Then
tmp = tmp & Mid(aCell.Value, i, 1)
End If
End If
Next i
vList = Split(tmp, ",")
FormatCallSign = vList(1) & Right("0" & Nbr, 2) & vList(0)
End Function
Put the formula in cell B2, for example by using the formulas command on the ribbon and selecting the function from the user defined section.
As asked earlier if the call sign had delimiters in it already, you could use a simple formula to rearrange the parts and exclude the delimiters.
=CONCATENATE(MID(A3,SEARCH("-",A3)+1,4),RIGHT("0"&MID(A3,SEARCH("/",A3)+1,SEARCH("-",A3)-SEARCH("/",A3)-1),2),LEFT(A3,SEARCH("/",A3)-1))
To build a formula like the above, start by constructing it in parts.
First write a Search function to find the "/", then copy it to find the "-"
Then write a mid function to get the characters to the right of the dash, left of the slash, then the numeric section. paste the formulas into a single formula for your masterpiece.
Since it makes better sense to keep the three elements in separate fields for simplified sorting, the above formula can be split into three separate formulas, one for each column.
=MID(A3,SEARCH("-",A3)+1,4)
=value(MID(A3,SEARCH("/",A3)+1,SEARCH("-",A3)-SEARCH("/",A3)-1),2))
=LEFT(A3,SEARCH("/",A3)-1)
This corrects sorting problems given the three elements are variable length.
The initial specification for callsign format is inaccurate, since they can begin with numbers or letters and a logical sort would be by ITU assigned prefix. A function would need a table lookup for country after it determined if the string after the forward slash was a valid country designation. This is actually a pretty complicated problem.

text to columns: split at the first number in the value

I have 1 column with about 60 cells with values or different length. Each (or at least most) of the values have a numeric characters in the value. I want to split the columns cells into more columns which I normally would do with the 'tekst to columns' function of excel.
But this function does not have an advanced option of splitting the value at the first numeric character. splitting based on spaces, comma etc. is possible but this does not help me.
Is there any way to divide the cells into 2 columns at the first number in the cell value?
I have looked at numerous other questions but none of them (or other internet fora) have helped me to split the value at the first number of the cell value.
Thanks #quantum285 for the answer. This routine works if the string contains one number. I changed the teststring to firstpart323secondpart.
then part1 returns 'firstpart32' and part2 return secondpart.
I tried to understand what happens in this code, please correct me if I'm wrong:
First, the lenght of the string is determined.
Secondly, for each position in this string is checked if it is numeric or not. But this check is dan from right to left? So in case of firstpart323secondpart: the length is 22.
then isnumeric() checks for every position from 1 to 22 if it is numeric and stops when it finds a number?
If so, part 1 is the the tekst before the value i, where i is the first position from right to left in the string where a number is found.
and part 2 is then the string on the right from this same position.
However, I am looking for a routine which find the first position from left to right (or the last position from right to left) where a number is, ...
So I changed the routine now, simply adjusting the for i = 1 to line:
Sub test()
For j = 4 To Cells(Rows.Count, 4).End(xlUp).Row
For i = Len(Cells(j, 4)) To 1 Step -1
If IsNumeric(Mid(Cells(j, 4), i, 1)) Then
Cells(j, 5) = Left(Cells(j, 4), i - 1)
Cells(j, 6) = (Right(Cells(j, 4), Len(Cells(j, 4)) - i + 1))
End If
Next i
Next j
End Sub
this almost perfectly works (except for a few cells which have multiple number combinations in the value (like: soup 10g 20boxes). But as these are only a few, I can adjust them by hand.
Thanks!
Sub test()
testString = "firstpart3secondpart"
For i = 1 To Len(testString)
If IsNumeric(Mid(testString, i, 1)) Then
part1 = Left(testString, i - 1)
part2 = (Right(testString, Len(testString) - i))
End If
Next i
MsgBox (part1)
MsgBox (part2)
End Sub
Use something like this within your loop.

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