Numbers digits

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi to all of you,
I would like to use a VBA code so that, to arrange the numbers in column “C” up to 7 digits and placed the letter “I” and “-“ at left side. Therefore that in case that, the number doesn't contains 7 digits should place "0" so that to complete it. I present below an extract of original data and expected result. Thank you all in advance.

Original data


1610956629689.png


Expected result


1610956649889.png
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Give this macro a try...
VBA Code:
Sub InvoiceNumbers()
  With Range("C2", Cells(Rows.Count, "C").End(xlUp))
    .Value = Evaluate("TEXT(" & .Address & ",""I-0000000"")")
  End With
End Sub
 
Upvote 0
Give this macro a try...
VBA Code:
Sub InvoiceNumbers()
  With Range("C2", Cells(Rows.Count, "C").End(xlUp))
    .Value = Evaluate("TEXT(" & .Address & ",""I-0000000"")")
  End With
End Sub
Hi Rick,
It works but it appears all numbers the same. Should arrange the numbers like "I-0005569" , "I-0045674" e.t.c. It gets the first number from row 2 and appears it to all below rows. However thank you for your support and attempt to resolve my issue. Hv a great day
 
Last edited:
Upvote 0
Therefore that in case that, the number doesn't contains 7 digits should place "0" so that to complete it
Youn can to do without VBA
Try i 'D' column then Copy/Paste as Values in 'C' column
Code:
=IF(LEN(C2)=1,"I-"&"000000"&C2,IF(LEN(C2)=2,"I-"&"00000"&C2,IF(LEN(C2)=3,"I-"&"0000"&C2,IF(LEN(C2)=4,"I-"&"000"&C2,IF(LEN(C2)=5,"I-"&"00"&C2,IF(LEN(C2)=6,"I-"&"0"&C2,IF(LEN(C2)=7,"I-"&C2,"")))))))
 

Attachments

  • Untitled-1.jpg
    Untitled-1.jpg
    27.4 KB · Views: 5
Upvote 0
Then try
VBA Code:
Sub InvoiceNumbers()
    a = Range("C2", Cells(Rows.Count, "C").End(xlUp))
    For i = 1 To UBound(a)
        a(i, 1) = "I-" & WorksheetFunction.Rept("0", 7 - Len(a(i, 1))) & a(i, 1)
    Next
    Range("C2", Cells(Rows.Count, "C").End(xlUp)) = a
End Sub

and formula
Excel Formula:
=IF(C2<>"","I-"&REPT("0",7-LEN(C2))&C2,"")
 
Upvote 0
Hi,
try formatting the Column & see if this will give required result

Assuming values in Column are as shown, just numbers then with sheet active, run this code once

VBA Code:
Sub FormatColumn()
    Columns(3).NumberFormat = """I-""0000000"
End Sub

You should see your cells formatted in required manner but the underlying cell value will just contain a number.
The format should now apply to any numeric values added in the column.

Dave
 
Upvote 0
Then try
VBA Code:
Sub InvoiceNumbers()
    a = Range("C2", Cells(Rows.Count, "C").End(xlUp))
    For i = 1 To UBound(a)
        a(i, 1) = "I-" & WorksheetFunction.Rept("0", 7 - Len(a(i, 1))) & a(i, 1)
    Next
    Range("C2", Cells(Rows.Count, "C").End(xlUp)) = a
End Sub

and formula
Excel Formula:
=IF(C2<>"","I-"&REPT("0",7-LEN(C2))&C2,"")

Hi Mohadin, it works perfect and nicely. Thank you for your time spent for my project. Hv a great day
 
Upvote 0
Hi dmt, Yes it works perfect and is an easy code. Thank you for your support. Hv a lovely day
 
Upvote 0

Forum statistics

Threads
1,215,398
Messages
6,124,699
Members
449,180
Latest member
craigus51286

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top