Why do I get an error for having the username code under the Private Sub ()

liabilityquek

New Member
Joined
Oct 17, 2021
Messages
25
Office Version
  1. 2019
Platform
  1. Windows
Hi

I would like to insert a username code under the existing Private Sub (). But whenever the code is triggered, a debug error surfaces and the workbook closes. Is there a way to fix this issue?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim TriggerCell As Range

Set TriggerCell = Range("C7")
   
    If Not Application.Intersect(TriggerCell, Target) Is Nothing Then
            If TriggerCell.Value = "Yes" Then
                Rows("14:16").Hidden = False
                Rows("17:25").Hidden = True
                             
                ElseIf TriggerCell.Value = "Credit Re-assessment" Then
                Rows("14:25").Hidden = False
                             
                ElseIf TriggerCell.Value = "No" Then
                Rows("14:25").Hidden = False
                              
                ElseIf TriggerCell.Value = "No/Credit Re-assessment - Sole Proprietorship (Owned by Company)" Then
                Rows("14:25").Hidden = False
                
                ElseIf TriggerCell.Value = "Please Select" Then
                Rows("14:25").Hidden = False
                
            End If
    End If

Dim User_Name As String

User_Name = Application.UserName
Range("C4") = User_Name
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You're not pausing events while the code runs so it creates an infinite loop. Note that I've added 2 lines, 1 at the top, 1 at the bottom. When your code performs an action that could cause it to run itself these lines are necessary to prevent that from happening.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False

Dim TriggerCell As Range

Set TriggerCell = Range("C7")
  
    If Not Application.Intersect(TriggerCell, Target) Is Nothing Then
            If TriggerCell.Value = "Yes" Then
                Rows("14:16").Hidden = False
                Rows("17:25").Hidden = True
                            
                ElseIf TriggerCell.Value = "Credit Re-assessment" Then
                Rows("14:25").Hidden = False
                            
                ElseIf TriggerCell.Value = "No" Then
                Rows("14:25").Hidden = False
                              
                ElseIf TriggerCell.Value = "No/Credit Re-assessment - Sole Proprietorship (Owned by Company)" Then
                Rows("14:25").Hidden = False
                
                ElseIf TriggerCell.Value = "Please Select" Then
                Rows("14:25").Hidden = False
                
            End If
    End If

Dim User_Name As String

User_Name = Application.UserName
Range("C4") = User_Name
Application.EnableEvents = True
End Sub
 
Upvote 0
You're not pausing events while the code runs so it creates an infinite loop. Note that I've added 2 lines, 1 at the top, 1 at the bottom. When your code performs an action that could cause it to run itself these lines are necessary to prevent that from happening.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False

Dim TriggerCell As Range

Set TriggerCell = Range("C7")
 
    If Not Application.Intersect(TriggerCell, Target) Is Nothing Then
            If TriggerCell.Value = "Yes" Then
                Rows("14:16").Hidden = False
                Rows("17:25").Hidden = True
                           
                ElseIf TriggerCell.Value = "Credit Re-assessment" Then
                Rows("14:25").Hidden = False
                           
                ElseIf TriggerCell.Value = "No" Then
                Rows("14:25").Hidden = False
                             
                ElseIf TriggerCell.Value = "No/Credit Re-assessment - Sole Proprietorship (Owned by Company)" Then
                Rows("14:25").Hidden = False
               
                ElseIf TriggerCell.Value = "Please Select" Then
                Rows("14:25").Hidden = False
               
            End If
    End If

Dim User_Name As String

User_Name = Application.UserName
Range("C4") = User_Name
Application.EnableEvents = True
End Sub
Thanks for the assist Jasonb!
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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