I have a column containing quantities. Along with these quantities there is a unit of measure displayed, such as KG, or L. However this isn't hard copied text but rather some custom formatting rule, for example #.##0 "L". What I would like is to extract these UOMs into their own seperate column as text. Is there some way I can access these formatting rules to do this or is there some other method that will get me what I want?
My solution for completeness.
Function getcellformat(ByRef valuecell As Range) As String
getcellformat = Replace(Split(valuecell.NumberFormat, " ")(1), Chr(34), "")
End Function
Thanks to teylyn for pointing out the .NumberFormat
You can use the .NumberFormat property to show the number format applied to a specific cell
Here's a screenshot from the Immediate Window for the Number Format of cell A1 of the current sheet
Related
is there a way to use VBA (or some excel function) to return a custom format that is used?
For example:
in cell A1 I have a value of 100 with custom format [$EUR ]#.##0,00;[Red][$EUR ]-#.##0,00
in cell A2 I have a value of 100 with custom format [$PLN ]#.##0,00;[Red][$PLN ]-#.##0,00
So basically, in A1 I have red colored EUR -100,00 and in A2 I have red colored PLN -100,00
Now let's assume I want do some calculations with only those cells that are formatted as EUR (in example above it means the cell has [$EUR] inside it's custom format.)
Any ideas on how to get that custom format string or anything else that will help me to differentiate those two cells by their custom format?
I tried Excel formula CELL("format";A1) and CELL("format";A2), but they both return ,2-
I've seen many VBA solutions to format a cell, but I haven't found any VBA function that returnes the custom format used (there are ways to return some general stuff like color, is it number etc, but I didn't find any custom format returns).
If you want to read that cells formatting (custom or not) then use:
Range("A1").NumberFormat
If you want a small UDF, that you can call from the worksheet itself, you need a small amount of VBA:
Create a new module, if you don't have one.
Paste into it the following:
Function formatting(rng As Range) As String
Application.Volatile
formatting = rng.Cells(1, 1).NumberFormat
End Function
Note, this will always return the formatting of the first cell within the range.
Then, in any given cell you can call that function- e.g.
=formatting(A1)
From there, you can use normal text manipulation functions such as Left, Mid etc. to locate the colour code or currency symbol - e.g.
=IF(LEFT(formatting(A1),2)="[$",MID(formatting(A1),3,3),"No currency found")
would give the result EUR
I have cells containing a simple concatenation function with D24 as the previous year (e.g. 15) and a custom format (MMM JJ)
CONCATENATE("Apr ",$D$24)
When I am copying and pasting these cells with a VBA then "Apr 15" becomes "15.04.16" and because of the formatting "Apr 16"
Selection.Value = Selection.Value
Was is the reason behind this? Is there another solution than just changing the format to text?
Excel will generally try to convert anything that looks like a date into a real date (serial number where 1 = 1 Jan 1900). One way to avoid that, and remove the formula as you are doing above, would be to pre-format as text. So:
With Selection
.NumberFormat = "#"
.Value = .Text
End With
might do what you want.
There are other ways, but if you don't change the cell format to text, or prefix the entry with a single quote ', any subsequent editing of that cell, even inadvertent selection, raises the risk of converting it to the real date.
That depends on what you want in your cell. Dou you want a string or a date?
If you want a string:
either format as text or
add a ': CONCATENATE("'Apr ",$D$24)
if you want a date:
use the following formula instead of concatenate: =DATE($D$24,4,1)
If you simply Copy Paste it, only the Value is pasted not the formatting (if I remember right)
Try to avoid using Selection instead use Range.
And use Range.Copy and Range.PasteSpecial Paste:=xlPasteFormats so your formatting is pasted with the values.
I am trying to have some vba code to place the value of a formula and text into a specific cell. I want the code to say Leads:(value of formula). Currently my code runs the formula and places the value in the correct box. I just do not know how to add the text with it. The code I have is written below.
ws.Range("$B$1").Formula = "=COUNTIF(E:E,""Lead"")"
A custom Range.NumberFormat property will give you the displayed result while leaving the actual raw value in a numerical form for possible further computation or comparison.
with ws
with .range("B1") '<~~ no need for absolute $ anchors here
.formula = "=COUNTIF(E:E,""Lead"")"
.numberformat = "[=1]L\e\a\d\: 0;L\e\a\d\s\: 0"
end with
end with
Try this:
ws.Range("$B$1").Formula = "=""Leads:("" & COUNTIF(E:E,""Lead"")&"")"""
so that it ends up like the following when applied:
="Leads:(" & COUNTIF(E:E,"Lead")&")"
I have two cells in Excel. one has a string and the other one has a date. in the third cell I want to put the date and the string together. For example:
A1 = "This "
A2 = "03/03/1982"
I want A3 to be:
This 03/03/1982
when I try to put this in the A3 formula: = A1 & A2 it returns some funny numerical value for the date and does not give me the literal date.
Don't know if it's the best way but I'd do this:
=A1 & TEXT(A2,"mm/dd/yyyy")
That should format your date into your desired string.
Edit: That funny number you saw is the number of days between December 31st 1899 and your date. That's how Excel stores dates.
This is the numerical representation of the date. The thing you get when referring to dates from formulas like that.
You'll have to do:
= A1 & TEXT(A2, "mm/dd/yyyy")
The biggest problem here is that the format specifier is locale-dependent. It will not work/produce not what expected if the file is opened with a differently localized Excel.
Now, you could have a user-defined function:
public function AsDisplayed(byval c as range) as string
AsDisplayed = c.Text
end function
and then
= A1 & AsDisplayed(A2)
But then there's a bug (feature?) in Excel because of which the .Text property is suddenly not available during certain stages of the computation cycle, and your formulas display #VALUE instead of what they should.
That is, it's bad either way.
Another approach
=CONCATENATE("Age as of ", TEXT(TODAY(),"dd-mmm-yyyy"))
This will return
Age as of 06-Aug-2013
Thanks for the solution !
It works, but in a french Excel environment, you should apply something like
TEXTE(F2;"jj/mm/aaaa")
to get the date preserved as it is displayed in F2 cell, after concatenation.
Best Regards
You can do it this simple way :
A1 = Mahi
A2 = NULL(blank)
Select A2 Right click on cell --> Format cells --> change to TEXT
Then put the date in A2 (A2 =31/07/1990)
Then concatenate it will work. No need of any formulae.
=CONCATENATE(A1,A2)
mahi31/07/1990
(This works on the empty cells ie.,Before entering the DATE value to cell you need to make it as TEXT).
I found that for this situation, the simplest solution is to define a Custom number format for the cell containing the date. The format in this case should be:
"This:" mm/dd/yyyy
To set this format:
Right click on the cell
Select Format Cell
Select Number tab (should be displayed by default)
Pick Custom from the Category list
Specify the format in the "Type" field
Press OK
Note: If you really want the preceding text to be picked from a cell, this solution will not work as described.
In a cell in Excel sheet I have a Date value like:
01/01/2010 14:30:00
I want to convert that Date to Text and also want the Text to look exactly like Date. So a Date value of 01/01/2010 14:30:00 should look like 01/01/2010 14:30:00 but internally it should be Text.
How can I do that in Excel?
=TEXT(A1,"DD/MM/YYYY hh:mm:ss")
(24 hour time)
=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")
(standard time)
Here is a VBA approach:
Sub change()
toText Sheets(1).Range("A1:F20")
End Sub
Sub toText(target As Range)
Dim cell As Range
For Each cell In target
cell.Value = cell.Text
cell.NumberFormat = "#"
Next cell
End Sub
If you are looking for a solution without programming, the Question should be moved to SuperUser.
Here's another option. Use Excel's built in 'Text to Columns' wizard. It's found under the Data tab in Excel 2007.
If you have one column selected, the defaults for file type and delimiters should work, then it prompts you to change the data format of the column. Choosing text forces it to text format, to make sure that it's not stored as a date.
In some contexts using a ' character beforehand will work, but if you save to CSV and load again this is impossible.
'01/01/2010 14:30:00
Couldnt get the TEXT() formula to work
Easiest solution was to copy paste into Notepad and back into Excel with the column set to Text before pasting back
Or you can do the same with a formula like this
=DAY(A2)&"/"&MONTH(A2)&"/"&YEAR(A2)& " "&HOUR(B2)&":"&MINUTE(B2)&":"&SECOND(B2)
I have no idea about the year of publication of the question; it might be old now. So, I expect my answer to be more of a reference for future similar questions after my post.
I don't know if anybody out there has already given an answer similar to the one I am about to give, which might result -I think- being the simplest, most direct and most effective: If someone has already given it, I apologize, but I haven't seen it. Here, my answer using CStr instead of TEXT:
Asuming cell A1 contains a date, and using VBA code:
Dim strDate As String
'Convert to string the value contained in A1 (a date)
strDate = CStr([A1].Value)
You can, thereafter, manipulate it as any ordinary string using string functions (MID, LEFT, RIGHT, LEN, CONCATENATE (&), etc.)
If you are not using programming then do the following
(1) select the column
(2) right click and select Format Cells
(3) Select "Custom"
(4) Just Under "Type:" type dd/mm/yyyy hh:mm:ss
In Excel 2010, marg's answer only worked for some of the data I had in my spreadsheet (it was imported). The following solution worked on all data.
Sub change()
toText Selection
End Sub
Sub toText(target As range)
Dim cell As range
Dim txt As String
For Each cell In target
txt = cell.text
cell.NumberFormat = "#"
cell.Value2 = txt
Next cell
End Sub
As Text is localized it will break when trying you try to share your files over diffrent cultures. ÅÅÅÅ-MM-DD might work perfectly in sweden, is US, Germany or israel it will turn to shit.
The reasonable solution would be that english was accepted everywhere, but it's not.
Basically DON'T EVER use text as intended to format dates.
Here is how to create the date in ISO format. TEXT is used to ensure leading
=YEAR(A1)&"-"&TEXT(MONTH(A1);"00")&"-"&TEXT(DAY(A1);"00")
If you want it backwards, sideways or whatever, just change it.
https://www.reddit.com/r/ISO8601/comments/enhlp6/logic_of_the_different_date_time_systems_with/
The selected answer did not work for me as Excel was still not converting the text to date. Here is my solution.
Say that in the first column, A, you have data of the type 2016/03/25 21:20:00 but is stored as text. Then in column B write =DATEVALUE(A1) and in column C write =TIMEVALUE(A1).
Then in column D do =B1+C1 to add the numerical formats of the date and time.
Finally, copy the values from D into column E by right clicking in column E and select Paste Special -> Paste as Values.
Highlight the numerical values in column E and change the data type to date - I prefer using a custom date of the form YYYY-MM-DD HH:MM:SS.