Results 1 to 2 of 2

Thread: [Excel] Office 2007 VB Script for slecting rows

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    Question [Excel] Office 2007 VB Script for slecting rows

    Hello,

    I am currently using the following code to select rows where a cell contains a specific value. For example:

    Code:
    Dim d As Range
       Dim rngD As Range
       For Each d In Intersect(ActiveSheet.UsedRange, Columns("F"))
           If d = "Canceled" Then
               If rngD Is Nothing Then Set rngD = d.EntireRow
               Set rngD = Union(rngD, d.EntireRow)
           End If
       Next d
       rngD.Select
       Selection.Delete
    The above code looks for the word canceled and deletes any rows where "canceled" is found in column F.

    However, when "canceled" is not in any cell in column F, i get a "Runtime error '91': Object variable or With block variable is not set"

    Is there a way around this? I'm a complete novice to VB and generally to most types of programing.

    Many thanks!

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Excel] Office 2007 VB Script for slecting rows

    use your range object and its built in excel function .find

    If you grab the current address and then use it, the object moves to the cell that it finds it in.

    Doesn't cancelled have two l's?

    Alternatively, with no code you should be able to flag all rows that have cancelled in a particular cell :-> =IF(ISERROR(FIND("cancelled",B2)),"N","Y")
    note that this is case sensative...

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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