I have a spreadsheet with numerous formats, mostly time and whole number. Data validation is present to assure proper formats. certain fields can be selected and VBA programmatically inserts the system time to the selected cell. This works perfectly and is very helpful when using this sheet. The problem occurs if a cell that should have a whole number (and definitely not a time) is accidently selected, then the "enter system time" code is run. This changes the format of that cell to time/date. said in another way, A1 is formatted for Number, 0 decimal places. A1 is selected and the "Enter system time" macro is run. The macro is not affected by the data validation rule that would otherwise prohibit anything but a whole number between 1 and 999. Now that the formatting is changed by VBA (somehow), when you enter, say, 5 into that (formerly) number-formatted cell, displayed is 1/5/1900 0:00. Because this is a compiled workbook, the change is permanent; the cell format cannot be changed back.
so, my question is: Is there a workaround? Is there a way to prevent this sort of format change? is there a way to unlock the cell if the format is wrong? Is there any sort of suggested method to avoid this?
You can check if the format is a number with 0 decimal places using the NumberFormat property of the Range object like:
If Range("A1").NumberFormat = "0" Then
'Some Code
End If
Check https://msdn.microsoft.com/pt-br/library/office/ff196401.aspx for more information on formats
Finally got this. The correct number formatting command is:
If Selection.NumberFormat = "[$-409]h:mm AM/PM;#" Then...
Related
READ CAREFULLY THE QUESTION PLEASE, IT MENTIONS EXCEL VBA.
NOT EXCEL
I was given the task of fixing a date input on a VBA form. A textbox should have the user enter the date as MM/DD/YYYY.
I am required to use an input mask, not allowed to do something as validating date after or using a calendar. So far I was able to use the 2 methods mentioned (forcing the format after using ISDATE).
However, it has now been made clear it has to be a mask so keys are filtered on entry, with the mask being visible when entering the date: __/__/____
Where you see underscore, he should only be able to enter numbers and the / are always at those positions
Is there a way to do this? I can only find a tutorial for the mask in Access VBA.
I Googled "Excel input mask" and the 2nd result was: Using an Input Mask Microsoft Excel written by Allen Wyatt...
Using an Input Mask
...You may wonder if there is a way to set up an input mask that will add the colon automatically. The good news is yes, there is. The bad news is no, there isn't. Sound confusing? Let me explain...
You can set up a custom format that will display your time in any format you want. For instance, you could use the following steps:
Select the cells you want to use for time input.
Choose Format from the Cells menu. Excel displays the Format Cells dialog box.
Make sure the Number tab is displayed.
In the Category list, choose Custom.
Replace whatever is in the Type box with #":"00.
Click on OK.
You can now enter your times using just digits. The problem (and this is the bad news) is that the cell doesn't really contain a time. If you enter 230 (for 2:30), it doesn't contain 2:30 as a timeāit contains two hundred and thirty. Thus, you can't use the contents of the cell directly in time calculations.
To overcome this, you can use another column to show the entered digits converted into a time. All you need to do is use a formula to do the conversions. For instance, if the time you entered was in cell A3, you could use the following formula in a different cell to do the conversion:
=(INT(A3/100)/24)+((A3 - (INT(A3/100)*100))/1440)
Format the cell that contains the above formula so it displays one of the various time formats, and you are all set.
(Full article and more at the source.)
This post may help you...
Formatting MM/DD/YYYY dates in textbox in VBA
We have an internal software that exports data reports to Excel, and several of the columns contain dates in the MM/DD/YYYY format. I have a spreadsheet setup where I will copy these exported reports over to, and then I have formulas already setup to look for these dates. The problem currently is that all dates in months 1-9 come in like this: 01/22/2017.
The formulas do not recognize them until I activate each and every cell, and hit enter, and then it re-formats to: 1/22/2017 and then everything works. Currently I am having to go cell by cell and activate, and hit enter. Simply selecting all of the cells, and changing the formatting to a Date hasn't worked. Is there a faster way around this? I'm open to VBA if it works, however I'm wondering if there is a simpler method I'm missing, or simply a way to get my formulas to recognize the original date formatting.
EDIT:
The data is exported from a SQL database. The formulas that need to reference are using <= in reference to week start dates on another sheet. I initially tried having those match this formatting, but because the dates come as General, math operators don't work at that point.
Select the date column and then click Data > Text to Columns
Next, Next, then select Date 'MDY'
then you should be able to do this using a number format of m/dd/yyyy or m/d/yyyy if you don't want a leading zero in the days as well
select the range and run:
selection.value = selection.value
(for some cases this does not work)... in such moments this should do:
selection.value = evaluate("INDEX(" & selection.address & "+0,)")
just keep in mind that there may be a need of changing the formatting of the cell...
This also can be done in a formula, without having to go through menu options, thus making it more automatic. Use TEXT and command and format the date exactly how you want. Leaving off leading zeros on the month or day is as simple as including just one character for them instead of two:
=TEXT(date-field,"M/D/YYYY")
instead of
=TEXT(date-field,"MM/DD/YYYY")
VBA can turn over Excel anyway you want .SO pl understand this question is NOT the duplicate of a similar question that asks how to get it done using VBA.
I would like to assign the current date to a cell and PRESERVE that value
That means after opening the file a few days , on the value should NOT change How do I get this done using a NON VBA formula based approach
a1=Date()
but the date value does not change.
Thank you both. You both rock. But stack will not let you do > 1
Both answers are practicable but given the reqs, I had - I just wanted to
'dirty stick in' the date wherever I want without scratching around .
The 1st answer is useful if you have a conditional logic 1 col
update should get a date in ,the other column. The 2nd one does the
quick stick of the date.
I don't see the problem. If you want a static date in a cell, just click on the cell and enter something like:
2/11/2016
The date will not change unless you change it.
If you are not certain of the current date, then click on the cell and touch:
Ctrl + ;
Like said many times at this side:
Goto options -> formulas -> enable iterative calculation
Then in A1:
=IF(A2="","",IF(A1="",NOW(),A1))
If you put anything in A2 then A1 will become the actual date/time and stay with this value as long as A2 is not empty... it will not change in later calculations ;)
You can try this with the actual formula (showing also time including seconds) it will not change. If you empty out A2 and reinsert something, it will jump to the new time and then stay with that value as long as you want (even close/open will not change that)
I want to ask a very simple and maybe for some it may sound silly.
One person told that while copying and pasting in Excel you should use Paste Special and paste formats first and then values, then it keeps any leading zeros or else they will be removed. But someone else told other way round i.e. first values and then formats. I didn't notice any change also if I pasted as it is after copying.
So please tell me if these people are true and if yes which exactly is the correct order?
Oh, the joy of Excel conversions. You have to be cautious with your different circumstances.
Time to have some fun. Try this:
In cell A1 format the data as Text and enter the value 010.
In cell A2 leave the format as general and enter as 010.
Now go to the immediate window in VBIDE and execute the following:
? Typename(Range("A1").Value)
? Typename(Range("A2").Value)
A1 is a string, and A2 is a double.
If you change the format of A1 now to General then again type:
? Typename(Range("A1").Value)
It is still string - AND it still has the leading 0 at the front!
HOWEVER: now execute the following:
range("A1").Value = range("A1").Value
Although this looks like a pointless command - its effectively updates the cell by using VBA. Excel will now do the conversion to a double!!
? Typename(Range("A1").Value)
This is now a double.
So altering the format after the data can result in different data because Excel is doing a clever conversion. But this is dependant on the cell being updated. Just changing the cell format might change that value, but not necessarily - and later it could change if a user presses F2 and enter on the cell. Lovely - thank you excel for being intelligent.
Don't even get me started with other conversions.
So, I suggest that in the majority of cases, you should actually format first and data after.
Happy pasting! :)
Cell value and cell format are two distinct properties of a cell in Excel.
For displaying values, excel applies the format of the cell to its value. So if your cell holds a number and the format specifies that the number should have leading zeros, the number will be displayed with a leading zero. This does not change the underlying value!
The format affects only the display.
As an example:
Let's say your cell holds the value 5.5 as numerical value. Applying the format 0.00 will display that value as 5.50. Applying the format 0.00 min will display the same value as 5.50 min. But the value itself is still unchanged 5.5 - this way, you can e.g. be very specific about how you want to display a number, but still use its plain value for calculations. Same principles apply to every other formatting rule and data type of course.
So no matter which way round you paste - as long as the cells end up with the correct combination of value and format, you will end up with the correct result.
Using VBA, I import a csv file and put a bunch of data into several columns.
One of these columns has a date and time. As I need to be able to use just the 'time' part of these cells, I try to convert the entire column to Time by using (and just about every other variation)
Cells(x.y).EntireColumn.NumberFormat = "hh:mm:ss"
or
Range("C1").NumberFormat = "hh:mm:ss"
Range("C1").EntireColumn.NumberFormat = "hh:mm:ss"
However, this does not convert the entire column. I've tried every possible other way of selecting the entire column and changing it (through VB) however still only a portion remains converted.
If I doubleclick on these unconverted cells and press enter they change to the correct format. I realise this is a common problem relating to Calculations but my workbook is set to Automatic Calculations and I've tried setting this in VB too. This doesn't change anything.
The only pattern I can find is that the cells stop being converted when the Day reaches double digits. For example:
Column C
01/05/2013 7:28:56
03/05/2013 13:24:53
07/05/2013 20:13:24
09/05/2013 8:29:22
12/05/2013 9:28:56
15/05/2013 21:14:25
17/05/2013 7:28:56
Becomes:
Column C
7:28:56
13:24:53
20:13:24
8:29:22
12/05/2013 9:28:56
15/05/2013 21:14:25
17/05/2013 7:28:56
In the formula bar up the top for each cell it still shows the whole Date and Time for all cells, not sure if this is related, but doesn't seem to matter in terms of the calculations i have to perform using the Time.
Essentially I have to take the time for a cell in column C and the time from another Cell (also in Date/Time format) and check the difference. After some research I decided the best way was to convert all the cells to a time format and then do my calculations.
Alternatively I could try converting the column to text and using a Split function (using space as a delimiter) and pulling the time out, but I'm having trouble doing this too, as once again trying to convert the entire column to text stops at the double digits for date.
Thanks for reading through all that, any thoughts and help would be appreciated.
Edit: Realised some of my syntax was incorrect in my post, this was however correct inside my macro
another edit: I think this definitely has something to do with the date format... I just realised that before i format them, the dates are m/dd/yyyy and then when it gets to actual double digit days it changes to dd/mm/yyyy, and thats when the problem occurs...
In order to avoid confusion, and as the date always seems to occupy the same width, I recommend to
1) import this column as a text
2) then go over the whole column
For Each C In Range("A:A").Cells
If C <> "" Then
' ....
End If
Next C
3) cut away the leading 11 positions, e.g. C = Mid(C, 11, 99)
4) convert the remaining string to a time, e.g. C = CDate(C) (... yes it works with a time as well, because a time is a fractional part of a date)
Alternatively you may want to capture the date part and bring it into shape, too. See here for some ideas using worksheet formulas.