Find number and add ' to begining

JonathanA

Board Regular
Joined
Jan 2, 2008
Messages
62
Whether there is a better way to deal with this I don't know but I have a file sent to me every week that I cannot alter or affect the format it comes in. It has a list of products some which are all numeric but most have a Letter prefix. I need to have all the numbers as text even if they are a number if you see what I mean.
My current solution (not really a solution) is to go into the file and replace say 12948 with '12948 and format to general, there are not too many of these and it's not a disaster but ideally I would love to have a macro do this for me, the numbers are always in the same column "B", from row 2 to unknown (end of file), additionally there are a couple of products that have a leading zero so I would need to replace 5252 with '05252, although these numbers can be specifically mentioned in the code as there are only a handfull of them.

I would guess I need to use "For each Cell in Range"
and some kind of replace function with an isnumeric test but that's as far as I have got, I haven't really got a clue where to start with the syntax.
Any tips/ideas greatly appreciated.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Code:
i = 0
Do
    i = i + 1
        
    If Len(Cells(i, 2).Value) = 4 Then
        Cells(i, 2).Value = "'0" & Cells(i, 2).Value
    Else
        Cells(i, 2).Value = "'" & Cells(i, 2).Value
    End If
Loop Until Cells(i + 1, 2).Value = ""
 
Upvote 0
Or

Code:
Sub NoToChr()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("B" & i)
        If IsNumeric(.Value) Then .Value = "'" & .Value
    End With
Next i
End Sub
 
Upvote 0
Just curious to know, is it that you want all the numeric data to display or precede a zero before any number in your column ?
 
Upvote 0
VoG remember there are some numbers with 4 digits as Excel trimmed them of the preceding 0.
 
Upvote 0
VoG remember there are some numbers with 4 digits as Excel trimmed them of the preceding 0.

Yep - my code doesn't address the leading zeros issue as I thought that only applied to a couple of specific codes.
 
Upvote 0
Thanks for all the speedy responses.

No, there are about 5 products with a leading zero that always gets lost in the transfer which I can specify, 5252 (should be '05252), 5253,5254,5253D,3463. The rest of the numbers just require a ' adding to them.
 
Upvote 0
Try

Code:
Sub NoToChr()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("B" & i)
        If .Value = 5252 Or .Value = 5353 Or .Value = 5254 Or .Value = "5253D" Or .Value = 3463 Then
            .Value = "'0" & .Value
        ElseIf IsNumeric(.Value) Then
            .Value = "'" & .Value
        End If
    End With
Next i
End Sub
 
Upvote 0
That works perfectly VoG, thanks very much.

Doing SQL for too long has withered my Excel VBA brain.;)
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,717
Members
449,050
Latest member
MiguekHeka

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