Sentence
Is
True
From:
I have converted the continuous beam analysis spreadsheet (last presented here) to Python code, linked to Excel VBA User Defined Functions (UDFs) with ExcelPython. To run the Python version you will need an installed copy of Python, including Numpy and Scipy. Everything else is included in the download file, including all the necessary ExcelPython files.
The spreadsheet (including full open source code) can be downloaded from Conbeampy.zip. To run the spreadsheet just unzip everything to any directory.
The file ConBeampy.xlsb includes all the new functions with one example of each.
ConBeampy-check.xlsb also includes checks of the Conbeam function results against results from Strand7, for 15 different configurations of beams with different span arrangements and support conditions. The screenshot below shows there was close agreement for all 15 cases.
More examples of using each of the included functions will be provided in future posts.
This post makes use of a spreadsheet for reading data from text files, including comma separated value (csv) files. The latest version includes a routine for saving the imported data in csv format. It can be downloaded (including full open-source code) from Text-in2.zip.
Excel allows csv files to be opened and saved directly from the File menu, without going through the File-Import process. Unfortunately the data is treated in the same way as text entered into a spreadsheet cell formatted as General, so any text that looks like a number will be stored as a numerical value, and displayed in the format that Excel deems appropriate. If the file is saved with the original name the data will be changed, which in some cases result in dates being changed, or numeric codes being converted into dates.
This is illustrated with the short csv file below, consisting of four columns:
– Numbers
– Text that looks like numbers
– Dates
– Text that looks like dates
The screenshot below shows the results of opening this file in Excel with default language set to Australian English, then saving it as csv, without making any changes.
It can be seen that changes have occurred in all the columns:
Changing the default language to US English results in significantly different results for the dates, and strings interpreted as dates:
CSV files can now be opened and saved without any unintended changes using the Spreadsheet Text-in2 (download link at the top of this post), which has three new features:
If each column of a csv file is imported as text (format code = @), the data is all imported with no changes:
For simple applications numbers imported as text will be treated as numbers by Excel, but for many purposes they must be converted to numbers for everything to work as expected; for instance the Sum function treats all text as having value zer0. Any column containing numeric data can be imported as numbers by entering the appropriate format code; for example enter 0.00 to import as numbers and display to 2 decimal points. See the Text-in2 NumFormats sheet for more examples of number format codes.
The dates are imported as text, but can be converted to Excel date values using the Text2Date function.
When the imported data is saved to a csv file all text remains exactly as in the original file:
Text2Date optionally allows the date order to be specified, with the System local default being used when not specified. Date order options are:
0: mm/dd/yyyy (default for US English)
1: dd/mm/yyyy (default for non-US English)
2: yyyy/mm/dd
The function also has an optional Separator argument, which should be specified for any separator other than /.
The screenshot below illustrates use of Text2Date with DateOrder set to zero. Note that in this case the function returns an error message (“Not a valid date string”) if the first number is greater than twelve, or the second position has a month name, rather than a number:
Also note that in this case the format for column 1 was set to 0.00 (rather than @), so this column has been imported as numbers, rather than text. The fractions in rows 32 and 36 have been converted to decimal values, but the text in column 2 remains unchanged.
More details of the Text-in2 spreadsheet (plus other text functions) can be found at:
Daily Download 32: Text functions
When working with imported data, or data from an opened csv file, it is often important to know the data type that has been used to store the data. This may not be obvious, for instance in the previous post we saw that a date imported into a cell formatted as text displays as text, but is converted into a date value when used in a simple formula. However if the cell is included as part of a range it will be treated as text, with a value of zero, even for a single cell range. For instance, if we format cell A1 as text and enter 8-jun the cell will display as entered (including the lower case j), but in another cell:
If we now format the cell as General, and re-enter 8-jun, the cell will display 8-Jun and =Sum(A1) will display 42163 (if it is formatted as a number), or 8-Jun (if it is formatted as a date).
The User Defined Function (UDF) below will return information about the data type for the values in a selected single column range:
Function GetDType(DRange As Range) As Variant
Dim DTypeA() As Variant, DVal As Variant, NumRows As Long, i As Long
DVal = DRange.Value
NumRows = UBound(DVal)
ReDim DTypeA(1 To NumRows, 1 To 2)
For i = 1 To NumRows
DTypeA(i, 1) = DVal(i, 1)
DTypeA(i, 2) = TypeName(DVal(i, 1))
Next i
GetDType = DTypeA
End Function
The function must be entered as an array function (using Ctrl-Shift-Enter), and returns two columns. The first shows the actual value stored by Excel (as opposed to the value displayed), and the second the data-type:
To work with number formats in VBA (which we need to use to open csv files without changing the data) we need to use Excel’s format codes, but there is no built-in way to show the correct format code for any selected cell format. An excellent and (almost) comprehensive guide to Excel number formats can be found at: A comprehensive guide to Number Formats in Excel, but for a quick way to find the correct number format, the UDF below will return the format code for any selected cell:
Function GetNumformat(Target As Range, Optional UseLocal As Boolean = True)
If UseLocal Then
GetNumformat = Target(1, 1).NumberFormatLocal
Else
GetNumformat = Target(1, 1).NumberFormat
End If
End Function
Examples of some format codes are shown below:
Each format comes in a standard and local version; the local version is returned by default.
Full details of how to generate these sometimes lengthy codes are given in the article linked above, other than for the start of the codes associated with Long Date and Time ([$-F800] and [$-F400]). A search on these codes suggests that the answer to the question of what they mean is, no-one knows (my lord), but a bit of experimentation shows that if the code between the square brackets is applied as a custom format (including the brackets) a date displays in date format on the spreadsheet, but in date number format in the edit bar, whereas applying the full code the date format displays in both the worksheet and the edit bar.
These functions plus routines to allow data from csv files to be quickly imported and saved without corruption will be available for download in the next post in this series.
Following a discussion at Eng-Tips, I have been having a closer look at how Excel handles the entry and display of dates. The Eng-Tips discussion concerned changes to data when csv files are opened in Excel. I will deal with that (together with a way to open and save csv files in Excel without changing anything) in a later posts, but for today I want to look at the basics of how Excel stores and formats dates.
Excel stores dates as a day number, starting from 1st January 1900. If you enter a sequence of numbers: 1, 2, 3, … in different cells, then format the cells as dates, it will display as below:
Note that day 60 displays as 29th Feb 1900, which does not actually exist, since century years not divisible by 400 are not leap years. The extra day was introduced to maintain compatibility with the earlier spreadsheet Lotus 123, who had probably done it by mistake.
There is also an option (under Options-Advanced-When Calculating This Workbook) to set day 1 to 1st January 1904, which is the standard for Apple Mac versions of Excel. Everything that follows uses the Windows default.
In addition to the Apple/Windows differences the date formats must also cope with different conventions for writing dates, especially when the month is shown as a number, and must deal with text that may or may not be a date.
The screenshot below illustrates different behaviour depending on format settings, and the first character of the entered text. Column A was pre-formatted to “Text” (to display the values as entered). The remaining columns were left in the default format (“General”).
In Columns A and C the text was entered as shown in Column A. In Columns D and G the same text was entered, except preceded by a space for Column D and an ‘ for Column G.
Columns B, D, F, and H contain a formula subtracting the value in Cell A3 (123) from the value in the adjacent cell.
The default Windows date display for my location (Australian English) was used, i.e. day-month-year.
Note that:
The screenshot below shows details of the format automatically applied to the dates in Column C entered as day-month-year. This is the standard short date format, and the * indicates that the displayed date will be re-formatted, depending on the language setting.
The note above says that the starred date formats will change in response to regional date and time settings, but the default setting (at least in Windows 8) is that the regional date and time settings are tied to the selected language, so to change the date display the Windows language should be changed, as shown below.
To change to United States English in Windows 8.1 I had first to add US English to the list in the Control Panel – Language dialog, then move it into top position:
After changing to US English the display is immediately updated:
After changing the language re-entering the same values in Column C will result in a new date value:
Note that the date values in Column D now match those in Columns B and H. The dates in Rows 4 and 7 are all the same, but in Columns A and G it is displayed as month-day (i.e. Jan-23rd), whereas in Column C it is displayed as month-year (i.e. Jan-2015).
Returning Australian English to top position in the Control Panel table reverses the process, with the dates in Column C maintaining their date value, but changing format in rows 6 and 9, and text in Columns A and G remaining unchanged, but all the associated date values change:
In summary, when everyone using a worksheet with dates uses the same language settings, and the same date format, the automatic formatting features of Excel should present no problem, although it is still necessary to take care when entering dates in day-month (or month-day) format that the correct year is entered.
For situations where worksheets may be shared between countries with different formats however great care should be taken, especially when calculations involving date differences are involved. Further recommendations can be found in an excellent article at Excel Semi-Pro Regional Date Formats in Excel.