VBA - Intersect code being re-initiated

nmbc99

New Member
Joined
Apr 28, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I am creating code on a protected sheet that utilizes the intersect function.
I want the entire sheet to be locked EXCEPT Column B. Column B has drop down options for users to select and once they select one it locks that cell and also records the date & their user name in columns C & D. All other empty cells in column B will remain unlocked (I'm struggling with this as well).

Now when a specific option is selected by the user I need to copy that entire row and paste the entire copied cell directly below the original but I need to delete the contents in B, C, D so that it becomes an empty unlocked cell.
However, I've noticed my code is restarting my sub because I'm making a change to column B, and it keeps adding the row endlessly. Does anyone have any suggestions on how to fix this and/or to do this better?

Thank you for any and all help!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim WorkRng As Range, c As Range, d As Range
    Dim Rng As Range
    Dim xOffsetColumn As Integer

    If Target.Columns.Count = Me.Columns.Count Then Exit Sub

    Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target) 'sets WorkRng to be on alert for changes in column B

    xOffsetColumn = 1 'constant definition
    If Not WorkRng Is Nothing Then 'looking for a change in column B
    Target.Worksheet.Unprotect Password:="1234" 'unprotecting the sheet
        Application.EnableEvents = False 'holding off other actions
        For Each Rng In WorkRng
            Set c = Rng.Offset(0, xOffsetColumn) 'assigning range value to c
            Set d = Rng.Offset(0, xOffsetColumn + 1) 'assigning range value to d
            If Not VBA.IsEmpty(Rng.Value) Then 'checking
                c.Value = Now
                c.NumberFormat = "dd-mm-yyyy, hh:mm:ss"
                d.Value = Application.UserName

            Else
                c.ClearContents
            End If
        Next

        Application.EnableEvents = True

        Target.Locked = True

        Target.Worksheet.Protect Password:="1234", Userinterfaceonly:=True, AllowFiltering:=True
        Set Temp = Target

    End If


    If InStr(1, Temp, ",", vbTextCompare) > 0 Then ' checking to see if the string contains a comma (the options with a comma are done and can be locked)
       
        Temp.Locked = True 'lock the target cell
       
        Else
        'Entering loop where the comma doesn't exist
        Temp.Locked = False 'keeping target cell unlocked
        Application.EnableEvents = False 'holding off other actions to attempt removing loop
        'MsgBox Temp.Address
        Rows(Temp.Row & ":" & Temp.Row).Select 'selecting the row target was in
        Selection.Copy 'copy range from above
        Rows(Temp.Row + 1 & ":" & Temp.Row + 1).Select 'selecting the row right below where target was
        Selection.Insert Shift:=xlDown 'shifting rows down
        Range("B" & Temp.Row + 1 & ":" & "D" & Temp.Row + 1).Select 'selecting B#:D# within new row
        Application.EnableEvents = True 'allowing actions again
        Application.CutCopyMode = False
        Selection.ClearContents 'deleteing contents of aforementioned cells

        'Set Temp = Cells(Temp.Row + 1 & Temp.Column) ' another attempt to prevent looping

       
    End If

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
However, I've noticed my code is restarting my sub because I'm making a change to column B, and it keeps adding the row endlessly. Does anyone have any suggestions on how to fix this
Try just moving that last EnableEvents line down

1724480143019.png
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,461
Members
452,645
Latest member
Tante

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