VBA Number formatting with an extensive list

Lizzielady

New Member
Joined
May 6, 2011
Messages
5
Hi guys!

Having a problem with one of my bits of code, hope someone can help :confused: ?!

I have a list of employee numbers which are either 3 or 4 digits long, the list can be anything from 1 row and up. It depend on what absences have been taken in that department.

I have wrote a macro which basically runs an import sheet, but the system in imports too needs the numbers to be in six digit style, so either 00 or 000 in front of number.

I wrote the below and it works, unless there is only one employee to import, then I get a 'Type Mis-match' error....am I missing something?

Dim a
Dim i As Long

With Range("A2", Range("A" & rows.Count).End(xlUp))
.NumberFormat = "@"
a = .Value
For i = 1 To UBound(a, 1)
a(i, 1) = Right("00000" & a(i, 1), 6)
Next i
.Value = a
End With

Thanks :)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
'a' is not an array if there is only one number in A2. You can see this with stepping thru the code and with the Locals window displayed. Does that help?

For the code, you just need to test whether there are any values below the header row first, then, rather than depending upon there being at least two values and thus, an array, just use a For Each...Next:

Rich (BB code):
Option Explicit
    
Sub example()
Dim Cell As Range
    
    If Cells(Rows.Count, 1).End(xlUp).Row >= 2 Then
        With Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))
            .NumberFormat = "@"
            For Each Cell In .Cells
                Cell.Value = Right("000000" & Cell.Value, 6)
            Next
        End With
    End If
End Sub
 
Upvote 0
Thank you, works great!

Thank you for your explanation too, I'm still pretty new with coding so I have a lottttt to learn :LOL:
 
Upvote 0
Hi Lizzielady,

You are most welcome and glad that helped. No worries about learning, we all are, and at least for me, even when I think I have a few things down pat, I forget stuff!

Have a great day,

Mark
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,548
Members
449,038
Latest member
Guest1337

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