Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Resize a Range error

Latest post Sun, Feb 14 2010 10:47 PM by Derek McGill. 4 replies.
  • Sun, Feb 14 2010 10:00 PM

    Resize a Range error

    Hi all

    Can anyone explain this code snippet and why if cell y11 is zero it gives an error ?

    Range("b11").Resize(, Range("y11").Value * 2).Interior.ColorIndex = 4

     

    Derek McGill

    • Post Points: 37
  • Sun, Feb 14 2010 10:29 PM In reply to

    • Rick Williams
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Melbourne, Australia
    • Posts 47
    • Points 826

    It looks to me that it sets the background colour of a block of cells to be lime green.  The number of cells it sets to green depends on the value in y11.  The range of cells to be coloured green starts with b11 on the left and extends to the right y11*2 cells.  

    The reason you get an error when y11 is zero (or probably blank too) is because it is attempting to select zero cells.  If this is a problem, my suggestion would be to add a quick check before the operation:

    If range("y11").value > 0 then Range("b11").Resize(, Range("y11").Value * 2).Interior.ColorIndex = 4

    So, if y11 is 0 (or blank or negative), no cell colours will be turned green.

     

    Rick Idea
    Melbourne, Australia

    Filed under: , ,
    • Post Points: 21
  • Sun, Feb 14 2010 10:31 PM In reply to

    • Jim Cone
    • Top 10 Contributor
    • Joined on Tue, Jan 15 2008
    • Portland, Oregon - USA
    • Posts 117
    • Points 1,591


    "Resize" specifies the size of a range... Resize(number of rows, number of columns)
    A range cannot have zero columns.

    You might try...
    If Range("y11").Value > 0 Then _
       Range("b11").Resize(, Range("y11").Value * 2).Interior.ColorIndex = 4
    (of course, that requires that cell y11 never have any text in it)

     

     

     

    • Post Points: 21
  • Sun, Feb 14 2010 10:41 PM In reply to

    Thanks Rick

    The worksheet is for a chess score table which shows players max/actual scores,

    and at the start they all have zero, Simple error trapping is not something I do well. thanks for your code and time

    Derek McGill

    • Post Points: 5
  • Sun, Feb 14 2010 10:47 PM In reply to

    Thanks for the info. there will not be text in the cell, code works well.

    Derek McGill

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright Excel User Group and the relevant contributors, 2009. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.