automatically add date to excel form after input - excel

In my excel code I want to add the date after information is entered into the form. I don't want to manually enter the date. It should just go in automatically once and should not be updated unless manually.

You can change the text to the most recently used date every time textbox is opened (or whatever trigger works best for you) like this:
Private Sub UserForm_Initialize()
Me.TextBox1.Value = Sheet1.Range("D" & Rows.Count).End(xlUp).Value
End Sub
And it comes out looking like this:
::
Additional References/Images:
Sub Rectangle1_Click()
UserForm1.Show
End Sub

Related

Calling a userform multiple times from within another userform - calendar date picker

I am using a calendar created by Siddharth Rout found here:How can I create a calendar input in VBA Excel?
I have a userform that sits on top of a basic database that has a number of date fields.
I would like to use the one calendar form multiple times, i.e. have a cmdbutton1 on the userform that calls the calendar and puts the date in a label1, and then a cmdbutton2 that calls the same calendar form to populate label2. Obviously these will have different dates.
I have tried to do various forms of temporary binding to variables and nothing has worked. Any help would be appreciated!
You can create instances of the UserForm, set these up when you want and display when you need. From these instances you can modify the child userform
Private Sub CommandButton1_Click()
Dim ufrm2 As UserForm2
Set ufrm2 = New UserForm2
ufrm2.Label1.Caption = Me.CommandButton1.Caption
ufrm2.Show
End Sub
Private Sub CommandButton2_Click()
Dim ufrm2 As UserForm2
Set ufrm2 = New UserForm2
ufrm2.Label1.Caption = Me.CommandButton2.Caption
ufrm2.Show
End Sub
Giving:
I was looking at this question, and I think I have found the answer #Pete_B_C was looking for. I have added some code to the Userform "frmCalendar" to get the desired result.
'~~> Insert Selected date
Private Sub DTINSERT_Click()
If Len(Trim(Label6.Caption)) = 0 Then
MsgBox "Please select a date first", vbCritical, "No date selected"
Exit Sub
End If
*'~~> Change the code here to insert date where ever you want
If UserForm1.Visible = True Then
'do something
UserForm1.TextBox1 = Label6.Caption
End If
If UserForm3.Visible = True Then
'do something
UserForm3.TextBox12 = Label6.Caption
End If*
Unload Me
End Sub
I have used the option to hide and show the Userforms depending on which Userform is being used. If anyone knows how to do this in a better way than the way I have figured it, please let me know

How to date stamp last Sunday's date

I'm looking to have a couple of dates stamped onto the spreadsheet on opening.
My current script is as follows:
Private Sub Workbook_Open()
'Automate start time on opening
With Worksheets("Time Recording Estimate")
If IsEmpty(.Range("B4")) Then .Range("B4").Value = "Start"
End With
End Sub
I now wish to also add a stamp for the last Sunday, hoping it can look something like the following:
Private Sub Workbook_Open()
'Automate start time on opening
With Worksheets("Time Recording Estimate")
If IsEmpty(.Range("B4")) Then
.Range("B4").Value = "Start"
.Range("V3").Value = Now() - Weekday(Now()) + 1
End With
End Sub
I'm not sure which part of the formula is wrong and is failing to go through.
Much appreciated for any assistance offered.
Looks like your code is missing an End If:
Private Sub Workbook_Open()
'Automate start time on opening
With Worksheets("Time Recording Estimate")
If IsEmpty(.Range("B4")) Then
.Range("B4").Value = "Start"
.Range("V3").Value = Now() - Weekday(Now()) + 1
End If
End With
End Sub
Once I added that (and changed the worksheet name to one that I actually have), it ran properly: if cell B4 is empty then then it puts "Start" in cell B4 and Sunday's date (today in this case, since it's Sunday) in cell V3.
I'd suggest that you [always] add this line to the top of any module:
Option Explicit
This will "force" proper declaration & usage of variables, objects, properties, etc, and will save you a lot of headaches in the future.
Also, you should compile the code, so you can see where problem lie. Here is a quick overview of the process.

Excel 2013 64 bit - VBA Userform - Add 28 days to date of textbox and display in another textbox

