A better way to do this kind of loop?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Code:
Range("a2:a1000").Select
    For Each r In Selection
        If r.Value >0 Then
            r.Offset(0, 3) = Now()
        End If
    Next r

I know theres probably a way without selecting, these sometimes run slow when making changes to a lot of cells
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
try

Code:
Sub MM1()
Dim r As Range
Application.ScreenUpdating = False
For Each r In Range("a2:a1000")
        If r.Value > 0 Then r.Offset(0, 3) = Now()
    Next r
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Personally I wouldn't use a loop, one other way....

Code:
Sub FilterGreaterThanOne()
    With Range("a1:a1000")
        .AutoFilter Field:=1, Criteria1:=">0", Operator:=xlAnd, Criteria2:="<>"

        On Error Resume Next
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Offset(, 3) = Now
        On Error GoTo 0
        .AutoFilter

    End With
End Sub
 
Upvote 0
try

Code:
Sub MM1()
Dim r As Range
Application.ScreenUpdating = False
For Each r In Range("a2:a1000")
        If r.Value > 0 Then r.Offset(0, 3) = Now()
    Next r
Application.ScreenUpdating = True
End Sub

thanks, modified slightly and its much quicker

Code:
Sub test1()Dim r As Range
Dim l
Dim lastrow As Long
Application.ScreenUpdating = False


lastrow = Range("A" & Rows.Count).End(xlUp).Row


For Each r In Range("a2:a" & lastrow)
        If r.Value > 0 Then r.Offset(0, 1) = Now()
    Next r
    
Application.ScreenUpdating = True


End Sub
 
Upvote 0
Personally I wouldn't use a loop, one other way....

Code:
Sub FilterGreaterThanOne()
    With Range("a1:a1000")
        .AutoFilter Field:=1, Criteria1:=">0", Operator:=xlAnd, Criteria2:="<>"

        On Error Resume Next
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Offset(, 3) = Now
        On Error GoTo 0
        .AutoFilter

    End With
End Sub

never thought about using filter, this is great and even with test data of 100,000 rows its almost instant
the loop with 100k rows freezes excel up for a few seconds

thanks
 
Last edited:
Upvote 0
You are welcome, you might get a saving by turning off screenupdating but I doubt if it will be enough to notice it.
 
Last edited:
Upvote 0
Code:
Range("a2:a1000").Select
    For Each r In Selection
        If r.Value >0 Then
            r.Offset(0, 3) = Now()
        End If
    Next r

I know theres probably a way without selecting, these sometimes run slow when making changes to a lot of cells

Another fast way :
Code:
Sub Test()
    Dim oSelection As Range

    Application.ScreenUpdating = False
    Set oSelection = Selection
    With Range("D2:D1000")
        .FormulaArray = "=IF(A2:A1000>0,NOW(),"""")"
        .Copy: .PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False: oSelection.Select
    End With
End Sub
 
Last edited:
Upvote 0
Another option
Code:
Sub AddNow()
   With Range("D2", Range("A" & Rows.Count).End(xlUp).Offset(, 3))
      .Value = Evaluate(Replace(Replace("if(@a>0,now(),@)", "@a", .Offset(, -3).Address), "@", .Address))
   End With
End Sub
 
Upvote 0
Another option
Code:
Sub AddNow()
   With Range("D2", Range("A" & Rows.Count).End(xlUp).Offset(, 3))
      .Value = Evaluate(Replace(Replace("if(@a>0,now(),@)", "@a", .Offset(, -3).Address), "@", .Address))
   End With
End Sub

Thanks Fluff this works great
Gonna need to learn how this works though to use in other circumstances, nice code
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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