I am trying to create a Sub that places a SUMIF formula in a cell. I have reduced the problem to a simple setup:
Private Sub CommandButton1_Click()
Cells(2, 3).Formula = "=SUMIF(A1:A5;D1;B1:B5)"
End Sub
I get a 1004 Error.
I can run the same code but with the SUM function instead:
"=SUM(B1:B5)"
This does not produce an error. Thus I suspect the semicolons in my formula.
.Formula accepts formulas in English.
Parameter separator in English is ,.
If you want to use formulas in the language of your Excel installation, use .FormulaLocal.
However to ensure your code will run on any Excel, fix your formula to be in accordance with the en-us locale.
Related
I'm learning VBA from a book, and the sample function in it is:
Function CubeRoot(number)
CubeRoot = number ^ (1 / 3)
End Function
If I call that function from a sub procedure, I get the correct result, but when I try to use it directly in Excel, I get a "FALSE" result in the cell. I followed the book step-by-step and it does not say to do anything that I haven't done. What am I missing/doing wrong or maybe I need to change some option? I am using Excel 2016.
Edit:
This is what I write in an excel cell: =CubeRoot(8) The result I get is FALSE. However, if I click the fX button and the function arguments box pops up, the formula result shown there is calculated correctly.
I write in an excel cell: =CubeRoot(8) The result I get is FALSE
At first try declaring the variable types properly:
Function CubeRoot(number As Variant) As Double
CubeRoot = number ^ (1 / 3)
End Function
But I do not believe that this will help because the error result then would be #VALUE! but not a boolean FALSE.
But the ^ operator can be overloaded. Maybe by some AddIn you are using. So it seems that it was overloaded to be some boolean operator.
Try the following:
Function CubeRoot(number As Variant) As Double
CubeRoot = Application.Power(number, (1 / 3))
End Function
If that helps, then my suspicion is true and you should go through your installed AddIns to determine which one is doing the weird overload.
If even the using Application.Power(number, (1 / 3)) does not help, then the cell containing the formula seems to be formatted using a weird number format. Try formatting it using the General number format.
According to your mention: "if I click the fX button and the function arguments box pops up, the formula result shown there is calculated correctly.", I suspect it is the weird number format rather than the first two suspicions. So do selecting the cell containing the formula =CubeRoot(8) and set number format General to it using Home tab and Number group.
Finally the problem was that the worksheet functions had been inputted into Excel4 macro sheets instead of worksheets. Probably the Excel4 macro sheets were added using Sheets.Add Method using the type xlExcel4MacroSheet. But in Excel4 macro sheets only Excel4 macro code is possible not default formulas as in worksheets. Excel4 macro is ancient type of macro language from Excel 4.0 (1992). Since Excel 5.0 (1993) VBA is used for Excel macros.
Declare all variables as double, and also the function return type as double.
By default 8 is treated as integer, so 8*X will result integer even if x is single or double.
I have the following array formula in cell B2 in my Excel spreadsheet:
{=IF(COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)))>=COUNT(IF(ISNUMBER(A30:A1000);COUNT(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);COUNT(B30:B1000>A30:A1000-1;A30:A1000))))}
Now I want to use the following VBA code to copy this code into cell A2:
Sheets("Sheet1").Range("A2").FormulaArray = Sheets("Sheet1").Range("B2").Formula
However, when I use this code I get runtime error 1004.
Do you have any idea how to solve this issue?
Your array formula is too long to pass along like that as the Range.FormulaArray property.
You don't need to keep repeating all of the conditions. As you cycle through rows 30 to 1000, if the first or second condition fails, the remainder of the formula for that cycle is not processed. IFs in a formula stop processing at the first FALSE.
=IF(COUNT(IF(ISNUMBER(A30:A1000), IF(B30:B1000>A30:A1000-1, A30:A1000)))>=COUNT(B30:B1000>A30:A1000-1,A30:A1000),COUNT(B30:B1000>A30:A1000-1,A30:A1000))
Now the code works just fine.
With Worksheets("Sheet3")
.Range("A2").FormulaArray = .Range("b2").Formula
End With
Note that I could not test this using semi-colons as the system list separator; only with my own system's commas. VBA does not like anything by EN-US regional settings in a .Formula, .FormulaR1C1 or .FormulaArray property. If you still have trouble, use debug,print to see how the .Formula is being returned. If it contains semi-colons, then use,
With Worksheets("Sheet3")
.Range("A2").FormulaArray = Replace(.Range("b2").Formula, Chr(59), Chr(44))
End With
In actual cell formula I can manually set the following formula.
=IF(MONTH(A15)<7,"FY "&YEAR(A15)-1&"/"&RIGHT(YEAR(A15),2),"FY "&YEAR(A15)&"/"&RIGHT(YEAR(A15)+1,2))
I am trying to set this formula by vba using the following code.
ActiveCell.formulaR1C1 = "=IF(MONTH(A15)<7,"FY "&YEAR(A15)-1&"/"&RIGHT(YEAR(A15),2),"FY "&YEAR(A15)&"/"&RIGHT(YEAR(A15)+1,2))"
The VBA compiler displays a compile error: Expected: end of statement. This appears to have a problem with the exclamation marks.
Does anyone now how to include a text constant in a cell formula set by vba code?
You need to use double quotes within the string like so:
ActiveCell.formulaR1C1 = "=IF(MONTH(A15)<7,""FY ""&YEAR(A15)-1&""/""&RIGHT(YEAR(A15),2),""FY ""&YEAR(A15)&""/""&RIGHT(YEAR(A15)+1,2))"
You only use FormulaR1C1 if you pass a reference in R1C1 format, but you're using A1 format. You can also shorten that formula if you wish:
ActiveCell.Formula = "=""FY ""&YEAR(A15)-(MONTH(A15)<7)&""/""&RIGHT(YEAR(A15)+(MONTH(A15)>=7),2)"
I am having trouble concatenating two columns (variable range) to another column in the same document. Is there something really obvious I'm missing?
Function ConcCol(ConVal_1 As String, ConVal_2 As String)
Range("V30:V500").Select
Application.CutCopyMode = False
With ActiveCell
.FormulaR1C1 = "=CONCATENATE(RC[5],"" "",RC[6])"
End With
Selection.AutoFill Destination:=Range(Destination), Type:=xlFillDefault
End Function
When running the above function, I get the following error:
Error: Run-time error '1004': AutoFill method of Range class failed
I assume it's something to do with only one cell being activated perhaps? My VBA knowledge is limited
Sub ConcCol()
Range("V3:V500").Select
For Each Cell In Selection
Cell.Value = "=CONCATENATE(RC[5],"" "",RC[6])"
Next Cell
End Sub
I think it'd be easier (if you're going with the selection idea), to just use each cell. I think your with statement doesn't want to loop at all, hence error. This works, a simple solution.
I think this is what you're looking for:
Sub tgr()
With Intersect(ActiveSheet.UsedRange.EntireRow, Range("V30:V" & Rows.Count))
.Formula = "=AA" & .Row & "&"" ""&AB" & .Row
End With
End Sub
Hey I think you are a bit confused. How do you plan to run this code? You need to provide more details. Here are three ways of running a vba code:
User Interface as a User Defined Function (UDF)
As a Function that is called/used by another VBA code
AS a macro and user need to run it from the macro list
For your purpose, why doesn't the user use CONCATENANTE function in the sheet and fill down?
Maybe use the Excel array functions?
I cannot post images, need 10 reputations :s
'I am using this formula in an Excel worksheet, in cell A6. It is working fine.
=IF(O6="Hand","Manual Entry",IF(O6="JET",R6,IF(O6="COKE","Red Bull",IF(O6="Freight","Logistics",IF(O6="TAX","Tax",IF(O6="TRANSFER COST","Transfer Cost Transactions",IFERROR(IF(FIND("INV#",R6,1)>=1,MID(R6,FIND("INV#",R6,1),10),""),"")))))))
Now, my question is: how do I convert this to VBA? I have tried recording it, and the code is as follows:
ActiveCell.FormulaR1C1 = _
"=IF(RC[14]=""Hand"",""Manual Entry JE"",IF(RC[14]=""JET"",RC[17],IF(RC[14]=""COKE"",""Red Bull"",IF(RC[14]=""FREIGHT"",""Logistics"",IF(RC[14]=""TAX"",""Tax"",IF(RC[14]=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",RC[17],1)>=1,MID(RC[17],FIND(""INV#"",RC[17]" & _
"""""),"""")))))))"
When I run this, I am receiving Run Time Error 1004: Application-defined or object-defined error.So I Changed this to something like this, this doing same as above formula except the find option, everything is running fine.
![VBA For Above formula][Any Help on the find?]
End sub.How do i get the fine option in the above VBA code.`
This works for me:
Range("L6").Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"
This is just your original Excel formula, but with the " characters escaped as "".
You need to escape your quotes.
ActiveCell.Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"
And use .Formula since you're using specific cell names that are not relative to the currently selected cell.