VBA code

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
Hi,
I have the code below which one has to run it manually. I need that if cell B9 is populated, it runs automatically.


Sub insertusername()
Range("A1").Value = Application.UserName
End Sub


Thanks in advance
 
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("B9")) Is Nothing Then
   If Target.Value = "" Then
      Range("A1").ClearContents
   Else
      Range("A1").Value = Application.UserName
   End If
End If
Application.EnableEvents = True
End Sub
 
Upvote 0

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.
You're welcome & thanks for the feedback
 
Upvote 0
Yes they are correct.

I highly doubt this is the case. Fluff's code is equal to mine other than that he has removed the Workbook and Worksheet references.

I always try to refer to both the workbook and worksheet, as not doing so may cause changes to happen in the wrong wb/ws depending on which is activated. This is especially the case during debugging.
 
Upvote 0
As it's a change event the ranges automatically refer to the sheet being changed & therefore there is no need to use workbook, or worksheet references.
 
Upvote 0
Yes, but I've made it a custom to always refer to wb/ws. Always refer to them --> never get an error related to that.
Regardless, my code ought to work
 
Upvote 0
There is nothing wrong with your code, and should work as long as the sheet names are correct.
If they aren't then the OP should have got a run time error 9, not a compile error, so there was something else going on.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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