Let's Toggle Cell Properties - Quickly

Posted Fri, Jan 14 2011 20:25 by Nate Oliver

Good evening,

We'll keep tonight's entry short. If you want to blast through a multi-cell Range of cells worth of data, I have a few thoughts. Use a buffered array, limit your property toggles to that which is necessary, e.g., don't flicker the Protect Property of the Worksheet on each pass in the loop.

With that, here's the example:

Sub foo()
Dim myArr() As Variant
Dim i As Long, j As Long
Let Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=vbNullString
Let myArr = Range("A7:C2367").Value
    For i = LBound(myArr, 1) To UBound(myArr, 1)
        For j = LBound(myArr, 2) To UBound(myArr, 2)
            If LenB(myArr(i, j)) > 0 Then
                With Cells(i + 6, j)
                    .Locked = True
                    .FormulaHidden = False
                End With
            End If
        Next
    Next
ActiveSheet.Protect Password:=vbNullString
Application.ScreenUpdating = True
End Sub

This should have some pretty fast shoes on her. It's a 1-based Array, if you're wondering why I'm adding 6. With that, happy MLK Weekend!

Best,
Nate Oliver

Comments

# re: Let's Toggle Cell Properties - Quickly

Saturday, January 15, 2011 8:50 AM by dkusleika

What's the advantage over, say, a For Each loop?

# re: Let's Toggle Cell Properties - Quickly

Saturday, January 15, 2011 4:04 PM by Nate Oliver

Hi ***, in my experience iterating through an array is quite a bit faster, percentage-wise, than iterating through a multi-cell Range. Range's are fairly heavy objects, and I suspect that testing each cell's value property adds quite a bit of overhead.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: