0

Main Module

My code is working fine, the idea is to lock the entire row in the target workbook if source workbook cells in column b have a yellow color....

The question is how to replace the for-each loop with arrays for performance-wise, that's all?

  Sub LockYellow()
    
    
    
    With ThisWorkbook.Worksheets("Data")
    .Unprotect "007"
    .UsedRange.Locked = False
    .Range("a1:z1").Locked = True
    End With
    
    ' ---## Global
        Dim SourceBook As Workbook
        Set SourceBook = GetWorkbook(Source)
    
        
        
        Dim X, xrange As Range
        Set xrange = SourceBook.Worksheets("Data").Range("b2:b1499")
        
        For Each X In xrange
        
        'If X.Interior.Color = 65535 Then
      If X.value = "User_1" Then
        Dim u As String
        u = X.Address
        'ThisWorkbook.Worksheets("Data").Range(u).Value2 = SourceBook.Worksheets("Data").Range(u).Value2
        'ThisWorkbook.Worksheets("Data").Range(u).Interior.Color = 65535
        ThisWorkbook.Worksheets("Data").Range(u).EntireRow.Locked = True
        End If
        Next X


       If Not SourceBook Is Nothing Then
           SourceBook.Close savechanges:=False
    End If
    
    ThisWorkbook.Worksheets("Data").Protect "007"
    
    
    End Sub

Helper Module

  Public Const Source As String = "C:\Users\vv\Desktop\Sourcesheet.xlsx"

    Public Function GetWorkbook(ByVal sFullName As String) As Workbook
    
        Dim sFile As String
        Dim wbReturn As Workbook
    
        sFile = Dir(sFullName)
    
        On Error Resume Next
            Set wbReturn = Workbooks(sFile)
    
            If wbReturn Is Nothing Then
                Set wbReturn = Workbooks.Open(sFullName)
            End If
        On Error GoTo 0
    
        Set GetWorkbook = wbReturn
    
    End Function
8
  • 2
    I don't think an array is going to help you here. Commented Dec 4, 2020 at 17:23
  • @Bigben is it a dead-end? no other alternatives? Commented Dec 4, 2020 at 17:25
  • Maybe Union but I haven't tested it. Commented Dec 4, 2020 at 17:25
  • What if I changed the logic from color to value, meaning that instead of checking the yellow color in the source sheet it will check a value i.e. "User_1". \then get the address, then lock the same entirerow address in target sheet. Commented Dec 4, 2020 at 17:27
  • That might be slightly faster. Commented Dec 4, 2020 at 17:27

1 Answer 1

2

Try this out:

Dim ws as Worksheet
Set ws = ThisWorkbook.Worksheets("Data")

Dim test as Range
Set test = SourceBook.Worksheets("Data").Range("b2:b1499")

Dim v As Variant
v = test.Value
    
Dim x as Long, rowLock as Range
For x = lbound(v) to ubound(v)
    
    If v(x,1) = "User_1"
        If rowLock is Nothing Then
           Set rowLock = ws.cells(x+1,test.column) 'x + 1 because range starts at row 2
        Else 
           Set rowLock = Union(rowLock,ws.cells(x+1,test.column))
        End If
    End If
Next

If not rowLock is Nothing Then rowLock.EntireRow.Locked = True
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.