ternary operator in vba excel throws error - excel

I'm trying to add this line of code in vba excel for custom function
(days > 0 ? days + ' day' + (days > 1 ? 's' : '') + ' ' : '') + hours+':'+minutes+':'+Math.round(seconds)
This is my first function in vba excel. Its showing the statement in red color. Does excel 2013 support this ?

Something like this should work...
You may need to use CStr(hours) etc depending on how you've Dim'd the variables
If Days > 0 Then
var = Days & Iff(Days > 1, " days ", " day ") & hours & ":" & minutes & ":" & Round(seconds)
End If
EDIT:
To reflect altered question...
Iff(Days > 0, Days & Iff(Days > 1, " days : ", " day : "), "") & hours & ":" & minutes & ":" & Round(seconds)

Related

Remove comma from concatenated string for last cell in Excel

I have a table in Excel that has 10 columns. For each row there are different numbers of cells populated e.g.
My concatenated string is as follows for HTML emails
(" + Machine1 + ", " + Machine2 + ", " + Machine3 + ", " + Machine4 + ", " + Machine5 + ", " + Machine6 + ")
However I want the comma to not show after the last cell in the row (whether that be column 3,4,5,6).
This is what I get
(TEST1, TEST2, TEST3 , TEST4, TEST5, , )
I want to remove the two commas at the end. Hope this makes sense!
If you have Office 365 Excel then use TEXTJOIN:
="(" & TEXTJOIN(", ",TRUE,A2:F2) & ")"
If not then you will need to use IF for each return and Mid:
=MID("(" & IF(A2 <> "",", " & A2,"") & IF(A2 <> "",", " & B2,"") & IF(C2 <> "",", " & C2,"") & IF(D2 <> "",", " & D2,"") & IF(E2 <> "",", " & E2,"") & IF(F2 <> "",", " & F2,""),3,999)
Alternative without IF
Just in addition to Scott's valid solution, I demonstrate an approach via the REPT function. Applied on a cell containing a string, it "repeats" its content & comma once (indicated by COUNTA() equalling 1), whereas an empty cell results in a zero repetition which allows to omit not only the cell content but the comma, too:
="(" & SUBSTITUTE(REPT(A1&",",COUNTA(A1))&REPT(B1&",",COUNTA(B1))&REPT(C1&",",COUNTA(C1))&REPT(D1&",",COUNTA(D1))&REPT(E1&",",COUNTA(E1))&REPT(F1&",",COUNTA(F1))&"$",",$",")")
A simple SUBSTITUTE removes the last comma, wherever it occurs before the closing bracket ")".

Getting VBA to display a formula with Subtracting

I asked a question yesterday on how to use VBA to display formulas within cells, which I now can use, except for my attempt at a subtraction.
I have my code here, is anyone able to point me in the right direction to get this to work.
With Worksheets(c10)
.Cells(NewEngineRowNumber + 3, 2).Formula = "=Sum((" & .Range(.Cells(NewEngineRowNumber + 6, 2)).Address(0, 0) & ") - (" & .Range(.Cells(NewEngineRowNumber + 2, 2)).Address(0, 0) & "))"
End With
I belived this to be the most likly method after a few different attempt to make it work.
To produce something like in Cells(NewEngineRowNumber+3,2) in the formula bar to display =Cells(BX) - Cells(BY), where X = NewEngineRowNumber +6 and Y = NewEngineRowNumber +2
Any help in helping me understand how to edit this to make it work would be much appreciated.
Thanks
Don't try doing all in one code line. Do separating it in multiple code lines is better for debugging:
With Worksheets(c10)
sAddress1 = .Cells(NewEngineRowNumber + 6, 2).Address(0, 0)
sAddress2 = .Cells(NewEngineRowNumber + 2, 2).Address(0, 0)
sFormula = "=Sum(" & sAddress1 & "-" & sAddress2 & ")"
.Cells(NewEngineRowNumber + 3, 2).Formula = sFormula
End With
But why are you thinking the SUM is necessary at all?
sFormula = "=" & sAddress1 & "-" & sAddress2
should also work.

Excel improving date diff function

