Consulting

Results 1 to 4 of 4

Thread: Solved: Referencing a Name Range with VBA

  1. #1
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location

    Solved: Referencing a Name Range with VBA

    I have a day off and apparently so does my brain.

    I want to ClearContents if a Zero is in the Cell of a Name Range.

    This code does not work.

    What am I missing?

    OTZeros is the Range Name


    Sub DelZeros()
    Dim zRng As String

    zRng = Range("OTZeros")
    For Each Cell In zRng
    If Cell.Value = "0" Then Cell.ClearContents
    Next Cell

    End Sub


    Thanks...

    Jim

  2. #2
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    try this:

    [VBA]
    Sub DelZeros()
    Dim zRng As Range
    Set zRng = Range("OTZeros") 'I presume OTZeros is Named Range

    For Each Cell In zRng
    If Cell.Value = "0" Then Cell.ClearContents
    Next Cell

    End Sub
    [/VBA]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It you're not using the Variable range name elsewhere, then
    [VBA]
    For Each Cell In Range("OTZeros")
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location
    Thank you very much...

    Jim

Posting Permissions

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