Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Need help to write macro please

I am trying to write a macro that will format column B for text and then
insert four zero's (0000) infront of an existing number in the cell starting
at line 2 and continuing for 100 lines into that column of a spreadsheet.
The cells in the column are already populated with 8 digit numbers.
I'm a beginner at this and have been able to insert the zero's but it takes
the number from the cell that I did the macro on and repopulates this number
into other sheets. ( Am I making any sense?)
Would anyone have any ideas of guidance?
Many thanks
Bob



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need help to write macro please

Bob


Try these for starts

Both macros replace the existing number in column B with one that ha
0000 at the start


Macro1 runs over b2 to b100

Macro2 runs over b2 to last used cell in column B


Sub Macro1()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b100")
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub



Sub Macro2()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b" _
& Range("b" & Rows.Count).End(xlUp).Row)
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Su

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Need help to write macro please

Mudraker
Thanks for the reply.
Tried copying and pasting into (replacing) existing macro and keep getting
problems
This is exactly what I've pasted in

Sub zeros()
'
' zeros Macro
' Macro recorded 5/26/2004 by Bob
'
' Keyboard Shortcut: Ctrl+b
'
Sub Macro2()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b" _
& Range("b" & Rows.Count).End(xlUp).Row)
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub

It keeps coming back "compile error - expected end sub"
Any suggestions -
Thanks very much


"mudraker " wrote in message
...
Bob


Try these for starts

Both macros replace the existing number in column B with one that has
0000 at the start


Macro1 runs over b2 to b100

Macro2 runs over b2 to last used cell in column B


Sub Macro1()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b100")
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub



Sub Macro2()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b" _
& Range("b" & Rows.Count).End(xlUp).Row)
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default Need help to write macro please

Your do not have matching Sub-End Subs. You have one too many Subs. Remove
the line "Sub Macro2()".

"Bob" wrote in message
...
Mudraker
Thanks for the reply.
Tried copying and pasting into (replacing) existing macro and keep getting
problems
This is exactly what I've pasted in

Sub zeros()
'
' zeros Macro
' Macro recorded 5/26/2004 by Bob
'
' Keyboard Shortcut: Ctrl+b
'
Sub Macro2()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b" _
& Range("b" & Rows.Count).End(xlUp).Row)
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub

It keeps coming back "compile error - expected end sub"
Any suggestions -
Thanks very much


"mudraker " wrote in message
...
Bob


Try these for starts

Both macros replace the existing number in column B with one that has
0000 at the start


Macro1 runs over b2 to b100

Macro2 runs over b2 to last used cell in column B


Sub Macro1()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b100")
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub



Sub Macro2()
Dim Rng As Range
Columns("B:B").NumberFormat = "@"
For Each Rng In Range("b2:b" _
& Range("b" & Rows.Count).End(xlUp).Row)
If Rng.Value < "" Then
Rng.Value = "0000" & Rng.Value
End If
Next Rng
End Sub


---
Message posted from http://www.ExcelForum.com/





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default Need help to write macro please

Bob,

Workbooks("Book1.xls").Worksheets("Sheet1").Range( "B2:B102").Value = _
"'0000" & Workbooks("Book1.xls").Worksheets("Sheet1").Range( "B2:B102").Text

If you are satisfied always to act on the worksheet, you can use.

Range("B2:B102").Value = "'0000" & Range("B2:B102").Text

Bob

"Bob" wrote in message
. ..
I am trying to write a macro that will format column B for text and then
insert four zero's (0000) infront of an existing number in the cell

starting
at line 2 and continuing for 100 lines into that column of a spreadsheet.
The cells in the column are already populated with 8 digit numbers.
I'm a beginner at this and have been able to insert the zero's but it

takes
the number from the cell that I did the macro on and repopulates this

number
into other sheets. ( Am I making any sense?)
Would anyone have any ideas of guidance?
Many thanks
Bob







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default Need help to write macro please

"Bob Kilmer" wrote in message
...
Bob,

Workbooks("Book1.xls").Worksheets("Sheet1").Range( "B2:B102").Value = _
"'0000" &

Workbooks("Book1.xls").Worksheets("Sheet1").Range( "B2:B102").Text

If you are satisfied always to act on the worksheet, you can use.



should be "ACTIVE worksheet"


Range("B2:B102").Value = "'0000" & Range("B2:B102").Text

Bob



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Need help to write macro please

Mudraker - I figured it out and it works GREAT! Thank you
Thank you very much to all.
Very much appreciated.
Best Rgds
Bob

"Bob" wrote in message
. ..
I am trying to write a macro that will format column B for text and then
insert four zero's (0000) infront of an existing number in the cell

starting
at line 2 and continuing for 100 lines into that column of a spreadsheet.
The cells in the column are already populated with 8 digit numbers.
I'm a beginner at this and have been able to insert the zero's but it

takes
the number from the cell that I did the macro on and repopulates this

number
into other sheets. ( Am I making any sense?)
Would anyone have any ideas of guidance?
Many thanks
Bob





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 to write a macro JStiehl Excel Discussion (Misc queries) 4 August 11th 08 10:08 PM
Help write Macro nc Excel Discussion (Misc queries) 5 November 17th 05 03:19 PM
How do i write a Macro that does following Gautam New Users to Excel 5 June 30th 05 08:24 AM
is it possible to execute write to the fields in another .xsl form a macro in another .xsl? e.g. some way to load another .xsl into an .xsl macro and write to its data? Daniel Excel Worksheet Functions 1 June 23rd 05 11:38 PM
How do I write a macro for... Christopher Anderson Excel Discussion (Misc queries) 1 December 20th 04 05:18 PM


All times are GMT +1. The time now is 09:11 AM.

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"