My First Do Until Loop - Not working

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi all,

I have a column of data in a table that contains numbers. For various reasons relating to lookups, I need to ensure that the "number" is in fact a text field that contains exactly 6 digits, with leading zeroes for smaller values.

I have identified the column and for the sake of a quick fix, I am writing directly to the sheet (I will in turn move to an array but this fix is needed quickly and there are only 300 odd rows).

My code looks like this:

VBA Code:
Set CellA = WS1.Range("A1:Z1").Find("Item number")
LCol = WS1.ListObjects(1).Range.Rows.Count

Set WorkingRange = WS1.Range(Cells(2, CellA.Column), Cells(LCol, CellA.Column))

For Each ItemCell In WorkingRange
    
    Do Until Len(ItemCell) = 6
    
        ItemCell.Value = "0" & ItemCell.Value
    
    Loop

Next ItemCell

It finds everything "correctly" but the loop action (adding the 0) doesn't seem to work, so the loop just continues around the first row forever (i.e. until I stop it).

I have missed something obvious I know - can anybody spot it?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I think I've figured it out now - I had to set the column to be text format.
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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