Excel VBA InputBox value is not a date - vba

I have a user input box where you type in a string, annoyingly this string looks like a date 00/00/0000 and excel reformats it as such.
When the value can't be a date ex. 18/19/4561 (month can't be 18 or 19) it displays it correctly.
But whenever it can be seen as a possible date it switches things around.
I've tried setting the value as a string rather than nothing but excel still changes it when putting it in the page.
When I try manually inputting it in the cell or equal the values from a manually entered cell it works fine.
But whenever I get it from the inputbox it messes with it. Even when I hard code the string to a variable (x = "05/06/4564") it switches things around.
How do I force excel to leave the string as is?

Prefix the value with a single apostrophe and Excel will interpret it as a string.
Eg '18/19/4561
Also, have you tried setting the cell format to Text

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.

How to use UserForm Values for multiple things

I have UserForm1 which is a multipage userform and I am trying to access the information that was gathered through the form in a sub located in Module1. This sub will need to access several different values and do different things with those values so this is going to be a multipart question.
I have the below code in which I attempt to use one of the values as the upper limit of a For Next Loop. However the current problem is that when the code reaches this line it jumps to the Userform_Initialize routine.
For X = 1 To UserForm1.LocalOffer.Value
Second part of this question comes from inside the For Next loop from above. Where I have the below code. Which would ideally allow me to cycle through a series of similarly named Textboxes from the userform. Not even sure if that will work as the code keeps breaking before getting to that part.
Range("B" & X).Value = UserForm1.Controls("LocalTier" & Tier).Value
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
I am using Excel 2013 but the file will be ran on Excel 2007
Edit:
Turns out that problem 1 was fixed by not closing the userform with the X button but instead adding a line to hide the userform when you hit the last button. As it turns out my code for the second question worked just fine once i got past that. Only question left is the last one which I have no ideas on.
As from the comments, I see you don't need anymore to know about points 1 and 2, I will hence limit my answer to the point 3.
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
You can use either string manipulation, or date conversion.
Let's assume the Textbox is called myDateTextbox
String manipulation
Among the string manipulators that VBA provides, I would cite Left() and Right().
For example:
last_digit_of_the_year = Right(myDateTextbox.Text, 1)
will return you the last character of the string. On the other hand:
first_digit = Left(myDateTextBox.Text,1)
will return you the first digit of the string.
You can use the Len(myDateTextBox.Text) built-in to return the current length of the string.
Date conversion
You can simply convert your string into date using the CDate() function. Please note this function will return an error if you pass an invalid string. If your textbox contains 24/01/1990, you can first convert the string into a date:
myDate = CDate(myDateTextBox.Text)
Hence, you can retrieve day, month or year like this:
myYear = Year(myDate)
myMonth = Month(myDate)
myDay = Day(myDate)
Please note that CDate recognizes date formats according to the locale setting of your system.. Hence, if the format in the TextBox is not the same than the one of your system, then consider manipulating the string before to adapt it to the proper format. For example, if your system has settings DD/MM/YYYY and your textbox shows a MM/DD/YYYY type, you can "adjust it" as follows:
wrongFormat = myDateTextBox.Text
splittedDate = Split(wrongFormat,"/")
goodFormat = splittedDate(1) & "/" & splittedDate(0) & "/" splittedDate(2)
If wrongFormat was 1/18/2014 but your system would like to read it as 18/1/2014, it's now fine because goodFormat will be equal to 18/1/2014 after the split and re-build.

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

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

vba treat time as string

I am storing some values in my excel using vba and them comparing with other results. The results can be whatever: name, surname, address, time...
The problem that I have is that when I store a string that has a date format it takes it as time automatically. For example if I enter 8:15 it automatically convert it to 08:15 and then when I am comparing it with other value that is the same (8:15) it returns me false because it is not 08:15.
So basically the question is... how can I introduce a value like 8:15 without being converted to 08:15?
Two methods:
Put an apostraphe at the front of the value to tell Excel it's a string. Example: The value '8:15 will be interpreted as a string of value "8:15".
Use a formula to store the value as a string. Example: ="8:15" is also interpreted as a string of value "8:15".
I'm assuming this is when you're entering the values in Cells. the format of the cells is probably general, thus it formats things like numbers or dates different. Change it to text.
select all (or specific columns) right click - format cells - number tab - text option. it says there "text format cells are treated as text even when a number is in the cell. the cell is displayed exactly as entered"
edit
another option, in VBA use .FormulaR1C1 instead of .value when comparing. .value is the actual value of the cell (like a formula result) where as .formulaR1C1 will give you the actual entered text. so .formulaR1C1 might get you the actual entered 8:15 instead of the corrected value: 08:15.

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"""