VBA worksheet change event

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,522
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends after a long time,

I need a small VBA code
When I enter "humayun" in cell A1 then A1 value should change to "humayun rayani" automatically

Regards,

Humayun
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If LCase(Range("A1").Value) = "humayun" Then Range("A1").Value = "humayun rayani"
End Sub
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub
    
    If Target.Address = Range("A1").Address Then
        If Target.Value = "humayun" Then
            Application.EnableEvents = False
            Target.Value = "humayun rayani"
            Application.EnableEvents = True
        End If
    End If
        
End Sub
 
Upvote 0
My Bad, for not putting up my question correctly

change A1 value to A1 value and "rayani"

if I enter Abdul it should be changed to "abdul rayani" as soon as i press enter
if i enter Humayun it should be chnaged to "humayun rayani" as soon as i press enter
 
Upvote 0
My Bad, for not putting up my question correctly

change A1 value to A1 value and "rayani"

if I enter Abdul it should be changed to "abdul rayani" as soon as i press enter
if i enter Humayun it should be chnaged to "humayun rayani" as soon as i press enter
Simple change:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub
    
    If Target.Address = Range("A1").Address Then
        Application.EnableEvents = False
        Target.Value = Target.Value & " rayani"
        Application.EnableEvents = True
    End If
        
End Sub
 
Upvote 0
Solution
Simple change:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub
   
    If Target.Address = Range("A1").Address Then
        Application.EnableEvents = False
        Target.Value = Target.Value & " rayani"
        Application.EnableEvents = True
    End If
       
End Sub
Thanks Joe
Works like a charm
 
Upvote 0
You are welcome.
hello, one more thing
what is wrong when i write the code like below
why it is not working

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A1")) Is Nothing Then

    Application.EnableEvents = False
    
    Range("A1").Value = Range("A1").Value & " Humyaun"
    
    Application.EnableEvents = True

        End If
End Sub
 
Upvote 0
hello, one more thing
what is wrong when i write the code like below
why it is not working

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A1")) Is Nothing Then

    Application.EnableEvents = False
   
    Range("A1").Value = Range("A1").Value & " Humyaun"
   
    Application.EnableEvents = True

        End If
End Sub
Nothing wrong with that code, it should work as long as:
- You have placed it in the proper sheet module
- Events are enabled

When you have code that enables/disables events, sometimes if you are testing you may accidentally disable it and not re-enable it. In that case, all events will stop working.
You can re-enable them by manually running this code:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub
 
Upvote 0
Nothing wrong with that code, it should work as long as:
- You have placed it in the proper sheet module
- Events are enabled

When you have code that enables/disables events, sometimes if you are testing you may accidentally disable it and not re-enable it. In that case, all events will stop working.
You can re-enable them by manually running this code:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub
ok but when i press delete then the code stops working next time
so every time I have to run the enable events then worksheet change would work ?? is that so ??
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,777
Members
452,942
Latest member
VijayNewtoExcel

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