Time value not being input correctly into spreadsheet - excel

I have a userform set up in Excel. It helps me to enter some values into a spreadsheet. I've used the following code numerous times, and every other time value goes its cell correctly. This one always comes up as "TRUE" on the spreadsheet. I don't know what's wrong.
ssheet.Cells(nr, 18) = Me.jobTravelStart
I've even tried
ssheet.Cells(nr, 18) = CDate(Me.jobTravelStart)
and I still get the same thing.

Ah, Excel and datetime values... Always fun (or trouble).
Problem is that excel uses one datatype for storing datetime values internally, and uses another to display it (I know, I know, this is technically not correct).
To troubleshoot these kind of problems, always go back to source, so check if the stored data is correct. Either use a Debug.Print statement to output the variable while the code is running, or check the local variables when debugging your code.
When you verified that the source is correct, focus on the display part. As xQbert stated in the comments, check the datatype set for the cell you're using to display the data. Make sure the datatype is correct, BEFORE the data is entered into the cell (if need be, you can explicitly do it in code, but that is quite slow, so do it only if there is no other option).
If all this does not help you, please provide more details on how it fails you and what you have found debugging. People can only help you if you provide enough info.

Related

Getting the excel displayed value

I've been scouring for days and I can't seem find what should be an easy answer. The displayed value is not necessarily the value that from cell.InnerText. I see post after post that requires you to look at the cell.StyleIndex property & cell.DataType.value to determine how to get the displayed value. And while I have plugged in some of this code and it works I hit another case I didnt handle where the cell StyleIndex is null which I can only assume the cell is formatted as "General". The on screen value "-39718.96" while the cell.InnerText returns "-39718.959999999999". I realize there is a easy fix to this by Double.Parse(value).ToString() but there seems to be an endless supply of formats https://stackoverflow.com/a/4655716/1713000 and that's a shortlist and dates are another problem. It seems we are left to handle each different style ourselves. Did Microsoft really not create a public api that returns the formatted displayed value and has no one really written a complete solution to handle this cluster of a mess?
Using Openxml for sheets is very difficult (much more so than using it for Word). If I were you I'd use the ClosedXMl nuget package as it simplifies Openxml for sheets.
Will use the ClosedXML lib instead

Structured references: Keep names through copy-past

So I'm using structured references for user data. Eventually we'll have a proper import procedure, but I was hoping to push that back to later and instead use copy-past of their raw data for now.
So I have a table, say tData, that contains a a copy of raw data output. That output already comes with headers, which are actually used in the structured references to the data throughout the workbook.
My problem is that the raw output isn't 100% stable - e.g. some columns may appear & go. Those aren't used in calculations, but it does affect slightly the table structure (position of columns). I can't control the raw output.
I was hoping to instruct them for now to copy-past directly. As I am working with the headers & not position, I thought the formulas would still work.
Summarize in the images below: =tTest[Column2] is the formula set for the box on the right. It should refer to the content of column2 (so 2). If I copy-past different headers, with "Column0" that shifts everything to the right, you can see that a positional reference is actually used by excel to refer to your data. It now returns "1" and Excel even changed the formula to "=tTest[Column1].
Seems wrong to me - e.g. if you reference something by name you don't expect it to actually be referenced by position.
I already tried tTest[[header]:[header]] and it doesn work either.
So, in the end, better write a proper import procedure I think. I won't post here because it's somewhat involved and there are various checks to perform. But overall steps are:
File picker to select the data to input
Copy everything into ThisWorkbook, close input file
Match the column headers from input data with mytable.HeaderRowRange
Copy the raw data into the appropriate column if match if found
So basically write a script to do a match on column headers, which is was I thought Excel would be doing. And still think it should do. However thinking about it, it is probably a lot simpler to code it this way than to have it actually dynamically adapt named references and not positions....
As mentioned by Bob Phillips, you can use INDIRECT. However that does involve adding an extra INDIRECT(reference_to_table_here) to, well, all references. Excel worksheet formulas are clunky enough I think without have to make them any clunkier.
Even though you write the formulas using column names, in the background Excel actually uses position of the column within your table for its calculations. Whenever you change the layout of the workbook, it updates the position for you.

