Excel VBA: Get the right date into a cell using DatePicker - vba

I have a UserForm with a DatePicker control in it.
It works fine, except when copying the selected date to the spreadsheet.
This is the code:
Range("A1").Value = UserForm1.DTPicker1.Value
Which returns:
00:00:00
In cell A1, no matter what date has been selected.

You cell formatting might be set to Time instead of Date.
Try changing that to see if it works.
Also, make formatting 'General' AFTER this step, to see if anything has been pasted in "A1".

You should need an intermediate variable to get it, like below:
t = Me.DTPicker1.Value
ws.Range("A1") = t

Make sure that the command to transfer the data is located in the same form or page of a multipage form as the DTPicker itself.
For some reason it wont work when the two are separated and will display a zero in the target cell. That zero is the "time" part of the date which has been switched off. Although the "Date" part refuses to transfer over, for some reason the time does, and so that zero time is read in the cell as time zero which is mid-day.
So in summary:
Keep the DTPicker and transfer control on the same page and that should solve the problem.
The code is pretty simple. It's
Sheet1.Range("AA9") = Me.DTPicker1.Value

Related

Preventing excel from altering cell contents to take literal values

I have scripted out a module that reads data I paste in from a SQL dump, and converts it into a data insert script to SQL. It is working great, only problem is for cells that contain items like:
11-20
instead of filling my value as '11-20', it is converting it to '20-Nov'.
I have adjusted how i read the cell from text to value to value2, Text comes the closest to right (rest do a math calc that tosses my overall sheet off even worse, namely dates). I have also tried such things as a Range("X:X").clear and clearformat as well. This also does not do the trick.
How do I force my string read of this cell to be the literal CSV content, and ignore the formula/calculations that excel is tossing at me?
EDIT:
Thanks to BraX for the solution!
I was unable to accomplish this by copying from one tab to the next within Excel, but i did get it to work by pausing my operation with a message box prompting the user to simply navigate to SQL and put the contents of the data in to the clip board. This works perfectly now!
Cells.Select
Selection.NumberFormat = "#"
MsgBox "Please navigate to SQL and copy your data to be insurted, including headers. When done click OK"
Range("A1").Select
ActiveSheet.Paste
Before adding the data to the sheet, set the NumberFormat to # for the affected columns.
Example:
ActiveWorkBook.WorkSheets("Sheet1").Range("A:A").NumberFormat = "#"
That will format the values to Text and prevent them from being interpreted as a Date when the data is added. Once Excel decides it's a date, it's too late.
i am pretty sure all it takes is to format the cells, right click on the range of cells you want formatted as plain text, click "FORMAT CELLS" and under the "NUMBER TAB" it shows the "CATEGORY" of the type of formatting, e.g: "General, Number, Currency, etc" select where it says "Text" and click ok, any text will be evaluated the same way you write it, so even if you write "11-5-18" it will be just that and won't be considered a date or anything.

Excel, Stop update NOW(), when other cell changed value or string

I've in a single row of excel
Start Time, End Time, Duration & Status
End time is being calculated using NOW(), function of excel. I want this to be stop updating value. If status cell in the row is changed to Stop from Running.
This Stop is being done by manually.
A possible solution to this problem is to use drop-down lists with data validation. (This can be done without VBA.)
Data validation drop-down lists are always non-volatile, even if you choose a volatile value from the drop-down list.
Simple example that you can use to apply to your spreadsheet:
Assume cell A1 contains the formula = NOW(), and that cell B1 is set up with data validation in the following way:
Now cell B1 will take a "snapshot" of NOW() whenever you choose the only available option from the drop-down list, and it will keep that value indefinitely until you reselect from the drop-down.
Any cells that need NOW() can instead just use $B$1, which is basically just NOW() but it is frozen in time until you reselect again from the drop-down list.
This is a sneaky way to convert a volatile cell to a non-volatile one (without using VBA).
If you want to use vba, Range("A1").Value = Now() is essentially the Now formula that runs only when the macro is triggered.
Of course, switch out Range("A1") with whatever cell you need the date. Without seeing your code I can't offer any suggestions. Personally I've used offset to select the paste location.

