Manual date selection in vba - excel

I am trying to find a way to allow a manual date + time. I have a worksheet that has a button for which when the user updates the table, they click the button and it inputs the current date and time. The challenges is I currently have the Now() function and every time I open the workbook, the date gets updated based on the now date. I want this to be a manual action.
I've searched here and on the internet, and all I can find is the automated NOW() or others that seem to automate the process.

You can use VBA to write the current date (plus time) into a cell on a button click:
Public Sub MyButton_Click()
Worksheets("Sheet1").Range("A1").Value = Now
End Sub
Just link that procedure with a button and it will write the date+time into A1 every time you click the button.

Related

I need a VBA code that after Userform enters date into a database, refreshes the cell the same way as going into it and pressing enter does

I have a long chunk of code that handles date data from a hidden datasheet, however anytime I enter a new date trough a userform, it writes it into the cell in the database in a format that for some reason can't be handled error free by my code chunk.
The problem is I need a sub to change the format (which is easy) but it does not actually apply the formatting unless I manually go into the datasheet, enter the cell and press enter.
I have searched and could not find a solution on how to do this in VBA, obviously this code is supposed to run automatic flawlessly plenty of times and having to manually go in an enter the cell defeats the entire purpose of the programming.
Sub Testing123()
MsgBox "run"
Workbooks("Excel Stock System.xlsm").Worksheets("DataNews_Events").Columns(8).NumberFormat = "dd.mm.yyyy hh:mm"
'something magic that actually automatically applies the format
End Sub

What is "normal" Excel behavior of macro called by Application.OnTime during cell edit?

I am a VBA beginner trying to determine the "normal" behavior of excel during a timed call to a macro using Application.OnTime. In my code (below) I am calling ActiveWorkbook.RefreshAll but I think this question may be relevant to the timed call to any macro. As a newbie, I want to understand the behavior and limits of VBA so that I can determine if VBA is the right tool or if I should use another solution in certain cases (perhaps python / openpyxl).
My set up: I am using Microsoft Excel 365 (annual subscription) and the Account page says I am using Version 2006 (Build 13001.20384 Click-to-Run). I have a single workbook containing a single Sheet1 which contains the formulas to import stock data using the built in feature under the Excel Data tab. I have several rows (one stock per row) of columnar data corresponding to the stock fields such as ticker symbol, price, etc.
To automatically refresh the data, I borrowed & modified code to refresh the data every minute. The code works fine. The "issue" I have is that between refreshes, if I start editing any cell on the sheet and continue to do so during the time when the next automatic call to the refresh should occur, it indefinitely delays the execution of said call until the moment I complete (or cancel) the cell edit. When it does the refresh upon my edit completion that refresh does seem to work correctly.
If I start a cell edit and do not complete it for several minutes then only a single refresh occurs when I complete (or cancel) the edit, even though several intervals may have passed (it does not makeup/catchup the missed/delayed calls).
This behavior is OK with me in this application, but I would like to understand if this is considered "normal" behavior, and if I should expect it to always behave this way. Perhaps this question extends beyond a refresh call but also to any timed call to any macro that may be delayed as a result of a cell edit in progress.
Here is my code.
This is in a file called VBA-Test-02-StockData.xlsm under ThisWorkbook (Code):
Private Sub Workbook_Activate()
'ActiveWindow.WindowState = xlMaximized
Application.Speech.Speak ("Activating stock workbook")
Call AutoRefresh
End Sub
This is in the same xlsm file under Module1 (Code):
Sub RefreshStockData()
Application.Speech.Speak ("Refreshing stock data")
ActiveWorkbook.RefreshAll
End Sub
Sub AutoRefresh()
Call RefreshStockData
Application.OnTime Now + TimeValue("00:01:00"), "AutoRefresh"
End Sub

Opening a vba-created calendar from another userform?

