Consulting

Results 1 to 8 of 8

Thread: Solved: Automatically Add Zeros in a cell

  1. #1
    VBAX Tutor
    Joined
    Feb 2008
    Location
    New York
    Posts
    215
    Location

    Solved: Automatically Add Zeros in a cell

    Hi all,
    I copy and paste data from a website, the website offers to see the report in xls format, so I select and copy the row and paste it in my Workbook named "ABC".
    My problem is, after I paste the values from the source .xls format, I want to use a macro that will automatically add '0' in Column B; when I paste the values, the number appears as '803987' but I want it to appear as '0803987'.
    Is it also possible to add a serial number in Column A, as I keep adding the rows? Any help would be big help for me. Thanks.

    Note: I take data from this page (http://www.dec.ny.gov/cfmx/extapps/d...x.cfm?pageid=2)
    by putting a date range, e.g. 7/1/08 to 7/7/08 and then open the data as .xls file.
    -u

  2. #2
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    if what you want is a 7 digit number in column B then you can do it with formatting, select all of column b and go to format cells and select custom. then enter 0000000 as your custom type.

  3. #3
    VBAX Tutor
    Joined
    Feb 2008
    Location
    New York
    Posts
    215
    Location
    Thanks again for your help figment. Is there any way to automatically add serial no. in Column A, whenever a new row is added? I used the formula '=row(a1)' and dragged down; it automatically enters a serial number in column A, but when i use autofilter, the numbers do not appear serially, is there a neater way to do this? Thanks.
    -u

  4. #4
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Quote Originally Posted by U_Shrestha
    Thanks again for your help figment. Is there any way to automatically add serial no. in Column A, whenever a new row is added? I used the formula '=row(a1)' and dragged down; it automatically enters a serial number in column A, but when i use autofilter, the numbers do not appear serially, is there a neater way to do this? Thanks.
    autofilter is going to just do a sort more or less for whatever your filtering on. You're saying you want to have say 10 rows and add autofilter which may display row 3 5 7 and 9 and want them to be numbered 1 2 3 4 ? Correct?

  5. #5
    VBAX Tutor
    Joined
    Feb 2008
    Location
    New York
    Posts
    215
    Location
    You're saying you want to have say 10 rows and add autofilter which may display row 3 5 7 and 9 and want them to be numbered 1 2 3 4 ? Correct?
    Yes
    -u

  6. #6
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Hmm interesting. Here's how I would try doing it (but I unfortunately am swamped at work this week to actually write this). I would first record a macro of myself using autofilter as I've never needed it in a macro. I would then use that to pass whatever the vba equiavlent of the autofilter term is to a variable and do a simple if statement where for example if you were autofilter on column A on the 'Value1', it would just put your serial number into the range("b" & A.row).

    I think this wouldn't be too dificult, just a little time consuming to figure out the way vba uses autofilter.

    I'd also search for the vba code to do on command autofilter so you can put it all in the ThisWorkbook macro ;-).

    Give it a go. I will try to have a look later this week if you're still stuck.

    Good Luck with what should be an interesting little maco.

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Honestly, I don't think you even need the autofilter. Run the following routine after each time you set/unset/modify the filter:

    [vba] Dim cl As Range
    Dim lCount As Long
    With ActiveSheet
    For Each cl In .Range("A4:A" & .Range("B" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    lCount = lCount + 1
    cl.Value = lCount
    Next cl
    End With[/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    VBAX Tutor
    Joined
    Feb 2008
    Location
    New York
    Posts
    215
    Location
    Thank you very much for your code Ken Puls. That works great for me.

    Thank you all
    -u

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •