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
Related
I have a huge issue with trying to copy values onto my database
lets say i have 2 columns, A is integer and B is date
I'm trying to build column C with the expression
= "(" &A2& ",'" & B2& "'),"
Expected output (1000, '2020-01-29'),
Obtained output (1000, '43859'), (?????)
Is there a function that allows me to do a formula but keep the date as it is?
Change your function to
= "(" &A2& ",'" &TEXT(B2,"DD/MM/YY")& "'),"
.
The =TEXT() function takes the value you want to format and the format as arguments.
=TEXT(Value you want to format, "Format code you want to apply")
Click here for the TEXT function manual
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();
I have two unordered sets of data here:
blah blah:2020:50::7.1:45
movie blah:blahbah, The:1914:54:
I want to extract all the data to the left of the year (aka, 1915 and 1914).
What excel formula would I use for this?
I tried this formula
=IF(ISNUMBER(SEARCH(":",A1)),MID(A1,SEARCH(":",A1),300),A1)
these were the results below:
: blahblah, The:1914:54::7
:1915:50::7.1:45:
This is because there is a colon in the movie title.
The results I need consistently are:
:1914:54::7.9:17::
:1915:50::7.1:45::
Can someone help with this?
You can use Regular Expressions, make sure you include a reference for it in your VBA editor. The following UDF will do the job.
Function ExtractNumber(cell As Range) As String
ExtractNumber = ""
Dim rex As New RegExp
rex.Pattern = "(:\d{4}:\d{2}::\d\.\d:\d{2}::\d:\d:\d:\d:\d:\d:\d)"
rex.Global = True
Dim mtch As Object, sbmtch As Object
For Each mtch In rex.Execute(cell.Value)
ExtractNumber = ExtractNumber & mtch.SubMatches(0)
Next mtch
End Function
Without VBA:
In reality you don't want to find the : You want to find either :1 or :2 since the year will either start with 1 or 2This formula should do it:
=MID(A1,MIN(IFERROR(FIND(":1",A1,1),9999),IFERROR(FIND(":2",A1),9999)),9999)
Look for a four digit string, in a certain range, bounded by colons.
For example:
=MID(A1,MIN(FIND(":" &ROW(INDIRECT("1900:2100"))&":",A1 &":" &ROW(INDIRECT("1900:2100"))&":")),99)
entered as an array formula by holding down ctrl-shift while hitting Enter would ensure years in the range 1900 to 2100. Change those values as appropriate for your data. The 99 at the end represents the longest possible string. Again, that can be increased as required.
You can use the same approach to return just the left hand part, up to the colon preceding the year:
=LEFT(A1,-1+MIN(FIND(":" &ROW(INDIRECT("1900:2100"))&":",A1 &":" &ROW(INDIRECT("1900:2100"))&":")))
Here is a screen shot, showing the original data in B1:B2, with the results of the first part in B4:B5, and the formula for B4 showing in the formula bar.
The results for the 2nd part are in B7:B9
I have an issue that I've not been able to figure out even with many of the ideas presented in other posts. My data comes in Excel and here are examples of each manner that any given cell might have the data:
4days 4hrs 41mins 29seconds
23hrs 43mins 4seconds
2hrs 2mins
52mins 16seconds
The end result would be to calculate the total minutes while allowing seconds to be ignored, so that the previous values would end up as follows:
6041
52
1423
122
Would anyone have an idea how to go about that?
Thanks for the assistance!
Bit tedious (and assumes units are always plural - also produces results in different order to example) but, with formulae only, if your data is in column A, in B1 and copied down:
="="&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"days","*1440+"),"hrs","*60+"),"mins","*1+"),"seconds","*0")," ","")&0
then Copy B and Paste Special values into C and apply Text to Columns to C with Tab as the delimiter.
This array formula** should also work:
=SUM(IFERROR(0+MID(REPT(" ",31)&SUBSTITUTE(A1&"dayhrminsecond"," ",REPT(" ",31)),FIND({"day","hr","min","second"},REPT(" ",31)&SUBSTITUTE(A1&"dayhrminsecond"," ",REPT(" ",31)))-31,31),0)*{1440,60,1,0})
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
The easiest option is probably VBA with a regular expression. You can then easily find each of the fields, and do the maths.
If you want to stick to "pure" Excel, then it seems to only option is to use SEARCH or FIND to find the position of each of the "days", "hrs", "mins" in the text (you may have to check if they're always plural). Then use MID with the position found above to extract the different components. See http://office.microsoft.com/en-gb/excel-help/split-text-among-columns-by-using-functions-HA010102341.aspx for similar examples.
But there's quite a bit of work to handle the cases where some components are missing, so either you'll use quite a few cells, so you'll get a very complex formula...
Here is a User Defined Function, written in VBA, which takes your string as the argument and returns the number of minutes. Only the first characters of the time interval names are checked (e.g. d, h, m) as this seems to provide sufficient discrimination.
To enter this User Defined Function (UDF), opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.
To use this User Defined Function (UDF), enter a formula like
=SumMinutes(A1)
in some cell.
Option Explicit
Function SumMinutes(S As String) As Long
Dim RE As Object, MC As Object
Dim lMins As Long
Dim I As Long
Set RE = CreateObject("vbscript.regexp")
With RE
.Pattern = "(\d+)(?=\s*d)|(\d+)(?=\s*h)|(\d+)(?=\s*m)"
.Global = True
.ignorecase = True
If .test(S) = True Then
Set MC = .Execute(S)
For I = 0 To MC.Count - 1
With MC(I)
lMins = lMins + _
.submatches(0) * 1440 + _
.submatches(1) * 60 + _
.submatches(2)
End With
Next I
End If
End With
SumMinutes = lMins
End Function
I have Excel data that has parentheses around negative values, but Excel does not see them as negative values, it seems to take them as text. E.g. trying to sum cells that have ($25.00) and $50.00 gives #VALUE! error.
The data is imported from a csv file.
It's not a formatting issue, changing negative value formatting does not change anything. So, how do I convert these values to actual negative numbers?
Thanks.
Here is some sample data in case someone wants it. So the values in parentheses are supposed to be negative numbers but seem to be text at the moment.
($25.00)
$50.00
($35.00)
($15.00)
Try replace $ with nothing, ) with nothing and ( with -.
Type 1 into an unused cell. Copy it. Select the numbers that are stored as text and select Home - Clipboard - Paste - Paste Special - Multiply. Delete the 1.
Complete syntax for converting numbers like ($6,437.55) to money data type:
CASE WHEN IsNumeric(Replace(Replace(Replace([INPUT-COLUMN-NAME], ',', ''),')',''),'(','-')) = 1 Then Cast(Replace(Replace(Replace([INPUT-COLUMN-NAME], ',', ''),')',''),'(','-') AS Money) ELSE 0 END As [OUTPUT-COLUMN-NAME]
This can be used directly in SQL statement like
Select ColumnA, CASE WHEN as above, ColumnC
From Table.. join etc.
You can also define a function like (which comes with a performance penalty though) and use function in select statement.
CREATE FUNCTION dbo.ConvertStringToMoneyType(#input_Number varchar(50))
RETURNS MONEY
AS
BEGIN
Declare #Output_Number money = 0
Set #Output_Number = CASE WHEN IsNumeric(Replace(Replace(Replace(#input_Number, ',', ''),')',''),'(','-')) = 1 Then
Cast(Replace(Replace(Replace(#input_Number, ',', ''),')',''),'(','-') AS Money) ELSE 0 END
RETURN #Output_Number
END
GO