Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2013, 02:18 PM
cacouncill94 cacouncill94 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 cacouncill94 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 14 m 37 sec
Reputation Power: 0
Add Zeroes Macro Stopped Working

In my job I deal with employee Id's alot and we have variations of them and sometimes I need to convert them to the other form which is as simple as adding enough zeroes to the beginning of the number until it is 7 digits long. It is not simple when I have hundered or thousands of IDs to convert so i made a macro which goes through the list of IDs and adds the correct number of zeroes until the id has 7 total digits. And it has worked every time I used it up until now and I cannot figure out what is wrong with it. Here is the code, the error is in the "For i = 1 To endrow -1" Like I said, this code has worked every time I have used it up until now. And if it does work it only adds the zeroes to the first cell, but it stops after that which it is not supposed to.

The way it works is, the numbers must be in the A column of the spreadsheet and it checks to see how long the number is then adds the approrpriate number of zeroes if it needs them, then loops to the next. it continues to loop until it reaches a blank cell.

Code:
Sub AddZeroes() 
     'Declarations
    Dim i As Integer, j As Integer, endrow As Long 
     'Converts the A column format to Text format
    Application.ScreenUpdating = False 
    Columns("A:A").Select 
    Selection.NumberFormat = "@" 
     'finds the bottom most row
    endrow = ActiveSheet.Range("A1").End(xlDown).Row 
     'selects the top cell in column A
    ActiveSheet.Range("A1").Select 
     'loop to move from cell to cell
    For i = 1 To endrow - 1 
         'Moves the cell down 1. Assumes there's a header row so really starts at row 2
        ActiveCell.Offset(1, 0).Select 
         'The Do-While loop keeps adding zeroes to the front of the cell value until it hits a length of 7
        Do While Len(ActiveCell.Value) < 7 
            ActiveCell.Value = "0" & ActiveCell.Value 
        Loop 
    Next i 
    Application.ScreenUpdating = True 
End Sub 

Reply With Quote
  #2  
Old February 5th, 2013, 02:50 PM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,938 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 26 m 49 sec
Reputation Power: 8490
Facebook
Why not just do something like ...

Code:
Activesheet.Columns("A:A").Numberformat="0000000"


?

But with your solution you might want to change

Code:
endrow = ActiveSheet.Range("A1").End(xlDown).Row 


to

Code:
endrow = Activesheet.cells.specialcells(xlcelltypelastcell).row
__________________
medialint.com

“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

Last edited by medialint : February 5th, 2013 at 02:54 PM.

Reply With Quote
  #3  
Old February 9th, 2013, 02:42 AM
GGriggs GGriggs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 18 GGriggs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 45 m 43 sec
Reputation Power: 0
use excel

easiest way is in excel if you are using VBA

put your number in a cell say A1 and then put

= text(a1,"0000000") in another cell

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Add Zeroes Macro Stopped Working

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap