How to date stamp last Sunday's date - excel

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.

Related

automatically add date to excel form after input

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

Excel 2016 ComboBox Date Display versus Number

This seems to be a simplistic task to get the actual date to display in the combobox versus 43466 (a number). The code that everyone seems to be using is the following with
.Text:
Private Sub ComboBox20_Change()
ComboBox20.Text = format(ComboBox20.Text, "dd/mm/yyyy")
End Sub
OR . Value
Private Sub ComboBox20_Change()
ComboBox20.Value = format(ComboBox20.Value, "dd/mm/yyyy")
End Sub
I have even tried with .text and .value. Either one keeps giving me the following error:
Compile Error:
Wrong number of arguments or invalid property assignment
Any thoughts on what I must be missing, having a brain meltdown on the most mundane and seemingly easy task.
Problem approach
The problem seems to be when you are attempting to change the value in the combobox, is there a value to begin with? If it is empty, it is taking the default version of the date.
Solution approach
Try to set the value before it goes into the Change event.
Sample code and demonstration
Private Sub ComboBox1_Change()
ComboBox1.Value = Format(ComboBox1.Value, "dd/mm/yyyy")
End Sub
Private Sub UserForm_Activate()
Dim CounterDate As Long
Dim TxtDate As String
For CounterDate = 1 To 2
TxtDate = DateAdd("d", CounterDate, Now)
ComboBox1.AddItem (TxtDate)
ComboBox1.Value = TxtDate
Next CounterDate
End Sub
Further comments
As you may see as soon as you change it, it enters again the "Change" event, you may turn off the events as soon as you enter the cycle and then turn them on again if it is causing erratic behaviour on your pc -sometimes it does-.

Excel Macro auto-refresh workbook based on user input time

I use a lot of WEBSERVICE calls in my workbook, which is connectionless. Therefore, the only way to periodically refresh values is with a macro. To do so automatically upon workbook open and every 30 seconds thereafter, the following works great:
Dim TimeToRun
Sub auto_open()
Sheets("DataInput").Select
Sheets("DataInput").Range("A1").Activate
Application.CalculateFull
Call ScheduleWorkbookRefresh
End Sub
Sub ScheduleWorkbookRefresh()
TimeToRun = Now + TimeValue("00:00:30")
Application.OnTime TimeToRun, "WorkbookRefresh"
End Sub
Sub WorkbookRefresh()
Application.CalculateFull
Call ScheduleWorkbookRefresh
End Sub
Sub auto_close()
Application.OnTime TimeToRun, "WorkbookRefresh", , False
End Sub
As usual, users claim the refresh interval of 30 seconds is somewhere between too short and too long. So, the idea is to let users fill in the interval they want in cell B9. However, there doesn't seem to be an acceptable way to put a cell number (or variable) into the TimeValue function.
Any ideas on how I might modify the macro to allow users to choose their own refresh interval, other than making the macro available for user edit (similar to handing a loaded gun, safety off, to a troop of chimpanzees)?
Use TimeToRun = Now + TimeValue("00:00:" & Sheets("DataInput").Range("A1").Value)

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()

Problems with obtaining modified date

I have an opened workbook called "Visor Portafolio.xls" and I want to write its last modified date on my current workbook (the one running the macro) into cell H6. I want to put this at the end of a macro that I use.
I've tried a lot of different solutions but it won't seem to work. Can anyone help me with this?
If you are in a macro:
Sub Macro1()
Range("H6").Select
ActiveCell.FormulaR1C1 = LastSaveDate()
End Sub
Function LastSaveDate()
Application.Volatile True
LastSaveDate = FileDateTime(ThisWorkbook.FullName)
End Function
base on: http://answers.microsoft.com/en-us/office/forum/office_2010-excel/insert-the-date-an-excel-workbook-was-last/c0c7335e-fc0d-43c7-b32d-215f84b452cc

Resources