In my Userform I have a textbox "ADD_Inc_DATE_TXT" which the user manually enters the date of the incident.
I have another textbox "TxT_SWIRL_DueDate" which should display the result of the adding of 28 days to the date entered in the "ADD_Inc_DATE_TXT" textbox or even just add 1 month to the date of the incident.
I have tried this in a module named "Mod_SWIRL_Due_Date":
Sub SWIRL_ExpiryDate()
TxT_SWIRL_DueDate = CDate(ADD_Inc_DATE_TXT)
ADD_Inc_DATE_TXT = DateAdd("m", 1, ADD_Inc_DATE_TXT)
End Sub
But this doesn't seem to do anything at all.
I would like the "TxT_SWIRL_DueDate" to display the expiry date when the "ADD_INC_Time_TXT" textbox is selected.
And in anticipation of the User amending the Incident Date, I'd like it to update the TxT_SWIRL_DueDate with the amended date.
Further Information:
I have included below all the code I have relating to dates. I have 4 Date Pickers/Popup Calendars ... and none of the dates come up in the dd/mm/yyyy format. (having some problem with putting the code in the 'code sample' )
Private Sub Calendar1_Click()
ADD_Inc_DATE_TXT.value = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.LBL_Inc_Day_Type.Caption = Format(ADD_Inc_DATE_TXT.Text, "ddd")
End If
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.ADD_Inc_DATE_TXT.Text = Format(ADD_Inc_DATE_TXT.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar2_Click()
TXT_AssetMgr_DATE = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.TXT_AssetMgr_DATE.Text = Format(TXT_AssetMgr_DATE.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar3_Click()
TXT_LastUserDATE = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.TXT_LastUserDATE.Text = Format(TXT_LastUserDATE.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar4_Click()
ADD_Date_ServiceJobLogged_TXT = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.ADD_Date_ServiceJobLogged_TXT.Text = Format(ADD_Date_ServiceJobLogged_TXT.Text, "dd/mm/yyyy")
End If
End Sub
‘Under Private Sub UserForm_Initialize() I have the following date related code:
Private Sub UserForm_Initialize()
Me.ADD_Date_Recorded_TXT.value = Format(Now, "dd/mm/yyyy") ‘ this works perfectly (correct format is returned)
Me.ADD_Time_Recorded_TXT.Text = Format(Now(), "HH:mm")
‘***Note: my system date is dd/mm/yyyy and in the spreadsheet cells, the format is set to dd/mm/yyyy
Use this code in the form's code module (right click the text box, "View code"):
Private Sub ADD_Inc_DATE_TXT_AfterUpdate()
TxT_SWIRL_DueDate.Value = Format(DateAdd("m", 1, CDate(ADD_Inc_DATE_TXT.Value)),"dd/mm/yyyy")
End Sub
I recommend useing the AfterUpdate event, as the Change event fires every time the value in the textbox gets changed - eg also while typing. This only fires when the user has finished typing, and moved to the next tiem on the form (clicks somewhere else).
Also consider using some form of date control instead of textbox control. There are quite a few out there, and depending on the Excel version you are using, you may only be able to use a select few of those.

clock in and clock out system

hi there im creating a spreadsheet to use for clocking in and out of work and have a simple wee GUI set up already. along with this i have the current time showing as soon as i start up the worksheet but it shows on every page i was wondering how to stop this?? sample code below:
Global clockOn As Boolean
Sub runClock()
Range("M15").Value = Now()
If clockOn = True Then
Application.OnTime Now + TimeValue("00:00:01"), "runClock"
End If
End Sub
Sub Auto_Open()
clockOn = True
runClock
End Sub
Sub stopClock()
clockOn = False
End Sub
on top of this i will be doing a macro to put the information onto a specific page all going well as this will be depending on the day selected from the drop down menu any help with this would be greatly appriciated :) as in maybe an if statement in the VBA code to select the correct page and leave the data there.
You need to specify the sheet that you are putting the value on; currently you only specify the range, but you don't specify the sheet. Can be done multiple ways, such as:
Sheets(1).Range("M15").Value = Now()
This selects the first sheet in the workbook. If you have multiple workbooks, you will need to specify that somehow as well (by referring to the active workbook, or the name of a workbook, etc.). For example:
Activeworkbook.sheets(1).Range("M15").Value = Now()

Set date format as dd-mmm-yyyy in activex textbox VBA

Is there a way to set date format as dd-mmm-yyyy in ActiveX TextBox?
I'm using the code below to separate the quote but there is one error while user is putting month., i.e. 31-May-1993 but sometimes user is putting 31-may-2015.
Because of that unable to fetch the data through server...
Private Sub TextBox1_Change()
If TextBox1.TextLength = 2 Or TextBox1.TextLength = 6 Then
TextBox1.Text = TextBox1.Text + "-"
End If
End Sub
Can I suggest you take a different approach with this and do something like (Note this is untested and not final code just a few suggestions for how you could handle this):
Private Sub Textbox1_LostFocus()
Dim Da as Date
Da = CDate(Textbox1.Text)
Textbox1.Text = Format(Da, "dd-mmm-yyyy")
End Sub
This will:
Take whatever text is entered when the user clicks outside of the active box (LostFocus instead of running every key press)
Convert the value to a Date
Put back into the Textbox in the format that you require (dd-mmm-yyyy)
You could then add error handling in this so that if vba can't convert it to a date it could report to the user that they have entered an incorrect value and to correct it before proceeding.
I'm also not advocating that this is best practice but a way that the OP could get his method to work.

Resources