How to get formatted display cell value in excel using closedXML? - excel

I would like to get the displayed value in excel, not the rich text, but the formatted display value.
For example, if the value is "7/1/2015", and this cell is with number format:cell.Style.NumberFormat.Format="d", then in excel this number will be displayed as 1.
I would like to get the "1" by using closedXML but with no success. Below are some value I tried:
cell.Value = "7/1/2015";
cell.RichText.Text = "7/1/2015";
cell.GetString() = "7/1/2015";
cell.GetFormattedString() = "7/1/2015";
cell.GetValue<string>() = "7/1/2015";
Does any one know how to achieve this?
Many thanks!

Have you tried using NumberFormat.Format?
ex. worksheet.Cell(rowCount, 2).Style.NumberFormat.Format = "mm/dd/yyyy";
Let me know if this is whatyou're looking for.

After some searching, I found this: https://github.com/ClosedXML/ClosedXML/issues/270
which indicates that closedXML formattedstring is different from Excel's and there won't be a fix.
So I ended up adding my own custom handler for date time values.

To get the display value for an Excel cell, i used this below RichText property rather than using the Cell.Value property (which gives the actual value of the cell without formatting).
using cXl = ClosedXML.Excel;
string cellValue, col="A";
int row=1;
cXl.IXLWorksheet ws;
cellValue = ws.Cell(row, col)
.RichText
.ToString();

Related

VBA - populating cell value based on another cell's value (code not working)

