compile error on format date - excel

I am trying to express the following in vba:
Format(Sheet2!R[2]C[7], "Short Date")
But I keep getting a compile error on the [2], saying: Expected: List separator or )
I'm trying to refer to that cell in that format because it is part of a larger formula to be auto-filled. Is there perhaps something that I'm missing?
so in the vba, i'll have something like:
Worksheets("Sheet1").Select
Range("A3").Select
Selection.AutoFill Destination:=Range("A3:A" & rowCount)
But cell A3 isn't filled by vba, it's put in manually in the excel doc as a function like:
=Sheet2!B3&" "&Sheet2!A3&" "&Sheet2!C3&"_"&Sheet2!D3&"_"&Sheet2!E3
My issue is that the second value in the function "&Sheet2!A3&" is actually a date and when it's written comes out like 41331 instead of a typical "dd/mm/yyyy" format. I was hoping to format this value into that human readable format and have it embedded in the function but still retain the auto-fill functionality.

Worksheets("Sheet2").Range("A1:D10") will give you a range. refer to http://msdn.microsoft.com/en-us/library/office/ff836512.aspx

The VBA can change the cell contents, but then you will lose the formula. Formatting the cell will not help in any event as it contains a string concatenated from many values that cannot be formatted as a date.
Use the Excel Text(Sheet2!A3,"dd/mm/yyyy") function shown in the comment by Sam Ward to format the original data in the formula.

Related

Change "XX:XX:00" to "00:XX:XX"

So I have this data directly copy/paste from iTunes :
Excel have "XX:XX:00" format which is "hh:mm:ss" but as you can imagine, it is more like "mm:ss:00". Musics are not hours long !
As it's not a direct cell format problem, I couldn't find easily the answer on internet. I need sometihng to put the 2 "00" at the start (in order to have "00:mm:ss")
EDIT : It was a cell format comprehension problem.
You can just divide the cell value to 60 like so
and then choose custom format for that cell like this
Format the cell with the time (C1) as "General". If it retains its value, like 11:42, then convert it with =TimeValue(C1) and format the cell as Custom mm:ss
If it changes to something like 0.4875 then convert it with =C1/60 and format the result as Custom mm:ss
This formula should work:
="00:" & MID(TEXT(A1,"h:mm:ss"),SEARCH(":",TEXT(A1,"h:mm:ss"))+1,3) & RIGHT(TEXT(A1,"h:mm:ss"),2)
The important element is to convert the time into a text string.
Change A1 to the first cell of Duration (Duree) & copy the formula downward
Then, you can copy the result and paste it as values
Edit: you can also use just the right function:
="00:"&RIGHT(TEXT(A1,"h:mm:ss"),LEN(TEXT(A1,"h:mm:ss"))-SEARCH(":",TEXT(A1,"h:mm:ss")))

Excel VBA - Convert a string Formula to an Excel Calculation in a range of cells

I'm just wondering if this is possible to do without a loop - In my excel sheet, in, say, Range("A1:C10") I have text concatenation formulas that, once concatenated, create real Excel functions.
As a stupid example, suppose I had the following in cell A1:
A1: ="=Sum(D"&C2&":E"&C3&")"
Now, I know in VBA I can do something along the following for any one specific cell:
Range("A1").Formula = Range("A1").Text
And it will convert my text formula into an Excel formula and evaluate it.
Now, what I'm curious about is, whether there a way to say, for example:
Range("A1:C10").Formula = Range("A1:C10").Text
Without looping through each cell individually?
Also, I can't use INDIRECT() as, unfortunately, my formulas refer to closed workbooks :/
Any ideas??
Range.Text contains the string representation of the cell's value. The actual calculated value (which I suspect is what you're after) is accessed using Range.Value - try this:
Range("A1:C10").Formula = Range("A1:C10").Value
Not sure if this is what you are trying to do, but if for example you use:
Range("A1:C10").Formula = "=Sum(D1:E1)"
then the relative references will be auto adjusted:
A1: =Sum(D1:E1)
A2: =Sum(D2:E2)
B1: =Sum(E1:F1)
... etc.

Date value changes when copied

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.

Set format of date, concatenated with string

I would like to get the following string as a date formatted:
="Status: "& TODAY()
However, I get:
Status: 42418
I would like to get:
Status: 18.02.2016
I already tried to change the format to Date or use the Datevalue function.
Any suggestions, how to get the current date correctly formatted?
You can format the date manualy with DAY(),MONTH() and YEAR() function.
="Status: "& DAY(TODAY())&"."& MONTH(TODAY())&"."&YEAR(TODAY())
Or you can use the TEXT() function to do the formatting.
="Status: " & TEXT(TODAY();"DD.MM.YYYY")
A better way to do this is just change the number format.
Change your cell formula back to =today()
Select the Format menu, then Number, More Formats, and finallay More date and time formats
Then set the custom format as followed:
The benefit of this is that the value of your cell is still a date, not string
EDIT
This answer primarily illustrate separation of presentation and data.
Think of currency. 2 is data, and US$ 2.00 is presentation of the data. When you compute the value, you just want to put the number 2 instead of a formula like = "US$" & data & ".00"
This makes the spreadsheet more robust to any future change where you want to reference your computed values in other cells.
Another example would be win-loss computation. It's better to output the value TRUE/FALSE or 1,0,-1 and then have a custom format to convert the value to text. (In this case, the format rule is "WIN";"LOSS";"DRAW")

Dates Formatting in Excel

I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy
But, when I try to do formulas based on the dates, something is clearly amiss.
For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.
For each record I tried a formula to parse it apart.=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
That did not help.
I also selected the column and switched it from general to date format
I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run =year(right(A1,4)) I get 1903 instead of 2013. When I run =date(right(A1,4),mid(A1,3,2),left(A1,2)) the formula returns 2/10/3192
It's very simple why your formulas doesn't work corectly. When yor're using somehting like this: RIGHT(A2,4), your value from A2 translates to 41579 for 1/11/2013 (Excel stores all dates as integers and all times as decimal fractions. You can read more here). Next formula should work well:
=DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy"),4,2),LEFT(TEXT(A2,"dd/mm/yyyy"),2))
Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column):
=A2*1
and drag it down. Then copy values from this temp column and paste them using "Paste special->Values" in colunm A (where should be date format as well)
Or you can use this simple macro:
Sub test()
With Range("A2:A100")
.NumberFormat = "dd/mm/yyyy"
.Value = .Value
End With
End Sub

Resources