Results 1 to 8 of 8

Thread: vb/excel question with ranges

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    vb/excel question with ranges

    I have a spreadsheet and was asked to write a VB Macro that will scan the spreadsheet and change the BG color of any cells that are not filled.. IE have a vakue of ""

    so i can do this easy enough i think... but i need to find the limits of the sheet.. so i figure i have to scan down and scan across to find the ends of the data.. for example the sheet looks like this
    VB Code:
    1. 212.44.42.827       done    done           done
    2. 741.171.456.747     done    done    done   done
    3. 123.14.4.487                done    done
    4. 173.14.46.681       done    done    done   done
    5. 113.142.46.888      done                   done
    6. 583.44.46.833       done    done    done
    so only where "done" is missing i want to change the BG color... any help on this??? I don't do much excel automation.. i am not so familiar with excels methods and properties
    Last edited by kleinma; Mar 12th, 2003 at 08:27 AM.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Sorted, that one didn't take too much code - not what I was expecting! btw, I'm not sure if this'll cover all cell formats, so you might wanna put another nested if statement in there checking the cell value using the isnull() function too incase that code doesn't fully cover your spreadsheet!
    Code:
    Private Sub CommandButton1_Click()
        For Each Cell In Range("A1:" & Cells.SpecialCells(xlCellTypeLastCell).Address)
            If CStr(Trim(Cell.Value)) = "" Then
                Cell.Interior.Color = &HC0FFFF
            End If
        Next Cell
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    If you use Excel2000 or later, you can do this with no code at all, using conditional formatting.
    Select the entire sheet (or the range you want to cover), and select Format --> Conditional formatting from the menu.
    Make a condition like:
    Cell value is
    equal to
    = ""

    and click the Format button to set your format.

    This way the sheet is updated as soon as a change is made. You don't have to run a macro anymore.
    Frans

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    thanks both of u

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by Frans C
    If you use Excel2000 or later, you can do this with no code at all, using conditional formatting.
    Select the entire sheet (or the range you want to cover), and select Format --> Conditional formatting from the menu.
    Make a condition like:
    Cell value is
    equal to
    = ""

    and click the Format button to set your format.

    This way the sheet is updated as soon as a change is made. You don't have to run a macro anymore.
    the only reason i don't like this approach is that it will make the entire sheet the color i pick.. when i say entire sheet i mean the cells past where the fields end..

  6. #6
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    Another property that doesn't seem to get mentioned much is
    VB Code:
    1. UsedRange

    This would also give you a way of selecting the correct cells.

    VB Code:
    1. For Each Cell In ActiveSheet.UsedRange.Select
    2.         If CStr(Trim(Cell.Value)) = "" Then
    3.             Cell.Interior.Color = &HC0FFFF
    4.         End If
    5.     Next Cell

    Its also easier on the eye.
    But each to his own.

    /Andrew

  7. #7
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86
    Hi I've used the code aboove to embed in my own app
    but
    for this" For Each Cell " It highllight "Cell"and says "Variable not defined" What to do?y?

  8. #8
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86
    Its ok... I've declared Cell as Range and it works..

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