Excel iferror for chart series or another workaround

I have a very interesting issue. I'm trying to create a dynamic chart. This works well for the most part, however, when I change one of my parameters it makes the chart references invalid, which causes an incredibly annoying pop-up saying there are invalid references... I've tried to use an iferror([series formula],[generic formula]) but this is an invalid error.
So, I will accept one of two solutions... Either a way to use iferror() with my series, or a way to turn off the obnoxious pop-up message.
Some more pertinent information :
I'm using dynamic named ranges
The dynamic ranges point to validation boxes (another thing I've considered is figuring out a way to auto-update the validation boxes since the issue is caused when validation box A is changed but B and C aren't changed yet.) The image attached will hopefully help explain....
Thank you for any assistance you can give!!
The problem is when the start date is later than the end date, right? Somewhere I usually use MIN(StartDate,EndDate) as my real start date and MAX(StartDate,EndDate) as my real end date.

default value to a cell of a new row without a macro (Excel)

I would like to insert a default value, to a specific cell in my worksheet,
but this default value should take care of new rows inserted in the worksheet.
I must not use a macro for this.
thanks
Maybe I am wrong, but I can not think of any way, one would be able to that. This is more a typicall database functionality. Without macros you would have to use a function or a format. A function, like values, would not be copied by inserting new rows - only formats would, so this narrows it down.
By the way, I interpreted your question to "default value on instertion of new row", not "default value when writing data in a existing clear row".
So, as a kind of "default value behaviour" you could use user defined cellformats.
i.e. use ;;'x'; as a user format and format your cell or column with it. This won't fill empty cells with 'x' but, whenever you would type in '0' it would change to 'x'.
However, I am very interested, if there is a better solution.
You can add validation to cells which can help you force a number into a particular cell, but as stated in the earlier answer a database is more designed for Default Values.
It seems to me you are trying to create a database in Excel, I wouldn't recommend this as excel is very good at prototyping algorithms however when it comes to structuring tables it can fail very quickly.
Use at least MS Access, namely as it comes with a database. Alternatives are rapid prototyping tool such as Eclipse or Netbeans, or Visual Studio if your budget can stretch that far. Couple the RAD tools with MySql (namely for ease of use and the fact that the community licence is good) and the system should be stable.

Importing Excel into SS2000; Error on Field; DTS

I'm trying to import an excel file in to a SQL Server 2000 database using DTS. This is nothing fancy, just a straight import. Where I work, we do this 1000 times a day. This procedure usually works without an issue but something must have changed in the file.
I'm getting the below error:
Screen shot of Error http://www.understandingguitar.org/wp-content/uploads/2008/12/packageerror-screenshot-20081212.jpg
I've checked to ensure that the column "AssignmentID" is stored as "text" in the excel sheet. I've also tried to change it to general. Exact same error regardless of setting. The field does contain numbers... I appreciate everyone's help on this!
Regards, Frank
Try opening at the excel file and see the column content.
Is any of the row value in that column right-aligned? (generally for numbers)?
I am guessing that such a row could be a problem.
It may be obvious, but is the destination string long enough to hold the string representation of the float? I'm not sure if Excel is rounding what it displays to you, so it may be worth trying with a wider column.
The answer has something to do with the fact that the procedure is expecting text but even if you set the properties (in the format dialog) to "text", excel may not handle the data as text. And hence, SQL Server (or the libraries) won't handle it to text.
When the procedures try to import it, the system feels that it is converting from a number to text and it expect that data maybe lost (even though no data will be lost) and the error is raised.
If figured out that I can get around this error by placing a ' (apostrophe) before each listed number. [I.E. '124321] This force excel to treat the number as text.
Hopefully this will save others the headache I now have from this. :-)
Regards,Frank

Resources