Solved

Excel 2007 - If column starts with the number 5, add three zeroes, if has spaces...

Posted on 2010-11-15
15
757 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:Morya1
  • 3
  • 3
  • 3
  • +3
15 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 34139382
A1 = 5432 7892 0987 6665
B1 = ="000" & SUBSTITUTE(A1," ", "")
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34139396
If the data is int A1, then use this

=IF(LEFT(A1,1)="5","000"&SUBSTITUTE(A1," ",""))

and copy down
0
 
LVL 24

Expert Comment

by:broomee9
ID: 34139408
Try this formula:

=TEXT(SUBSTITUTE(A1," ",""),"0000000000000000000")

See attached example.
Book1.xls
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34139414
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 "")
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34139421
Sorry, this one better

=IF(LEFT(A1,1)="5","000"&SUBSTITUTE(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","")&SUBSTITUTE(A1," ","")
0
 
LVL 12

Expert Comment

by:sdwalker
ID: 34139433
If your data is in cell A1, then use this formula in B1

=IF(LEFT(A1,1)="5", "000" & SUBSTITUTE(A1," ",""), SUBSTITUTE(A1," ",""))
0
 

Author Comment

by:Morya1
ID: 34139447
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:Zack Barresse
ID: 34139471
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
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 34139478
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.
0
 
LVL 12

Expert Comment

by:sdwalker
ID: 34139505
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

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34139527
firefytr's code looks good.  It touches ONLY the cells that start with "5".
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

Open in new window

0
 
LVL 12

Accepted Solution

by:
sdwalker earned 250 total points
ID: 34139568
My code works, but if you prefer the format of firefytr's code, you would need to change to ...
Sub ConvertCCs()
    Dim c As Range
    For Each c In Selection.Cells
        c.NumberFormat = "@"
        If Left(Trim(c.Value), 1) = "5" Then
            c.Value = "000" & Replace(c.Value, " ", "")
        Else
            c.Value = Replace(c.Value, " ", "")
        End If
    Next c
End Sub

Open in new window

0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 34139641
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?  :)
0
 

Author Comment

by:Morya1
ID: 34142702
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?
0
 

Author Closing Comment

by:Morya1
ID: 34142917
I'm sure it works  - just have to implement it correctly. Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now