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
 

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.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, ThisWorkbook.Sheets("REFERENCE").Range("B9")) Is Nothing Then
[COLOR=#333333]    [/COLOR]ThisWorkbook.Sheets("REFERENCE").[COLOR=#333333]Range("A1").Value = Application.UserName[/COLOR]
End If
Application.EnableEvents = True
End Sub

Place this in the sheet you want this to happen in
 
Last edited:
Upvote 0
Hi,
It gave me Compile error: Invalid or unqualified reference and .Range became highlighted.

Thanks
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("B9")) Is Nothing Then
   Range("A1").Value = Application.UserName
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks Fluff it worked perfect. Just another question to tell you that in cell B9 I'm going to have this formula =TODAY(). Does that code work or I have to do something else?
Thanks
 
Upvote 0
No that code will only work if you enter a value manually.
You can move it to a calculate event, but as your formula is volatile, I would advise against it.
 
Upvote 0
Sorry Fluff,
I have a problem that when I clear the contents in B9, cell A1 remains populated. Is there a way so that when I clear contents in B9, cell A1 becomes unpopulated?

Thanks in advance
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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