Morya1
asked on
Excel 2007 - If column starts with the number 5, add three zeroes, if has spaces...
I have a column of credit card numbers, but to use these numbers in my work, I need to insert three zeroes in front of every card number that starts with the number 5. Also, I need to have removed any spaces in the numbers.
So if a value in the column had a number like this: 5432 7892 0987 6665 (not a real number, btw) the function would take it and change it to look like this: 0005432789209876665
And the format of the cell for the reformatted credit card number needs to be in a text format.
Seems simple enough, but beyond me.
So if a value in the column had a number like this: 5432 7892 0987 6665 (not a real number, btw) the function would take it and change it to look like this: 0005432789209876665
And the format of the cell for the reformatted credit card number needs to be in a text format.
Seems simple enough, but beyond me.
If the data is int A1, then use this
=IF(LEFT(A1,1)="5","000"&S UBSTITUTE( A1," ",""))
and copy down
=IF(LEFT(A1,1)="5","000"&S
and copy down
Try this formula:
=TEXT(SUBSTITUTE(A1," ",""),"0000000000000000000 ")
See attached example.
Book1.xls
=TEXT(SUBSTITUTE(A1," ",""),"0000000000000000000
See attached example.
Book1.xls
or this
=IF(MID(A1,1,1)="5", "000" & SUBSTITUTE(A1," ", ""), SUBSTITUTE(A1," ", ""))
it adds "000" to it if it starts with "5" (also replaces " " with "")
=IF(MID(A1,1,1)="5", "000" & SUBSTITUTE(A1," ", ""), SUBSTITUTE(A1," ", ""))
it adds "000" to it if it starts with "5" (also replaces " " with "")
Sorry, this one better
=IF(LEFT(A1,1)="5","000"&S UBSTITUTE( A1," ",""),""&A1)
If it does not start with 5, it just copies the data, but turns it into TEXT
And this last one below if you want to always remove spaces even if it does not start with 5
=IF(LEFT(A1,1)="5","000"," ")&SUBSTIT UTE(A1," ","")
=IF(LEFT(A1,1)="5","000"&S
If it does not start with 5, it just copies the data, but turns it into TEXT
And this last one below if you want to always remove spaces even if it does not start with 5
=IF(LEFT(A1,1)="5","000","
If your data is in cell A1, then use this formula in B1
=IF(LEFT(A1,1)="5", "000" & SUBSTITUTE(A1," ",""), SUBSTITUTE(A1," ",""))
=IF(LEFT(A1,1)="5", "000" & SUBSTITUTE(A1," ",""), SUBSTITUTE(A1," ",""))
ASKER
Looks like all these are copy downs, aren't they? I'm looking for a script for more automation. It also has to replace the original value in the cell, not create a new column or cell. thanks
If you're looking for a VBA script, perhaps something like this...
Sub ConvertCCs()
Dim c As Range
For Each c In Selection.Cells
If CStr(Left(Trim(c.Value), 1)) = "5" Then
c.NumberFormat = "@"
c.Value = "000" & Trim(c.Value)
End If
Next c
End Sub
HTH,
Zack
Sub ConvertCCs()
Dim c As Range
For Each c In Selection.Cells
If CStr(Left(Trim(c.Value), 1)) = "5" Then
c.NumberFormat = "@"
c.Value = "000" & Trim(c.Value)
End If
Next c
End Sub
HTH,
Zack
Oh, btw, you would need to select the cells you want this done on prior to running this routine, if you didn't notice in the code. Sorry for not mentioning it at the last post.
Try this code
Sub test()
For i = 1 To 100
myVal = Range("A" & i).Value
If myVal <> "" Then
If Left(myVal, 1) = "5" Then
myNewVal = "'" & ("000" & Replace(myVal, " ", ""))
Else
myNewVal = "'" & (Replace(myVal, " ", ""))
End If
Range("A" & i).Value = myNewVal
End If
Next i
End Sub
firefytr's code looks good. It touches ONLY the cells that start with "5".
Would tweak a little
Would tweak a little
Sub ConvertCCs()
Dim c As Range
For Each c In Selection.Cells
If Left(Trim(c.Value), 1) = "5" Then
c.NumberFormat = "@"
c.Value = "000" & Replace(c.Value, " ", "")
End If
Next c
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I did not include any cells value which didn't start with "5", as it wasn't stated. It seemed the OP was still speaking of the "5" values. Although you catch me second guessing myself if I may have misunderstood the specifications on the original question.
And I guess you wouldn't need the Cstr() on that value, would ya? :)
And I guess you wouldn't need the Cstr() on that value, would ya? :)
ASKER
Sorry, guys, I can't get any of them to work so I must be doing something wrong.
I am inserting the code by clicking on the Macros ribbon item under the Developer menu. When the Macro dialogue box comes up I click Edit, paste the code and save. Then run the code and nothing! It just sits there. Any ideas?
I am inserting the code by clicking on the Macros ribbon item under the Developer menu. When the Macro dialogue box comes up I click Edit, paste the code and save. Then run the code and nothing! It just sits there. Any ideas?
ASKER
I'm sure it works - just have to implement it correctly. Thank you!
B1 = ="000" & SUBSTITUTE(A1," ", "")