ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   VBA Macro change column contents (https://www.excelbanter.com/excel-discussion-misc-queries/254295-vba-macro-change-column-contents.html)

James C[_2_]

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

Mike H

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


Gary''s Student

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


James C[_2_]

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


James C[_2_]

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



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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com