Textbox datwe auto update

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi good morning, I have the code below which is in my textbox, but it doesn't auto update,can you help me please?
Code:
Private Sub TextBox10_GotFocus()
If Me.TextBox10.Value = "" Then
   Me.TextBox10.Value = Format(Date, "dd/mm/yyyy")
 End If

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,
GotFocus is not the right event for this.

Put it into userform initilize event and it works.
Code:
Private Sub UserForm_Initialize()
        If Me.TextBox1.Value = "" Then
            Me.TextBox1.Value = Format(Date, "dd/mm/yyyy")
        End If
End Sub
 
Upvote 0
HI havn't got a userform my textbox is in Sheet1. will this make a difference?
 
Upvote 0
Hi yes it does in Sheet1 there is no initialize event.
Should this textbox receive the date when you open it oder what do you like to achive?
Every event driggers in different situations so it is important to understand when you like what to happen.
With your code it changes the textbox value wenn that textbox get the focuse as the event says..
So you need to let us know what exactly what you like to happen.
 
Upvote 0
Hi thank you ,what I want Is the textbox which is in sheet1 in excel, so when the spreadsheet Is opened to show the current date
 
Upvote 0
Hi,
than you need this event.

Code:
Private Sub Worksheet_Activate()
        If Me.TextBox1.Value = "" Then
            Me.TextBox1.Value = Format(Date, "dd/mm/yyyy")
        End If
End Sub

On the top left dropdown you select worksheet and on the right dropdown you select activate. Just in case you don't know how to get there.

HTH
 
Upvote 0
Hi sorry thanks again I cant find the drop down for worksheet, and the activate bit, im in the developer tab and to the left I have the visual basic button
 
Upvote 0
Do I put the code in Thisworkbook or Sheet1? how do I activate, sorry I am still new to this.
 
Upvote 0
Hi,
no Problem we all where new to this .)
Doppelclick on the worksheet in the VP Editor then it opens the worksheet module.
On top you have Option Explicit hopefully :) and above there you see a dropdown (Allgemein) in german not sure what it says in english but klick on it and it shows worksheet. Select worksheet and on the righthand you see another dropdown there you select activate and click on it.

You should see something like that

Code:
Private Sub Worksheet_Activate()

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
on Sub Worksheet_Activate() you enter your code the worksheet_SelectionChange Event just delete and you are set to go

HTH
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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