In 64-bit MS Office there is no more Date-Picker.
I have found a VBA-created calendar, consisting of of a class and a userform.
Original Page
English Translation of Page
I have imported the elements into my Excel file.
How could I open the calendar from my Userform2? My Userform2 has a field for date value. A call the Userform with a button then how could I call the calendar from my Userform2?
I am quite a beginner in VBA, I would prefer a detailed answer :)
Regional issue with the control
Although handy, this control will generate errors for those trying to use it outside of Germany (and possibly even within Germany) since it's hard-coded to rely on a date format of "dd.mm.yyyy" — which hasn't been an official date standard standard anywhere (including Germany) since 1996.
To fix the issue, replace each of the four offending lines with the below alternatives, which instead use the DateSerial function as an "international date handling solution". (The quickest way to locate them in frmCalendar is to search the project for "01.".)
intDayCounter = Weekday(DateSerial(dYear, dMonth, 1), vbMonday)
datLastDayMonth = fLastDayInMonth(DateSerial(dYear, dMonth, 1))
Me.Controls(strLabel).Caption = Mid(fLastDayInMonth(_
DateSerial(dYear, dMonth - 1, 1)), 1, 2) - intVormonat
Me.Controls(strLabel).Caption = Mid(fLastDayInMonth(_
DateSerial(dYear - 1, dMonth + 11, 1)), 1, 2) - intVormonat
Using the control is a userform
To add this custom control with on your userform:
Add a command button to your user form
Right-click the new button and click View Code
Paste the code below into the _Click() procedure for your button
frmCalendar.Show
TextBox1 = g_datCalendarDate
...where TextBox1 is the name of the control where you want the selected date.
Clicking the button will now open the calendar, allow the user to select a date, close the calendar, and populate the specified control with the selected date.
If the calendar is contained within a userform, find the name of that userform, then add a command button to Userform2 and use code similar to:
Private Sub CommandButton1_Click()
dateUserfom.Show
End Sub

show data refreshed time in excel

I have an excel which has sql queries associated with it. I open the excel , click on Data->Refresh All and send across to the users.
When I click on refresh All , I want the refreshed date to appear in a cell on that sheet. Can you give some hint on this.
Add another SQL Query somewhere else in your workbook
SELECT GETDATE()
Make sure you refer to the same database your original query is referring to.
when you refresh the spread sheet the date will be updated as well.
Select Data ribbon
From Other Sources
From Microsoft Query
Choose DataBase (same as one used for the original Query)
"Query Wizard - Choose Columns" Click Cancel
"Do you want to continue editing the data in Query" Click Yes
"Add Tables" Click Close
Click on "SQL Button" and add the query there "SELECT GETDATE()"
Click OK
Click on Return data to Microsoft Excel
Rather than using the Ribbon to perform the Refresh, try a little macro like:
Sub Macro1()
ActiveWorkbook.RefreshAll
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
End Sub
Pick whatever cell you like.
after searching various Excel related sites (mrexcel.com, exceltown.com and answers.microsoft.com) i've decided that the best solution (for me) does NOT need Macro or another query:
add as a last column in your existing query whatever function provides the current date and time [for Ingres it's date('now')]
hide the column from the resulting table
in the cell that you desire to show the last date & time that your table was refreshed enter the formula max(j:j). substitute j with the column that you've hidden.
simple :)
This was the easiest way for me to get a refresh date. Add a =Date.Time.LocalNow() column. Hide Column. https://exceloffthegrid.com/power-query-get-refresh-date-time/

Support needed with vba programming for a pop up calendar

I have no experience with vba and would much appreciate your assistance with the implementation of a pop-up calendar user form. This link will direct you to the source of the calendar that I am trying to introduce into my workbook:
http://www.ozgrid.com/forum/showthread.php?t=142603
I have copied the class module, the user form, and the module into my workbook. When I launch the macro, this opens up a user form where i can select the date wished on a calendar.
What I miss is to be able to send the date selected in the calendar to a specific cell in my workbook.
I would much appreciate if somebody could guide me through how to write a few lines of code that would send the date selected in the user form to a specific cell in my workbook!
Again, I am very new to this so let me know if anything is unclear in my explanation. I have spent a lot of time on this so any support is highly appreciated! It probably only takes a few moments for you but would mean a lot to me!
Try this post. It semes to give a better guide to work with datepicker control. However it shows coding to make an add-in.
Hence most basic approach for you would be to,
Add a Form
Add datepicker control
code from there
per this article.
But do remember calendar control in Excel/Access can sometimes disappear due this reason mentioned in my post.
If you are planning to use date picker control, here is the code to pass the value from form to anywhere you want ;)
Private Sub myDtPicker_Change()
Dim dtDateSelected as Date
dtDateSelected = myDtPicker.Value
'-- do anything else
End Sub
The class writes the selected Date into a Textbox. After you selected the Date, you can use the value of the textbox to set the value of the Cell.
Private Sub UserForm_Initialize()
Set clsCal = New clsCalendar 'Initialize the Class'
Set clsCal.Form(Me.TextBoxDate) = Me 'Tells the class to write the Selected date'
' into the textbox "Me.TextBoxDate"'
End Sub
So in that example, whenever you select a date, the class will automaticaly store the selected Date in the Textbox.
After you selected the date, you can use the following code to add the value to a cell:
Range("A1").value=TextBoxDate.Value

Resources