Consulting

Results 1 to 5 of 5

Thread: Looping a format style macro

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location

    Looping a format style macro

    I am seeking a solution to enable a format style to be applied to a worksheet. Whilst I have experiemented with the macro recorder and shortened up the code it built, I need the code to loop down the sheet as long as there is data in Column A
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub FormatCells()
    Dim LastRow As Long
    Dim i As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    If LastRow Mod 2 = 0 Then LastRow = LastRow + 1

    For i = 2 To LastRow Step 2

    With .Cells(i, "A").Resize(, 3).Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent3
    .TintAndShade = 0.799981688894314
    .PatternTintAndShade = 0
    End With

    With .Cells(i, "D").Resize(2, 4)

    .BorderAround LineStyle:=xlContinuous

    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlNone
    End With
    Next i
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Thank you Bob. Once the style is applied to the sheet, can I simply change the sub title from Sub FormatCells() to Sub Worksheet_Change(ByVal Target As Range) so that it comes into effect as soon as I insert a new pair of rows?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I don't think it would be a straight lift Ted.

    You would need to determine what triggers the styling, and what to do if the value is removed, etc.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location

    Conditional formatting?

    Aussiebear,

    Did you try conditional formating? See the attached copy of your workbook; I added conditional formatting to it. I think it will work for you, and it resolves the issue of what to do when new line pairs are added, inserted or a line pair deleted or cleared.

    Thanks,
    Ron
    Windermere, FL

Posting Permissions

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