Error in Workbook_SheetChange - infinite loop without reason

wns10680

New Member
Joined
Sep 13, 2021
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
I am new to Excel VBA, please anyone can help me check with the problem?
I am currently like to update the user name, which I stored in ThisWorkbook.Sheets("User").Range("M1"), once the sheet changes.
However, the code keep loop without ending from the red line below. Can anyone help explain what happen with my code?



Rich (BB code):
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim lRow As Long, lCol As Long, i As Integer, j As Integer
Dim name As String

On Error GoTo 0

If (Sh.name = "Summary" Or Sh.name = "Sample" Or Sh.name = "User") = False Then 'loop start from here

    lRow = Sh.Cells(Rows.Count, 1).End(xlUp).Row
    lCol = Sh.Cells(1, Columns.Count).End(xlToLeft).Column
    If Target <> "" Then

        Do While ThisWorkbook.Sheets("User").Range("M1") = ""
            MsgBox "Please enter your name."
            LoadUserForm
        Loop

        name = ThisWorkbook.Sheets("User").Range("M1")

        If name <> "" Then
    
            Sh.Range("Q"& Target.Row).Value =name  'the code after proceed this line keep loop back to the first If statement
            MsgBox name
       
        End If

    End If

End If

End Sub
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
your code will loop continuously becuase this line:
VBA Code:
Sh.Range("Q"& Target.Row).Value =name 'the code after proceed this line keep loop back to the first If statement
makes a change on the worksheet and thus triggers the workhseet change event again and again and ....
You need to inhibit the events handling before writing and restore it afterwards like this:
VBA Code:
application.enableEvents=False
Sh.Range("Q"& Target.Row).Value =name 'the code after proceed this line keep loop back to the first If statement
application.enableEvents=True
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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