• Color a cell (2002/SP3)

    Author
    Topic
    #455010

    Hi,

    I have a range of number from A1 to F10. I set this range in VBA as:

    Dim r As Range
    Set r = Selection.CurrentRegion

    I want to format the range as follows:
    1) A1 to bold and color 55
    2) B1 to F1 to bold and color 50

    In my code I have:

    r.Cells(1, 1).Font.ColorIndex = 55
    for C = 1 to 5
    r.Cells(1, C).Font.ColorIndex = 50
    next C

    When I run this code nothing happens. Can someone help me here?

    Viewing 2 reply threads
    Author
    Replies
    • #1131029

      Try

      With Range("A1").Font
      .Bold = True
      .ColorIndex = 55
      End With
      With Range("B1:F1").Font
      .Bold = True
      .ColorIndex = 50
      End With

      • #1131036

        I’m looking for a more general form. The statement “Range(“A1″).Font…” works well for a VBA with a constant range. In my case though the range can change from A1:F10 to another range. That’s why I chose to use “CurrentRegion” for the selection. I was reading that using the command “Selection.CurrentRange.Columns(2).Font.Colorindex” works for column 2 in the current range. Is there a general for for picking a single or multiple cells in the selection range and changing the font?

        Any thoughts?

    • #1131038

      (Edited by wdwells on 01-Nov-08 09:19. Forgot to declare “z”, and got my “x” and “y” variables confused, The fallacy of using Air Code.)

      If you want to make the code relative to the selected range, try:

      Sub FormatFont()
      Dim c As Long
      Dim x As Long
      Dim y As Long
      Dim z As Long

      x = Selection.Column
      y = Selection.Row
      z = Selection.Columns.Count

      With Cells(y, x).Font
      .ColorIndex = 55
      .Bold = True
      End With
      For c = 1 + x To x + z - 1
      With Cells(y, c).Font
      .ColorIndex = 50
      .Bold = True
      End With
      Next c
      End Sub

    • #1131155

      Your code works fine for me as does this version:

      Dim r As Range
      Set r = Selection.CurrentRegion
      r.Cells(1, 1).Font.ColorIndex = 55
      r.Cells(1, 2).Resize(, 5).Font.ColorIndex = 50
      

      In what way does yours not work?

      • #1131159

        Thanks Don and Rory. Great help. The formatting code works well now.

        What does “Resize(, 5)” do?

        • #1131161

          Resize(rowcount, columncount) takes a range and resizes it (starting from the upper left corner) to have the specified number of rows and columns; if either of the dimensions is omitted, it is left unchanged.

          r.Cells(1, 2) is second cell in the top row of the range r.
          .Resize(, 5) leaves the row count unchanged (at 1) and increases the column count to 5 (from 1)
          So r.Cells(1, 2).Resize(, 5) is a range one row high and 5 columns wide starting ar the second cell in the top row of range r.

      • #1131162

        Hi Rory
        Thanks for the Resize command. That’s new to me.

        Irrespective of the selected range, your code formats A1:F1 on my 2003 system. Any thoughts on why?

        • #1131163

          Works fine for me in 2003. I’d have to guess that the CurrentRegion is extending up to A1 for some reason.

          • #1131179

            [indent]


            I’d have to guess that the CurrentRegion is extending up to A1 for some reason


            [/indent] Yes Rory; that was indeed the case. I was unclear of the difference between Selection and region. Hans has sorted this out for me below.

        • #1131164

          The range r is set to Selection.CurrentRegion. Range.CurrentRegion is the largest rectangular area without empty rows or columns that contains Range. So if the current region of the selection contains A1, the code colors cells A1 and B1:F1.

    Viewing 2 reply threads
    Reply To: Color a cell (2002/SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: