Macro: if column contains any number of possible strings, add zeros to front of number in that column

sndameron

New Member
Joined
Sep 21, 2014
Messages
30
I have a column that contains numbers whose leading zeros disappeared thanks to importing into excel. I want to reduce the number of steps this involves so I really don't want to open it in some other program to fix it then import. I'm already running a macro on this for something else, and would love to just add a little bit of script into it to fix this issue. So, I need a macro written that will look at the column with say a header name of ID. If the adjacent column called NAME is any of the following text: apple, orange, banana, blueberry, then add zeros to the beginning of the numbers of column ID so that its total digits is 9. If it does not match criteria, keep it at whatever number is already in the adjacent cell. It would be great if it could just replace the number in the ID column, but I'm okay if it gets moved to another column if that makes it simpler. For example:
IDNAMECONVERT TO
1234banana000001234
45cucumber45
9876orange000009876
343apple000000343

<tbody>
</tbody>
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this :
Code:
Sub FT()
Dim ray: ray = Array("apple", "banana", "orange")
Dim rng As Range: Set rng = Range([B1], Cells(Rows.Count, "B").End(xlUp))
Rng.AutoFilter Field:=1, Criteria1:=ray, Operator:=xlFilterValues
rng.SpecialCells(xlCellTypeVisible).NumberFormat = "000000000"
rng.AutoFilter
End Sub
 
Last edited:
Upvote 0
Amended :
Code:
Sub FT()
Dim ray: ray = Array("apple", "banana", "orange")
Dim rng As Range: Set rng = Range([B1], Cells(Rows.Count, "B").End(xlUp))
Rng.AutoFilter Field:=1, Criteria1:=ray, Operator:=xlFilterValues
rng.Offset(0,-1).SpecialCells(xlCellTypeVisible).NumberFormat = "000000000"
rng.AutoFilter
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,874
Messages
6,127,473
Members
449,384
Latest member
purevega

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