Macro works, but very slow

Livin404

Active Member
Joined
Jan 7, 2019
Messages
361
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, the Macro works as designed. it is looking in Column A for the word "DATE" and left aligning only those cells containing "DATE". However, it is very slow and I get worried while I'm waiting that it is going to error out on me. I hope it is something really simple. This alignment suppose to happen on "Outbound FIDS", and then I want to make sure I return to "72 Hr" as the active sheet which is why I included it. Thank you,

VBA Code:
Sub LeftAlign_Date()
    Dim cel As Range
Dim WS As Worksheet

For Each WS In Worksheets
    For Each cel In WS.Range("A:A")
        If InStr(1, cel.Value, "DATE") > 0 Then cel.HorizontalAlignment = xlLeft
    Next
Next
Sheets("72 Hr").Select
End Sub
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,914
Office Version
  1. 365
Platform
  1. Windows
Two things we can do to help speed things up:
1. Delay all screen updating until the loops are finished
2. Only go down as far as the last populated cell in column A instead of hitting every possible cell in column A

See if this is any faster:
VBA Code:
Sub LeftAlign_Date()
    
    Dim cel As Range
    Dim WS As Worksheet

    Application.ScreenUpdating = False

    For Each WS In Worksheets
        For Each cel In WS.Range("A1:A" & WS.Cells(Rows.Count, "A").End(xlUp).Row)
            If InStr(1, cel.Value, "DATE") > 0 Then cel.HorizontalAlignment = xlLeft
        Next
    Next

    Application.ScreenUpdating = True

    Sheets("72 Hr").Select
    
End Sub
 
Solution

Livin404

Active Member
Joined
Jan 7, 2019
Messages
361
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Spot on, that is exactly what it was. I could barely blink and it was done. Cheers!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,914
Office Version
  1. 365
Platform
  1. Windows
Excellent! Glad to hear it worked well for you.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
This alignment suppose to happen on "Outbound FIDS"
Sounds like you have a fast enough solution, but if the above is true then I don't see why you are cycling through all the worksheets in the workbook?
Depending on how many worksheets you have in the workbook and how many rows of data in column A of those sheets, it could add considerable (unnecessary) time to the macro.
Assuming that it is to operate on that one sheet only and "DATE" does not occur in cell A1, where I'm assuming a header exists, you could also try this.

VBA Code:
Sub LeftAlignDate()
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  With Sheets("Outbound FIDS")
    With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
      .AutoFilter Field:=1, Criteria1:="*DATE*"
      .Offset(1).SpecialCells(xlVisible).HorizontalAlignment = xlLeft
    End With
    .AutoFilterMode = False
  End With
  Application.Calculation = xlCalculationAutomatic
  Sheets("72 Hr").Activate
  Application.ScreenUpdating = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,364
Messages
5,595,722
Members
414,013
Latest member
tnobbs

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
Top