Excel VBA

What I am trying to do is write a macro that applies a formatting to the the B colum n from cell 2 down. It will duplicate the value in column a - but add dashes to it - like from 1234567891234 to this 12345-67891-234 in that format too 5/5/3

This is what I have - and I have changed it many times already

appreciate your help

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
Dim istart As Long

Set i = 2 - 1000

Set istart = B2

Do Until Record Is Null

Sheets("ProdAtt").Range.istart = Mid(A, 1, 5) & "-" & Mid(A, 6, 5) & "-" & Mid(A, 11, 3)


End Sub

Open in new window

dawber39Database Analyst / Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Glenn RayExcel VBA DeveloperCommented:
I'm restating your question to make sure I understand what you want to happen.

You would like a macro to automatically populate a formatted value in column B whenever a new value in column A is added or changed.  The value in B should be of the following format:

1) What is "Record"?   There is no built-in name or operation in VBA for this.
2) What happens if the value in A is not exactly 13 characters long?
3) Do you want this to run every time ANY cell is changed on the worksheet?  

Glenn RayExcel VBA DeveloperCommented:
Here's an example of a way to produce a formatted string like you described:
Option Explicit
Dim boolChange As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
    If boolChange Then Exit Sub
    Dim rng As Range
    Dim c As Object
    Set rng = Range("a2:a1000")
    For Each c In rng
        If c.Value <> "" Then
            boolChange = True
            If Len(c.Value) = 13 Then
                c.Offset(0, 1).Value = Mid(c, 1, 5) & "-" & _
                          Mid(c, 6, 5) & "-" & _
                          Mid(c, 11, 3)
                c.Offset(0, 1).Value = "Not a Valid Value"
            End If
            boolChange = False
            Exit Sub
        End If
    Next c

End Sub

Open in new window

If the value in column A does not have 13 characters, a message will display in column B.  This only affects rows 2:1000; that can easily be optimized, but I wanted you to see a structure that was similar to your initial attempt.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob HensonFinance AnalystCommented:
So you want the 13 digit value in column replicated in column B but with the dashes inserted.

Why not just the MID function like you have done in the code but in a formula in column B? As column A expands a simple double click of the bottom right hand corner of the last formula in column B will populate the rest of the cells in column B.

Alternatively, you can use Custom format, either in column A or in column B. If in column B the formula in B would simply be =A2 and then custom format would be:


The dashes would only appear in the formatting not in the cell contents.

Rob H
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

dawber39Database Analyst / Application DeveloperAuthor Commented:
Perfect - Thank you
dawber39Database Analyst / Application DeveloperAuthor Commented:
Awesome as always
Glenn RayExcel VBA DeveloperCommented:
I agree with Rob that - if you didn't absolutely need the new, 15-character string with the hyphens as a value - either using a concatenated function or a custom format would work:

in cell B2, copied down:
=IF(LEN(A2)=13,LEFT(A2,5)&"-"&MID(A2,6,5)&"-"&RIGHT(A2,3),"Not a valid value")

apply this custom format:

Only problem with the custom format is that it formats right-to-left, so if the value in column A has anything other than 13 characters, you'll get an undesirable result. Also, the underlying value will still be a number and not a string.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.