How to link files in excel with a text in the path changing dynamically?

I have a file which needs to fetch data accordingly every week.
Let us say the formula to get data in a cell is
=\T:\Datafile\weekdata\2015\Week01\[Summary.xlsx]Sales'!D4
I have a cell in the sheet C3 which changes every week accordingly.It will change to week02 next week and i wish the path to change too..to
=\T:\Datafile\weekdata\2015\Week02\[Summary.xlsx]Sales'!D4"
I tried doing a concatenation to make the path dynamic
="\T:\Datafile\weekdata\2015\"&C3&"\[Summary.xlsx]Sales'!D4"
but it doesn't seem to work out.I checked evaluate formula and it resolves C3 to Week02 but the value doesn't come in the cell.In stead just the below text
\T:\Datafile\weekdata\2015\Week02\[Summary.xlsx]Sales'!D4
appears in the cell instead of any number.
Let me know where am i going wrong and how to resolve it.
Try =INDIRECT():
=INDIRECT("\T:\Datafile\weekdata\2015\"&C3&"[Summary.xlsx]Sales'!D4")
This function does exactly what you are trying to do, takes a built string and makes it a cell reference.
The problem with =INDIRECT() is that it only works if the source workbook is open:
If the source workbook is not open, INDIRECT returns the #REF! error value.
(That quote is from https://support.office.com/en-sg/article/INDIRECT-function-21f8bcfc-b174-4a50-9dc6-4dfb5b3361cd)
By googling "excel convert text to formula" I found this SO Q&A: How to turn a string formula into a "real" formula
If you don't like the VBA solution, I had success with
iDevlop's =EVALUATE() solution.

Copy Value Of Cell, Not Formula

I am trying to grab only the current number of minutes in an hour.
I use the NOW() function and I have the format of the cell it's in set to only mm. So in the cell display, if it's 9:08, I get 08.
Whenever I try a excel formula like =VALUE(D5) or =D5, I get (I'm assuming this is what it is) an unformatted date string.
I am now looking to macros using VBA to solve my problem.
I've tried:
Worksheets("Sheet1").Cells(23, 1) = Worksheets("Sheet1").Range("E20").Value
However I get the same thing as with my excel formulas. Is there any VBA way of getting only the value that's displayed in the cell and copied to a new area. It doesn't really need to be copied over, I just need to use the value further in my macro.
I'm open to other ways of getting the current number of minutes in the current hour as well.
Do try not to confuse formatting and data.
why not use =MINUTE(D5) in a cell which will set that cell to the minute value irrespective of formatting? Use that cell as your source in your VBA.
=HOUR(D5) would operate similarly but return the hour part.
If the displays 9:08
then select that cell and:
Sub dural()
Dim s As String
s = Split(ActiveCell.Text, ":")(1)
MsgBox s
End Sub
Note this is a String
Might be calculated like so:
=60*(NOW()*24-INT(NOW()*24))

How to VBA change cell's value (display text) without changing its formula?

I've a problem with this VBA macro.
Sub demoChangeText()
Application.Range("B2").Value = "Overidden text"
End Sub
My test is here. To run this macro, open it in Excel, press Ctrl+F8 and choose demoChangeText.
As the result, this macro changes the value of cell B2 (the text displayed to us) but clear its formula. I need to change B2's value BUT also need the formula to be remained.
So my question is How to change the display text of cell without changing its formula?
UPDATE
I ask this question because I'm trying to solve this problem
I'm not sure if this will help, as it is a bit hard to tell what your underlying requirement is, but here goes anyway:
Several things affect the display of a cell:
the entered value, if its a constant
the result of a calculation, if its a formula
the format of the cell
the conditional format(s) of the cell, if any
In the example sheet provided you have a formula of =ROW()&COLUMN() which returns a string result of 22
You can make this display something else by applying a cell format,
eg a format of 0;0;0;Ov\e\r\ri\d\d\e\n t\ext will display any string value as Overridden text
This can by applied with VBA with
Range("B2").NumberFormat = "0;0;0;Ov\e\r\ri\d\d\e\n t\ext\s"
or
Range("B2").NumberFormat = "0;0;0;""Overridden texts"""