Results 1 to 4 of 4

Thread: Excel - convert values to zero [Resolved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Resolved Excel - convert values to zero [Resolved]

    I want to select a range in a worksheet (B6:AZ1000 should cover all the ranges in any worksheet) and if there's a number in the cell, change it to zero. There are no formulas, text, etc; the cell will either be blank or have a number, but I want to keep the blank cells blank. This is in order to convert existing worksheets into templates.
    I can do this manually by copying a cell with a zero, selecting the entire worksheet or range, then use PasteSpecial Values. But how can I do this in code? And can I make this code a part of Excel rather than having to put it into every worksheet? Thanks.
    Last edited by salvelinus; May 17th, 2005 at 02:14 PM.
    Tengo mas preguntas que contestas

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Excel - convert values to zero

    You could record a macro, and after you end it, edit the code in the macro, which you could then use (after slight modification) in a vb program that would call each workbook that you want to modify.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel - convert values to zero

    Here is a macro that will set the cell value to zero if it currently contains a number (this will work for all cells on the worksheet). You can paste this into a code window for the workbook you want to store it in.
    VB Code:
    1. Sub SetValuesToZero()
    2.  
    3. Dim objCell As Range
    4.   For Each objCell In ActiveSheet.UsedRange
    5.     If objCell.Value <> "" Then
    6.       If IsNumeric(objCell.Value) Then
    7.         objCell.Value = 0
    8.       End If
    9.     End If
    10.   Next objCell
    11.  
    12. End Sub
    NB: This will ignore any cells which contain anything other than a number.


    And can I make this code a part of Excel rather than having to put it into every worksheet? Thanks.
    As long as the worksheet that it is stored in is open, you can run the macro. I doubt that you will want to run this everytime you have Excel open, so I would recommend saving it to a "common macros" workbook (which you can put other macros into later), and just load that workbook whenever you want to use it.

    It is possible to store macros in a hidden workbook which always opens with Excel, but doing this will slow down Excel slightly - so it is best to avoid it.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Excel - convert values to zero

    Thanks, I'll give that a try later today. Do a lot more work in Access than Excel.
    Tengo mas preguntas que contestas

Posting Permissions

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



Click Here to Expand Forum to Full Width