VBA - simplify code

oggy3000

Board Regular
Joined
Jul 13, 2011
Messages
51
Hey,
I hope someone here can help me. I recorded the the following code with the macro recorder and it works so far. Problem is, it takes about 15 seconds until it is through. Is there a way to speed things up because I am using it for a second sheet with the same layout aswell.

It is supposed to allignt certain columns left/right and apply conditioned formatting.

Cheers
Jan
Code:
Sub Button505_Click()
    Sheets("Outbound").Select
    Range("E:E,G:H").Select
    Range("G7558").Activate
    With Selection
        .HorizontalAlignment = xlCenter
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("F:F").Select
    Range("F7558").Activate
    With Selection
        .HorizontalAlignment = xlLeft
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
        , Formula1:="="""""
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=TODAY()"
    Selection.FormatConditions(2).Interior.ColorIndex = 6
     Cells(1, 1).Select
    Range("A1").Activate
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Ok I took out some lines and now its only 3-4 seconds, which is acceptable. Is there a way of doing it more elegantly?

Cheers
Jan

Code:
Sub Button505_Click()
Sheets("Outbound").Select
    Range("E:E,G:H").Select
    Range("G7558").Activate
    With Selection
        .HorizontalAlignment = xlCenter
    End With
    Columns("F:F").Select
    Range("F7558").Activate
    With Selection
        .HorizontalAlignment = xlLeft
    End With
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
        , Formula1:="="""""
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=TODAY()"
    Selection.FormatConditions(2).Interior.ColorIndex = 6
     Cells(1, 1).Select
    Range("A1").Activate
End Sub
 
Upvote 0
Code:
Sub Button505_Click()
Sheets("Outbound").Select
Range("E:E,G:H").HorizontalAlignment = xlCenter
With Columns("F:F")
    .HorizontalAlignment = xlLeft
    With .FormatConditions
        .Delete
        .Add Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="="""""
        .Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=TODAY()"
    End With
    .FormatConditions(2).Interior.ColorIndex = 6
End With
Cells(1, 1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,391
Members
452,909
Latest member
VickiS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top