Shortening the IF And Formula using Date Function - excel

Below is my IF AND formula using dates.
=if(and(C4>= & DATE(2016,01,01),C4<= &
DATE(2016,01,31)),"Jan",if(and(C4>= & DATE(2016,02,01),C4<= &
DATE(2016,02,29)),"Feb",if(and(C4>= & DATE(2016,03,01),C4<= &
DATE(2016,03,31)),"Mar",if(and(C4>= & DATE(2016,04,01),C4<= &
DATE(2016,04,30)),"Apr",if(and(C4>= & DATE(2016,05,01),C4<= &
DATE(2016,05,31)),"May",if(and(C4>= & DATE(2016,06,01),C4<= &
DATE(2016,06,31)),"Jun",if(and(C4>= & DATE(2016,07,01),C4<= &
DATE(2016,07,31)),"Jul",if(and(C4>= & DATE(2016,08,01),C4<= &
DATE(2016,08,31)),"Aug",if(and(C4>= & DATE(2016,09,01),C4<= &
DATE(2016,09,30)),"Sept",if(and(C4>= & DATE(2016,10,01),C4<= &
DATE(2016,10,31)),"Oct",if(and(C4>= & DATE(2016,11,01),C4<= &
DATE(2016,11,30)),"Nov",if(and(C4>= & DATE(2016,12,01),C4<= &
DATE(2016,12,31)),"Dec",0))))))
Please help me in shortening this formula.
Thanks.
Salman Khan

I hope, Below formula will resolve your problem
= TEXT(c4,"mmm")

Related

Half the code in my script is giving an error

For i = 2 To 50
ws.Range("K" & i).FormulaLocal = "=if(countif($C$2:C" & i & ";C" & i & ")=1;row();"")"
Next i
ws.Range("K" & i).FormulaLocal = "=if(countif($C$2:C" & i & ";C" & i & ")=1;row();"")
I am getting error on this line. Where am I going wrong?
you need to double the quotation marks within the string (at the end):
ws.Range("K" & i).FormulaLocal = "=if(countif($C$2:C" & i & ";C" & i & ")=1;row();"""")"

.NumberFormat not working on cells using SUMIFS

I want to format a range of cells using the Indian currency comma separation style, that is formatting the default 123,456,789 into 12,34,56,789, and I'm using the following code :
LastCompoundInterval = Range("B" & Rows.Count).End(xlUp).Row
Range("F2:F" & LastCompoundInterval).Formula = "=SUMIFS($I$2:$I$" & LastTransaction & ",$H$2:$H$" & LastTransaction & ","">"" & B2, $H$2:$H$" & LastTransaction & ",""<="" & C2)"
With Range("F2:F" & LastCompoundInterval)
.NumberFormat = "[>=10000000]##\,##\,##\,##0;[>=100000] ##\,##\,##0;##,##0"
End With
The above number format works on other ranges as expected, but the default number format continues to be in use for Range F
As a standalone Excel solution, I changed your formula to format it as it is being incorporated into the cell using TEXT().
LastCompoundInterval = Range("B" & Rows.Count).End(xlUp).Row
Range("F2:F" & LastCompoundInterval).Formula = "=TEXT(SUMIFS($I$2:$I$" & LastTransaction & ",$H$2:$H$" & LastTransaction & ","">"" & B2, $H$2:$H$" & LastTransaction & ",""<="" & C2),""[<=9999999]###,####;###,###,####"")"
For a VBA solution, this will help guide you by adding the reference to the workbook / worksheet. Be sure to change "Sheet1" to the name of your worksheet.
LastCompoundInterval = Workbooks(ThisWorkbook).Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
Workbooks(ThisWorkbook).Worksheets("Sheet1").Range("F2:F" & LastCompoundInterval).Formula = "=SUMIFS($I$2:$I$" & LastTransaction & ",$H$2:$H$" & LastTransaction & ","">"" & B2, $H$2:$H$" & LastTransaction & ",""<="" & C2)"
With Workbooks(ThisWorkbook).Worksheets("Sheet1").Range("F2:F" & LastCompoundInterval)
.NumberFormat = "[>=10000000]##\,##\,##\,##0;[>=100000] ##\,##\,##0;##,##0"
End With

Issue when inserting a formula into a cell

I'm trying to insert formulas into cells using this following code:
ActiveCell.Formula = "=ISERROR(IF(O" & ActiveCell.Row & "=M; _
LEFT(O" & ActiveCell.Row & ";LEN(O" & ActiveCell.Row & ")-1)*1000000; _
LEFT(O" & ActiveCell.Row & ";LEN(O" & ActiveCell.Row & ")-1)*1000000000);" & vide & ")"
(vide is a string variable) but it keeps returning Application defined or Object defined error.
Thanks !
Change ; to ,
Change " & vide & " to """ & vide & """
Change M to ""M""
Change ISERROR to IFERROR
ActiveCell.Formula = "=IFERROR(IF(O" & ActiveCell.Row & "=""M"",LEFT(O" & ActiveCell.Row & ",LEN(O" & ActiveCell.Row & ")-1)*1000000,LEFT(O" & ActiveCell.Row & ",LEN(O" & ActiveCell.Row & ")-1)*1000000000),""" & vide & """)"
It's because of your local settings, you use ; unstead of ,.
If you must use ;, then try replacing:
ActiveCell.Formula
with:
ActiveCell.FormulaLocal
Also, I like to use Chr(34) to get the ".
So at the end change your formula to:
...1000000000);" & Chr(34) & vide & Chr(34) & ")"

Convert sumproduct formula into a generic formula that can be used across all sheets in a workbook

I have this formula:
=SUMPRODUCT(('Data-2017'!W1:EZ1="Enemy 1")*('Data-2017'!W3:EZ1361="Yes")*('Data-2017'!J3:J1361=$T$4))
where Data-2017 is the sheet name.
I want to convert it into a generic formula that references a sheet name in a cell.
I have tried converting to following but failed:
=SUMPRODUCT(--(INDIRECT("'" & $D$3 & "'!" & "W1:EZ1=" & CHAR(34) & "Enemy 1" & CHAR(34))*("'" & $D$3 & "'!" & "W3:EZ1361=" & CHAR(34) & "Enemy 1" & CHAR(34))*("'" & $D$3 & "'!" & "J3:J1361=" & T4))
where D3 is the cell for sheet name, T4 is another value in to suffice the condition.
INDIRECT only goes around the cell references not the whole formula:
=SUMPRODUCT((INDIRECT("'" & $D$3 & "'!" & "W1:EZ1")="Enemy 1")*(INDIRECT("'" & $D$3 & "'!" & "W3:EZ1361")="Yes")*(INDIRECT("'" & $D$3 & "'!" & "J3:J1361")=$T$4))
I actually figured out by accident after further research:
=SUMPRODUCT((INDIRECT("'"&$D$3&"'!$W$1:$EZ$1")="Enemy 1")*(INDIRECT("'"&$D$3&"'!$W$3:$EZ$1500")="Yes")*(INDIRECT("'"&$D$3&"'!$J$3:$J$1500")=T$4))

Inserting semicolon in between data in a cell in Excel

I have data in column A, C, E and G. I want column I to hold all of the data from these separated by semicolons, I have searched the web but all I find is how to replace line breaks with semicolons and that doesn't do it. Below are the 3 pieces of code I have attempted to work into it
Range("I" & i).Value = "=A" & i & Chr(59) & "&" & "C" & i & "&" & "E" & i & "&" & "G" & i
Range("I" & i).Value = "=A" & i & "&C" & i & "&E" & i & "&G" & i
Range("I" & i).Value = Range("A" & i).Value + ; + Range("C" & i).Value + ; + Range("E" & i).Value + ; + Range("G" & i).Value
The second line comes closest to what I want but when I add & ";" into it I get errors.
Any ideas thoroughly appreciated
SOLVED : This was solved by an answer below, I had managed my own fix which I will detail but the accepted answer is a cleaner, more efficient way
mystring = "=A" & i & "&" & Chr(34) & ";" & Chr(34) & "& C" & i & "&" & Chr(34) & ";" & Chr(34) & "& E" & i & "&" & Chr(34) & ";" & Chr(34) & "& G" & i
Range("I" & i).Value = mystring
You should use "&"";""".
In detail, you should use something like
"=A" & i & "&"";""" & "&C" & i
which result in
=A1&";"&C1
suppose i = 1.
=CONCATENATE(A1, ";", C1, ";", E1, ";", G1)
Using VBA
Range("A2").FORMULA = REPLACE("=CONCATENATE(A1, ';', C1, ';', E1, ';', G1)", "'", CHR(34))

Resources