Solved

Excel - count number in cell, based on count, add number at start of cell.

Posted on 2013-05-22
5
345 Views
Last Modified: 2013-05-22
I need a VB macro that counts the number of numbers in a cell A1 and if the count is 7, to update the cell with a given number before it, in cell D1, in this example, the value '0.'

So if A1 = 1234567, then the count in B1 = LEN(A1), as 3. Then D1 = 01234567, as the updated value.
0
Comment
Question by:Osley
5 Comments
 
LVL 9

Expert Comment

by:jsdray
ID: 39189703
not following you... you lost me with the "the count in B1 = LEN(A1), as 3"
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 230 total points
ID: 39189709
I agree that the question is not clear.  Does it have to be VBA or can we just do this with a basic formula?  For example in D1 you could have:
=IF(LEN(A1)=7,"0" & A1, A1)

Open in new window


-Bear
0
 
LVL 20

Assisted Solution

by:ltlbearand3
ltlbearand3 earned 230 total points
ID: 39189716
if you really want something in VBA this will give you the basis for what you need:

Public Sub AddZero()
    If Len(Cells(1, 1)) = 7 Then
        Cells(1, 4).Value = "0" & Cells(1, 1).Value
    End If
End Sub

Open in new window


-Bear
0
 
LVL 81

Expert Comment

by:byundt
ID: 39189736
Are you looking to pad with zeros so there are 8 digits? If so, why not use custom format:
0000000#
No VBA necessary for this approach.

Or you can convert the number to text with a formula:
=TEXT(A1,"0000000#")
0
 

Author Comment

by:Osley
ID: 39189750
Thanks for the 'if' function. Worked well.
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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

867 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

18 Experts available now in Live!

Get 1:1 Help Now