Looping with leading zero number

nora1212

New Member
Joined
Jan 29, 2021
Messages
15
Office Version
  1. 2010
Platform
  1. Windows
Hi

I'm trying to use the find function to loop through and find the numbers 01, 02, 03 through to 15 (and then do something etc). However, vba will not accept any leading zeros e.g. when i type "For c = 01 To 15", it changes it to "For c = 1 To 15". How can I format the code to accept a leading zero on numbers 1-9 and then no zero from 10 onwards?

Thanks for your help
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I don't think you need to.
Just do:
VBA Code:
For c = 1 to 15
   f = Format(c,"00")
and search for "f" instead of "c", which will contain leading zeroes.

Just make sure that the values you are searching for ACTUALLY have leading 0s, and are not just numbers with special formatting.
 
Upvote 0
Solution
You need to check your spreadsheet and confirm whether your numbers are actually numbers or text.
To display the leading zero in Excel you need to either format the number as "00" or enter the number as a text value.
The quickest way to check is to change the column format to Number with 2 decimal places. If the numbers change to showing 2 decimal places they are being recognised as numbers in Excel. If the do not change and continue to show 01 etc then they were text

If they are numbers formatted to 01 etc then For c = 1 to 15 should work fine.

If they are text then you need and extra step: (@Joe4 beat me to this part)
For c = 1 to 15 followed by f = Format(c,"00")
 
Upvote 0
I don't think you need to.
Just do:
VBA Code:
For c = 1 to 15
   f = Format(c,"00")
and search for "f" instead of "c", which will contain leading zeroes.

Just make sure that the values you are searching for ACTUALLY have leading 0s, and are not just numbers with special formatting.
Perfect Joe! Thanks very much.
 
Upvote 0
You need to check your spreadsheet and confirm whether your numbers are actually numbers or text.
To display the leading zero in Excel you need to either format the number as "00" or enter the number as a text value.
The quickest way to check is to change the column format to Number with 2 decimal places. If the numbers change to showing 2 decimal places they are being recognised as numbers in Excel. If the do not change and continue to show 01 etc then they were text

If they are numbers formatted to 01 etc then For c = 1 to 15 should work fine.

If they are text then you need and extra step: (@Joe4 beat me to this part)
For c = 1 to 15 followed by f = Format(c,"00")
Thanks also Alex! Team effort!
 
Upvote 0
You are welcome.
Glad we could help!
:)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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