Solved

Add : ever two numbers in a cell

Posted on 2013-01-10
11
273 Views
Last Modified: 2013-01-10
All,

I get a spreadsheet every day with over 100 mac address. Unfortunately the mac address don't have the colon separating the numbers. How do I go about formatting a cell to add the colons?
0
Comment
Question by:Edward Pamias
11 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38762759
You can use this macro. It assumes no headings and that the data is in column A.

Sub MacAddress()
Dim lngLastRow As Long
Dim lngIndex As Long
Dim V As String
lngLastRow = Range("A65536").End(xlUp).Row
' Change 1 to 2 if you have a heading
For lngIndex = 1 To lngLastRow
' 1 in these two lines represents column A
    V = Cells(lngIndex, 1).Value
    Cells(lngIndex, 1).Value = Mid(V, 1, 2) & "-" & Mid(V, 3, 2) & "-" & Mid(V, 5, 2) & Mid(V, 7, 2) & "-" & Mid(V, 9, 2) & "-" & Mid(V, 11, 2)
Next

Open in new window

0
 
LVL 50

Expert Comment

by:barry houdini
ID: 38762763
I assume there may be letters in which case you can't use number formatting, if you have an entry like this in A2

0123456789ab

and you want it to look like this

01:23:45:67:89:ab

You could use this formula in B2

=REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(A2,3,0,":"),6,0,":"),9,0,":"),12,0,":"),15,0,":")

regards, barry
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 38762768
To ammend my code a bit to make it clearer.

Sub MacAddress()
Dim lngLastRow As Long
Dim lngIndex As Long
Dim V As String
Const COL = 1 ' = column A

lngLastRow = Range("A65536").End(xlUp).Row

For lngIndex = 1 To lngLastRow
    V = Cells(lngIndex, COL).Value
    Cells(lngIndex, COL).Value = Mid(V, 1, 2) & "-" & Mid(V, 3, 2) & "-" & Mid(V, 5, 2) & Mid(V, 7, 2) & "-" & Mid(V, 9, 2) & "-" & Mid(V, 11, 2)
Next
End Sub

Open in new window

0
 
LVL 14

Author Comment

by:Edward Pamias
ID: 38762807
@MartinLiss

I am using office 2010 if that makes a difference. I am getting a compile error. See below

Sub MacAddress()
Dim lngLastRow As Long
Dim lngIndex As Long
Dim V As String
conts COL = 1 ' = column A
lngLastRow = Range("A65536").End(xlUp).Row

For lngIndex = 1 To lngLastRow
    V = Cells(lngIndex, COL).Value
    Cells(lngIndex, COL).Value = Mid(V, 1, 2) & "-" & Mid(V, 3, 2) & "-" & Mid(V, 5, 2) & Mid(V, 7, 2) & "-" & Mid(V, 9, 2) & "-" & Mid(V, 11, 2)
Next
End Sub
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38762814
I updated the code. Change conts to Const
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Author Comment

by:Edward Pamias
ID: 38762887
@Martinlis

We are close I am missing a colon between BB and CC



00:aa:bbcc:dd:ee
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38762936
Sub MacAddress()
Dim lngLastRow As Long
Dim lngIndex As Long
Dim V As String
Const COL = 1 ' = column A
lngLastRow = Range("A65536").End(xlUp).Row

For lngIndex = 1 To lngLastRow
    V = Cells(lngIndex, COL).Value
    Cells(lngIndex, COL).Value = Mid(V, 1, 2) & ":" & Mid(V, 3, 2) & ":" & Mid(V, 5, 2) & ":" & Mid(V, 7, 2) & ":" & Mid(V, 9, 2) & ":" & Mid(V, 11, 2)
Next
End Sub

Open in new window

0
 
LVL 14

Author Comment

by:Edward Pamias
ID: 38762966
@ Martinls

I got it.. I put my glasses on this time and saw you were missing a ":"  so I added it and it works great!  Thanks!

Sub MacAddress()
Dim lngLastRow As Long
Dim lngIndex As Long
Dim V As String
Const COL = 1 ' = column A
lngLastRow = Range("A65536").End(xlUp).Row

For lngIndex = 1 To lngLastRow
    V = Cells(lngIndex, COL).Value
    Cells(lngIndex, COL).Value = Mid(V, 1, 2) & ":" & Mid(V, 3, 2) & ":" & Mid(V, 5, 2) & ":" & Mid(V, 7, 2) & ":" & Mid(V, 9, 2) & ":" & Mid(V, 11, 2)
Next
End Sub
0
 
LVL 14

Author Closing Comment

by:Edward Pamias
ID: 38762970
Thank you very much! This worked great!
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38763070
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38763087
The Format function is another option:
Cells(lngIndex, COL).Value = Format(V, "&&:&&:&&:&&:&&:&&")
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

762 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

26 Experts available now in Live!

Get 1:1 Help Now