Macro works, but very slow

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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
 
Upvote 0
Solution
Spot on, that is exactly what it was. I could barely blink and it was done. Cheers!
 
Upvote 0
Excellent! Glad to hear it worked well for you.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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