+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    02-09-2010
    Location
    brussels
    MS-Off Ver
    Excel 2003
    Posts
    63

    Make code shorter

    Hello there,

    Can somebody help me out with shrotening following code please? Can't find a solution.

    Code:
    Sub lineoutcoloring()
    
        If Sheets("Shiftrapport").Range("E6").Value = "" Then
        Range("A6:X10").FormatConditions.Delete
        Range("A6:X10").Interior.Color = RGB(0, 0, 0)
        Range("A6:X10").Font.Color = RGB(0, 0, 0)
        Range("A6:X10").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E11").Value = "" Then
        Range("A11:X15").FormatConditions.Delete
        Range("A11:X15").Interior.Color = RGB(0, 0, 0)
        Range("A11:X15").Font.Color = RGB(0, 0, 0)
        Range("A11:X15").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E16").Value = "" Then
        Range("A16:X20").FormatConditions.Delete
        Range("A16:X20").Interior.Color = RGB(0, 0, 0)
        Range("A16:X20").Font.Color = RGB(0, 0, 0)
        Range("A16:X20").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E21").Value = "" Then
        Range("A21:X25").FormatConditions.Delete
        Range("A21:X25").Interior.Color = RGB(0, 0, 0)
        Range("A21:X25").Font.Color = RGB(0, 0, 0)
        Range("A21:X25").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E26").Value = "" Then
        Range("A26:X30").FormatConditions.Delete
        Range("A26:X30").Interior.Color = RGB(0, 0, 0)
        Range("A26:X30").Font.Color = RGB(0, 0, 0)
        Range("A26:X30").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E31").Value = "" Then
        Range("A31:X35").FormatConditions.Delete
        Range("A31:X35").Interior.Color = RGB(0, 0, 0)
        Range("A31:X35").Font.Color = RGB(0, 0, 0)
        Range("A31:X35").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E36").Value = "" Then
        Range("A36:X40").FormatConditions.Delete
        Range("A36:X40").Interior.Color = RGB(0, 0, 0)
        Range("A36:X40").Font.Color = RGB(0, 0, 0)
        Range("A36:X40").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E41").Value = "" Then
        Range("A41:X45").FormatConditions.Delete
        Range("A41:X45").Interior.Color = RGB(0, 0, 0)
        Range("A41:X45").Font.Color = RGB(0, 0, 0)
        Range("A41:X45").Borders.ColorIndex = xlAutomatic
        End If
        If Sheets("Shiftrapport").Range("E46").Value = "" Then
        Range("A46:X50").FormatConditions.Delete
        Range("A46:X50").Interior.Color = RGB(0, 0, 0)
        Range("A46:X50").Font.Color = RGB(0, 0, 0)
        Range("A46:X50").Borders.ColorIndex = xlAutomatic
        End If
        
    End Sub
    Many thanks!

    Aquila
    Last edited by Aquila; 03-22-2010 at 08:09 PM.

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,027

    Re: Make code shorter

    Hi

    How about

    Code:
    For i = 6 To 46 Step 5
        If Cells(i, "E").Value = "" Then
          Set rng = Cells(i, "A").Resize(5, 24)
          rng.FormatConditions.Delete
          rng.Interior.Color = RGB(0, 0, 0)
          rng.Font.Color = RGB(0, 0, 0)
          rng.Borders.ColorIndex = xlAutomatic
        End If
      Next i
    rylo

  3. #3
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    843

    Re: Make code shorter

    Try this.

    Code:
    Sub lineoutcoloring()
    Dim i As Integer, rng As Range
    For i = 6 To 46 Step 5
    
    If Sheets("Shiftrapport").Range("E" & i).Value = "" Then
    Set rng = Range(Sheets("Shiftrapport").Range("E" & i).Offset(0, -4), _
            Sheets("Shiftrapport").Range("E" & i).Offset(4, 19))
    rng.Select
    With rng
        .FormatConditions.Delete
        .Interior.Color = RGB(0, 0, 0)
        .Font.Color = RGB(0, 0, 0)
        .Borders.ColorIndex = xlAutomatic
    End With
    Set rng = Nothing
    End If
    Next
    End Sub

  4. #4
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,042

    Re: Make code shorter

    maybe like this:

    Code:
    Sub lineoutcoloring()
    Dim cCell As Range, i As Integer
    For i = 6 To 46 Step 5
        Set cCell = Sheets("Shiftrapport").Range("E" & i)
        If cCell.Value = "" Then
            With Range("A" & i & ":X" & i + 4)
                .FormatConditions.Delete
                .Interior.Color = RGB(0, 0, 0)
                .Font.Color = RGB(0, 0, 0)
                .Borders.ColorIndex = xlAutomatic
            End With
        End If
    Next i
    End Sub
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  5. #5
    Registered User
    Join Date
    02-09-2010
    Location
    brussels
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Make code shorter

    Whow...

    Thank you guys for quick response!!
    rylo and teylyn your code works fine.
    mdbct you code works but the range is not ok.

    Anyway many thanks for the small code!
    Problem solved

    grts
    Aquila

  6. #6
    Registered User
    Join Date
    02-09-2010
    Location
    brussels
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Make code shorter

    Hello,

    one more question about this,

    I drawed line's in the ranges and also want them to change color.
    How can i reach them? i've tried many ways but always i get an error massage.
    .drawing.color
    .lines.color
    ...?

    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0