Code to change multiple numbers at once

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,199
Office Version
  1. 2007
Platform
  1. Windows
Afternoon.

In a worksheet at column B is a list of customers names.
When a customer makes a return purchase their name is then followed by a number.
Example,

Tom Jones
Tom Jones 1
Tom Jones 2
Tom Jones 3

At first it was fine but now introducing a combobox list once the numbers are greater than 9 the list becomes out of order & looks a mess..

I would like a code on a command button etc that would change the number as the list is now nearly 1000 entries.
Example
Tom Jones No change
Tom Jones 1 Would then become Tom Jones 001
Tom Jones 2 Would then become Tom Jones 002
Tom Jones 3 Would then become Tom Jones 003

Many thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Code:
With Workbooks(REF).Sheets(REF)

LRow = .Cells(.Rows.Count, "B").End(xlUp).Row

For each c In .Range("B2:B" & LRow)
    If IsNumeric(Right(c,2) And Not IsNumeric(Mid(c,Len(c)-2,1) Then
        c.Value = Left(c, Len(c)-2) & "0" & Right(c,2)
    ElseIf IsNumeric(Right(c,1) Then
        c.Value = Left(c, Len(c)-1) & "00" & Right(c,1)
    End If
Next c

End With
 
Last edited:
Upvote 0
Hi,
I get a syntax error and below is shown

Code:
If IsNumeric(Right(c,2) And Not IsNumeric(Mid(c,Len(c)-2,1) Then
 
Upvote 0
another one for you
test on a copy of your worksheet!

Code:
Sub FormatNos()
    Dim Ws As Worksheet, Rng As Range, Cel As Range, Txt As String, S As Integer, L As Integer, Nbr As Integer
    Set Ws = ActiveSheet
    Set Rng = Ws.Range("B2", Ws.Range("B" & Rows.Count).End(xlUp))

    For Each Cel In Rng
        Txt = Cel.Value: L = Len(Txt): S = InStrRev(Txt, " "): Nbr = 0
        On Error Resume Next:  Nbr = CInt(Right(Txt, L - S)): On Error GoTo 0
        If Nbr > 0 Then Txt = Left(Txt, S) & Format(Nbr, "000")
        Cel = Txt
    Next
End Sub
 
Upvote 0
My bad, forgot two paranthesis:

should be
Code:
If IsNumeric(Right(c, 2)) And Not IsNumeric(Mid(c, Len(c) - 2, 1)) Then
 
Upvote 0
Hi,
I run the code but get a subscript out of range on this line of code.
Code:
With Workbooks(REF).Sheets(REF)

So my workbook is called DR & the worksheet in question is called POSTAGE
I then change the code to,
Code:
With Workbooks(DR).Sheets(POSTAGE)

But still the same message ?
 
Upvote 0
I always use REF in my code to indicate a reference to be added/changed to suit OP's code.

You're on the right path, but not quite there. There are three ways of referencing wokbooks/-sheets:

1. The name as a string. Either directly (Workbooks("name")), or indirectly with a variable:
Code:
wbname = "name"
With Workbooks(wbname)
'[...]

2. The index as an integer. Refers to the order in which all workbooks were opened. (1) is the first wb, (2) is the second. E.g. Workbooks(1). Variables can also be used:
Code:
wbindex = 3
With Workbooks(wbindex)
'[...]

3. The CodeName of a sheet. These are listed in the VB editor (Alt + F11). E.g. Sheet2



For more information read this webpage tutorial
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,268
Members
448,558
Latest member
aivin

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