I've defined the following formula in Excel
=DATEDIF(B595;TODAY();"y") & " years, " & DATEDIF(B595;TODAY();"ym") & " months, " & DATEDIF(B595;TODAY();"md") & " days"
But it shows a result like:
0 years, 0 months, 20 days
I would prefer to see:
20 days
Can I use a VBA function to get a nicer result? Or a formula?
Just add If statements to the formula that will remove the component if DateDif evaluates to zero:
=IF(DATEDIF(B595;TODAY();"y")=0;"";DATEDIF(B595;TODAY();"y") & " years, ") & _
IF(DATEDIF(B595;TODAY();"ym")=0;"";DATEDIF(B595;TODAY();"ym") & " months, ") & _
IF(DATEDIF(B595;TODAY();"md")=0;"";DATEDIF(B595;TODAY();"md") & " days, ")
I broke the formula into three lines so its readable here, but it should be inputted as one line

Excel Sequential Numbering order based on hierarchy position of data

I need to add sequential numbering order in excel based on the list position of data within excel. I have tried many things to get this right but surely there must be an easy solution.
As you can see my data is an ordered list of text where you can see the hierarchy in the offset of data down the rows. I am trying to automatically create the yellow picture based on the data from the left side.
The only solution I have come up with is to add columns in front of each column, filter the records based on non-blanks then work from left to right manually adding the sequence, but even this is too time intensive.
Ill be working on a macro next but thought there may be an easier solution using a formula. - will update if found.
This is probably not the most efficient solution, but it works:
Formula in G2 (fill the rest of the area with it):
=IF(ISBLANK(A2),IF(ISBLANK(B2),G1,G1+1),0)
Formula in L2 (fill the rest of the area with it):
=IF(G2>0,K2&"."&G2,"")
Formula in Q2 (fill the rest of the area with it):
=IF(ISBLANK(B2),"",MID(L2,2,LEN(L2))&" - "&B2)
You can easily use it for any depth by increasing the areas size, but you need an empty top row and empty column on the left of each area to make it work.
You could probably skip L:O area by combining the last two formulas.
Result:
Formulas: (blue are constants, orange are for the first hierarchy level, green are dragged in the box)
Sub Sequential_Numbering()
Dim tmpRange As Range, tmpArray(1 To 4) As Integer, x As Integer
Set tmpRange = Range("A1")
tmpArray(1) = 1
tmpRange.Value = tmpArray(1) & " - " & tmpRange.Value
For x = 2 To 13
Set tmpRange = Rows(x).Find(What:="*")
Select Case tmpRange.Column
Case 1
tmpArray(1) = tmpArray(1) + 1
tmpArray(2) = 0
tmpArray(3) = 0
tmpArray(4) = 0
tmpRange.Value = tmpArray(1) & " - " & tmpRange.Value
Case 2
tmpArray(2) = tmpArray(2) + 1
tmpArray(3) = 0
tmpArray(4) = 0
tmpRange.Value = tmpArray(1) & "." & tmpArray(2) & " - " & tmpRange.Value
Case 3
tmpArray(3) = tmpArray(3) + 1
tmpArray(4) = 0
tmpRange.Value = tmpArray(1) & "." & tmpArray(2) & "." & tmpArray(3) & " - " & tmpRange.Value
Case 4
tmpArray(4) = tmpArray(4) + 1
tmpRange.Value = tmpArray(1) & "." & tmpArray(2) & "." & tmpArray(3) & "." & tmpArray(4) & " - " & tmpRange.Value
End Select
Next x
End Sub

Calculate average from dates in text

I'm using this formulas:
=DATEDIF(B9,S9,"d") & " Days " & TEXT(S9-B9, "h:m") & " hrs:min"
=DATEDIF(B10,S10,"d") & " Days " & TEXT(S10-B10, "h:m") & " hrs:min"
etc..
And now i need to have a formula that calculates the average of those dates. The problem is that they are in text and excel cannot calculate average.. Would appreciate any input. Thanks
Your formula isn't a reliable method for calculating days and hours between two dates. Consider where B9 is 1st Jan 2013 at 22:00 and S9 is the next day 2nd Jan at 06:00 - there are only 8 hours between those two "timestamps" but your formula will give the result
1 Days 8:00 hrs:min
better to use this version
=INT(S9-B9) & " Days " & TEXT(S9-B9, "h:m") & " hrs:min"
That will give correct results in all cases
For the average you can use a formula like this
=INT(AVERAGE(S9:S18)-AVERAGE(B9:B18)) & " Days " & TEXT(AVERAGE(S9:S18)-AVERAGE(B9:B18), "h:m") & " hrs:min"
where you have data in rows 9 to 18
Consider the following:
Formulas:
C2 = B2-A2
(same for rows 2 through 6)
C7 = AVERAGE(C2:C6)
D2 = INT(C2) & " Days " & TEXT(C2, "h:mm") & " hrs:min"
(same for rows 2 through 7)

Resources