Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Add : ever two numbers in a cell

Posted on 2013-01-10
Medium Priority
310 Views
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
Question by:Edward Pamias
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 49

Expert Comment

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
``````
0

LVL 50

Expert Comment

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 49

Accepted Solution

Martin Liss earned 2000 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
``````
0

LVL 19

Author Comment

ID: 38762807
@MartinLiss

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

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 49

Expert Comment

ID: 38762814
I updated the code. Change conts to Const
0

LVL 19

Author Comment

ID: 38762887
@Martinlis

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

00:aa:bbcc:dd:ee
0

LVL 49

Expert Comment

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
``````
0

LVL 19

Author Comment

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!

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 19

Author Closing Comment

ID: 38762970
Thank you very much! This worked great!
0

LVL 49

Expert Comment

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

Marty - MVP 2009 to 2012
0

LVL 29

Expert Comment

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

## Featured Post

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will diâ€¦
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns overâ€¦
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
###### Suggested Courses
Course of the Month8 days, 4 hours left to enroll