Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Filling in ranges (WinNT, W97)

    I've just written myself a little macro to fill cells in a range automatically with the contents of the cell above, which works perfectly:

    For Each c In Range("Data")
    If c.Value <> "" And c.Offset(1, 0).Value = "" Then c.Offset(1, 0).Value = c.Value
    Next c

    My question is, how do I stop it from running outside the range? That is, I don't want it to repeat the whole last row in the next row down, just fill the range itself?

    Any help would be greatly appreciated!
    Beryl M


  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Filling in ranges (WinNT, W97)

    Hi Beryl,
    This should do it:
    <pre>For Each c In Range("Data")
    With c
    If .Value <> "" And .Offset(1, 0).Value = "" _
    And Not Intersect(.Offset(1, 0), Range("Data")) Is Nothing _
    Then .Offset(1, 0).Value = .Value
    End With
    Next c
    </pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Filling in ranges (WinNT, W97)

    Brilliant! That did it perfectly - many thanks, Rory!

    ... and now I've got another command to go off and learn about - 'Intersect' ...!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Beryl M


Posting Permissions

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