I've tried to write the below code so depending on the value in cell worksheets "2. Survey" cell f11 then i want "yes" or no value to appear in my current tab cell e59 (current tab - 3. Unit Specification). Im currently getting run time error 9 but don't know why. (probably done something dumb)
Worksheets("3.Unit Specification").Range("e59").Formula = "=IF('2.
Survey'!F11=""Mains"",""Yes"","")"
run time error 9 is my current issue i just want the cell e59 to have the value yes or have no value (this is a drop down box though and the value is an exact match to the list so don't know if that is resulting in the problem).
You need to double up all the quotes. You missed the last pair.
Worksheets("3.Unit Specification").Range("e59").Formula = _
"=IF('2. Survey'!F11=""Mains"",""Yes"","""")"
Edit
'Non-formula approaches
If Worksheets("2. Survey").Range("F11").Value = "Mains" Then
Worksheets("3.Unit Specification").Range("e59").Value = "Yes"
Else
Worksheets("3.Unit Specification").Range("e59").Value = ""
End If
'OR
Worksheets("3.Unit Specification").Range("e59").Value = IIf(Worksheets("2. Survey").Range("F11").Value = "Mains", "Yes", "")

How do I search for numbers within a range that are written in a specific format?

I am trying to write an Excel formula that measures the number of times a number between 1000 and 9999 is written in text using the format 0,000. (This is being used to read old content from our website and measure how many pages do not align with a new style guide.) Here is what I have so far:
=count(search(text(1000,"0,000"),G17))
This formula works if the text in the content is 1,000, but, obviously, not if the text is 1,001.
I don't know how to enter the range in. I assume it should go where the 1000 is, but nothing I try works.
Does anyone know how to do this?
If your text-based number values in column G are between 0 and 999,999 then this should return a count of all text-based numbers that would have a numerical value between 1000 and 9999 if they were actually numbers.
=SUMPRODUCT(COUNTIF(G:G, {"1,*","2,*","3,*","4,*","5,*","6,*","7,*","8,*","9,*"}))
Another approach is that anything between 1,000 and 9,999 is going to have a length of 5.
=SUMPRODUCT(--(LEN(G:G)=5))
If you add the following code to a new "Module" in the VBA Editor you will have access to it as a worksheet function.
I've not tested it all that much but it worked for my example.
Public Function RESearch(SourceText) As Integer
Dim REO As Object: Set REO = CreateObject("VBScript.RegExp")
REO.Pattern = "(\d{1},\d{3})"
REO.Global = True
REO.IgnoreCase = False
REO.MultiLine = True
Dim Matches As Variant
Set Matches = REO.Execute(SourceText)
RESearch = Matches.Count
Set REO = Nothing
End Function
This will add a function "RESearch" to the workbook, and should return the count of all numbers that match the pattern.
Try this:
=COUNTIF(G:G,"?,???")

Excel - How to get expression text rather than the value

I'm having problem with a big set of Excel data. One others had inputted the data like this:
A
10
10:12
11:12:15
My task is to convert it to something like this:
B
Pig
Pig:Koala
Dog:Koala:Bird
I was trying to use substitute:
= SUBSTITUTE(A1, "10", "Pig")
But the problem is, Excel recognizes those value in A column as other data types (number, time...) and the SUBSTITUTE doesn't work on those types.
How could I fix this issue?
Thank you.
This function will return a string that matches what excel is displaying.
Option Explicit
Function ToText(r As Range) As String
If r.Count <> 1 Then
ToText = "#ERR!"
Exit Function
End If
ToText = IIf(r.NumberFormat = "General", CStr(r.Value), Format(r.Value, r.NumberFormat))
End Function
for example, if 10:11:12 is in A1, which excel thinks is a time, and is formatted this way, then =ToText(A1) will return the string 10:11:12, which you can then manipulate as you would any other text
put this into a module on the spreadsheet ( ALT + F11 ) so the function is available to excel
Select column A from first to last record and right click on that,
then change the format by clicking Format cells...
and choose whatever format you want...
like
then use SUBSTITUTE(A1, "10", "Pig") method

JExcel/POI: Creating a cell as type Text

I have a requirement that involves reading values from an excel spreadsheet, and populating a spreadsheet for users to modify and re-upload to our application. One of these cells contains a text string of 5 characters that may be letters, numbers, or a combination of both. Some of these strings contain only numbers, and begin with a zero. Because of this, the cell type is Text; however, when I use Apache POI or JExcel to populate a spreadsheet for the users to modify it is always set as cell type General.
Is there a way using either of these libraries, or some other excel api that I have not seen yet, to specify that a cell have type Text?
My co-worker just found a way to accomplish this. In JExcel, it can be accomplished by using a WritableCellFormat such as:
WritableCellFormat numberAsTextFormat = new WritableCellFormat(NumberFormats.TEXT);
Then, when you are creating your cell to add to a sheet you just pass in the format as normal:
Label l = new Label(0, 0, stringVal, numberAsTextFormat);
If you are using Apache POI, you would create a HSSFCellStyle, and then set it's data format like this:
HSSFCellStyle style = book.createCellStyle();
style.setDataFormat(BuiltInFormats.getBuiltInFormat("text"));
Many times when user enters number in cell which type(formatting) is text(string), spreadsheet software (openoffice or msoffice) changes it's formatting automatically. I am using apache poi and this is the way I wrote my code :
cell = row.getCell();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
// if cell fomratting or cell data is Numeric
// Read numeric value
// suppose user enters 0123456 (which is string), in numeric way it is read as 123456.0
// Now I want ot read string and I got number...Problem?????
//well this is the solution
cell.setCellType(Cell.CELL_TYPE_STRING); // set cell type string, so it will retain original value "0123456"
value = cell.getRichStringCellValue().getString(); // value read is now "0123456"
break;
default:
}

How do I retrieve an Excel cell value in VBA as formatted in the WorkSheet?

I need to keep leading zeros on a list of numbers. The numbers are added like this (in a loop, but this is just an example using the (1, 1):
Set cel = Sheet.worksh.Cells(1, 1)
cel.ColumnWidth = 10
cel.Value = e.Name
cel.NumberFormat = "0000"
Where e.Name is the number, something like "0720". This displays on the worksheet just fine, but if I do something like this:
Msgbox Sheet.worksh.Cells(1, 1).Value
I get "720". I need it to be "0720", I know I could check using Len() and add the zeros that way, but I was wondering if there was a more direct approach with the Range object that would do this for me.
You are confusing a number with its textual representation.
You want the .Text property, not .Value, but then, you might have problems with it.
Use This:
Msgbox Format(Sheet.worksh.Cells(1,1).Value, "0000")
Even better, if you're not sure what the format is:
Msgbox Format(Sheet.worksh.Cells(1,1).Value, Sheet.worksh.Cells(1,1).NumberFormat)
A little late to the party, but you could always try calling up the value and number format separately, like this:
Msgbox Application.WorksheetFunction.Text(Sheet.worksh.Cells(1,
1).Value, Sheet.worksh.Cells(1, 1).NumberFormat)

Resources