Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 17
Default VBA Macro change column contents

I have a column in a worksheet which has a list of 3 digit numbers. I want to
change the 3 digits to become 4 digits, the new fourth digit has to be a
leading zero, i.e 355 to become 0355. I prefer the format of the digits to be
a number but a text format would not be a problem.

The length of the digits in the column are not static. i.e one month there
may be 200 rows of data the next might be 300 and I would want the macro to
cater for this variation.

Thanks
--
James
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default VBA Macro change column contents

James,

A macro simply isn't necessary, select the column and apply a custom format of
0000
that's four zeroes
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"James C" wrote:

I have a column in a worksheet which has a list of 3 digit numbers. I want to
change the 3 digits to become 4 digits, the new fourth digit has to be a
leading zero, i.e 355 to become 0355. I prefer the format of the digits to be
a number but a text format would not be a problem.

The length of the digits in the column are not static. i.e one month there
may be 200 rows of data the next might be 300 and I would want the macro to
cater for this variation.

Thanks
--
James

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 17
Default VBA Macro change column contents

Mike thanks sometimes it is so easy to look over the simple way out.

Thanks
--
James


"Mike H" wrote:

James,

A macro simply isn't necessary, select the column and apply a custom format of
0000
that's four zeroes
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"James C" wrote:

I have a column in a worksheet which has a list of 3 digit numbers. I want to
change the 3 digits to become 4 digits, the new fourth digit has to be a
leading zero, i.e 355 to become 0355. I prefer the format of the digits to be
a number but a text format would not be a problem.

The length of the digits in the column are not static. i.e one month there
may be 200 rows of data the next might be 300 and I would want the macro to
cater for this variation.

Thanks
--
James

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default VBA Macro change column contents

Click on any cell in the column and run:

Sub MakeLeadingZero()
Dim ic As Long, n As Long, i As Long
Dim z As String
ic = ActiveCell.Column
n = Cells(Rows.Count, ic).End(xlUp).Row
z = "0"
For i = 1 To n
With Cells(i, ic)
v = .Value
.NumberFormat = "@"
.Value = z & v
End With
Next
End Sub

--
Gary''s Student - gsnu200909


"James C" wrote:

I have a column in a worksheet which has a list of 3 digit numbers. I want to
change the 3 digits to become 4 digits, the new fourth digit has to be a
leading zero, i.e 355 to become 0355. I prefer the format of the digits to be
a number but a text format would not be a problem.

The length of the digits in the column are not static. i.e one month there
may be 200 rows of data the next might be 300 and I would want the macro to
cater for this variation.

Thanks
--
James

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 17
Default VBA Macro change column contents

Gary thanks for highlighting this in a VBA statement.
--
James


"Gary''s Student" wrote:

Click on any cell in the column and run:

Sub MakeLeadingZero()
Dim ic As Long, n As Long, i As Long
Dim z As String
ic = ActiveCell.Column
n = Cells(Rows.Count, ic).End(xlUp).Row
z = "0"
For i = 1 To n
With Cells(i, ic)
v = .Value
.NumberFormat = "@"
.Value = z & v
End With
Next
End Sub

--
Gary''s Student - gsnu200909


"James C" wrote:

I have a column in a worksheet which has a list of 3 digit numbers. I want to
change the 3 digits to become 4 digits, the new fourth digit has to be a
leading zero, i.e 355 to become 0355. I prefer the format of the digits to be
a number but a text format would not be a problem.

The length of the digits in the column are not static. i.e one month there
may be 200 rows of data the next might be 300 and I would want the macro to
cater for this variation.

Thanks
--
James



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I change the row/column format in a macro to beyond letters? LunaMoon Excel Discussion (Misc queries) 3 August 4th 08 03:48 AM
Launch macro when drop-down contents change... Tom Mackay Excel Discussion (Misc queries) 1 September 6th 07 08:52 PM
Excel Macro to Insert the contents of one column to other Dhawal Excel Discussion (Misc queries) 5 October 5th 06 01:22 PM
macro to change multiple values in a column mrwawa Excel Discussion (Misc queries) 7 July 2nd 06 04:35 PM
Macro to remove contents of cell and move all other contents up one row adw223 Excel Discussion (Misc queries) 1 July 1st 05 03:57 PM


All times are GMT +1. The time now is 07:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"