I need to know an equivalent in VB code for:
'In Excel cell (for ex. xlSheet.Range("A1"))
{=Average(If(A1:A10="Text",B1:X10))} ' <---Equivalence of this written all in VB code, not in Excel
That is, calculate the average of an Excel matrix (for ex. B1 to X10 Excel range), with criteria, from VB.
Here:
Evaluate("=AVERAGE(IF(A1:A10=""test"",B1:U10))")
Don't forget to put double quotes after single quotes for text.
I would like to suggest this code for the issue:
Worksheets("Data1").Range("D2") = Application.WorksheetFunction.AverageIfs(Worksheets("Sheet1").Range("A2:A11, "E2<>0", B2:B11))
Other should be:
Suppose in A1 you have written this formula, so in VBA code you can use something like this:
=IF(WEEKDAY(A1,2)>5,"NO WEEKDAY","")
Range("b2").Formula = "=A1"
Please change cell address as you need.
Related
I have a macro button in worksheet 2 and want it to put a formula into a column of worksheet 1.
For example I can do a simple sum formula such as:
Sheets("worksheet1").Range("I:I") = "=SUM(M:M)"
This works but when I try and do it with the actual more complicated formula I want it will not work.
Why is this?
Sheets("worksheet1").Range("I:I") = "=IF(ISNUMBER(SEARCH("*567*",B:B)),"INSTOCK","")"
Writing a double quote like you did makes VBA think you ended your string after just writing "=IF(ISNUMBER(SEARCH(". In fact, this code will error out. You'll need to double-up your quotes. A great way to understand what you are writing would be to use Debug.Print first:
Debug.Print "=IF(ISNUMBER(SEARCH(""*567*"",B:B)),""INSTOCK"","""")"
So this will work:
Sheets("worksheet1").Range("I:I") = "=IF(ISNUMBER(SEARCH(""*567*"",B:B)),""INSTOCK"","""")"
Note: since you are using whole column references, this is going to be heavy on your calculation!
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.
Using MS Excel 2010: I used the CONCATENATE formula to create a text string that looks like a formula and need a formula that will convert the text string to a formula; without the use of MS Excel Paste Special function or VBA/Macro.
Example: In Cell B2:G2 contains text, in Cell B4 I have CONCATENATE text formula that returns a text string
=TRIM(CONCATENATE(B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2))
I want Excel to interpret this string as a formula and return/show the value in Cell B6 as: "Always be yourself … do not go out and look for a successful personality and duplicate it.” - Bruce Lee
I have attempted using the INDIRECT formula and without success, not certain if it's my formula or if it's possible. SAMPLE: Cell B6:
=INDIRECT(CONCATENATE("B4"))
1) CONCATENATE and & are the same thing. In other words the formula you wrote as a string could be translated as:
=TRIM(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(CONCATENATE(B2)," "),C2)," "),D2)," "),E2)," "),F2)," "),G2)," "),H2))
If you are going to write out CONCATENATE for you function, then separate all he strings you want joined together as with a coma as has been pointed out by #MarcoVos in the comments to your question:
=CONCATENATE(B2," ",C2," ",D2," ",E2," ",F2," ",G2," ",H2)
Or if you want to use the more common form of & your formula would look like what you originally posted without the CONCATENATE out from like this:
=B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2
Those last two formulas will produce the same results.
2) The INDIRECT function will convert text to a cell references or address. It will not convert a formula as text and spit out the results of the formula. Macros Vos in his answer is correct in that FORMULATEXT() will display the formula in the referenced cell as text or a string. If you need to follow a sequence where you provide the string and then must convert the string into a formula, then I suggest you use the EVALUATE Function. You cannot use EVALUATE from a regular excel formula though. You can call it from a named range though. Create a named range. Lets for example call it EvalText. In the formula portion for creating the named range, enter:
=EVALUATE($B$4)
It will automatically add the sheet name. Now in any cell on your sheet you can enter:
=EvalText
and it will return whatever the string in B4 works out to be as a formula. It will spit out an error if its not a proper excel formula.
Why not do it the other way around?
Put:
=TRIM(CONCATENATE(B2&" "&C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2))
in cell B6 and
=FORMULATEXT(B6)
in cell B4.
The problem with the named "Range" is that it is not part of the Recalc-Loop.
Defining (in VBA) a function
Function MyEval(text As String)
MyEval = Evaluate(text)
End Function
and adding
Private Sub Worksheet_Change(ByVal Target As Range)
Application.CalculateFull
End Sub
to the codesheet of the worksheet under consideration
solves the problem.
Of course, the workbook than has t be saved as XLSM.
I want to count the number of numbers in a single cell.
Example:
In cell A1, I have the following addition:
=12+45+23+51+10 which totals (and shows) 141.
In cell B1, I would like the see how many numbers have been added together, which means that there should be 5 (12 is a number, 45 another one, etc... and all together, there are 5 numbers in cell A1).
I know it seems to be a ridiculous question, but I scanned all the platforms for this issue and did not find any suitable solution. Tried all the LEN and LEN SUBSTITUTE alternatives out there, but somehow it does not work.
Thank you upfront for your help. Optimal solution would be a excel formula, alternatively VBA would also work.
Excel 2013 has a new function Formulatext() which returns the, well, formula text. Using that the Len() and Substitute() approach works.
=LEN(FORMULATEXT(A1))-LEN(SUBSTITUTE(FORMULATEXT(A1),"+",""))+1
Hit Alt+F11 and Insert->Module with the following VBA:
Function NumCount(Rng As Range)
x = Split(Rng.Formula, "+")
NumCount = UBound(x) + 1
End Function
Then you can call =NumCount(A1) on any cell to get the number of numbers in the formula for that cell.
Use this VBA:
Function GetFormula(Cell as Range) as String
GetFormula = Cell.Formula
End Function
and then to get the number of numbers...
=LEN(GetFormula(D99))-LEN(SUBSTITUTE(GetFormula(D99),"+",""))
on this as my D99 contents...
=45+46+47+50+100
The one major drawback here is that I'm assuming everything is + if you have -, *,/ or other operators this gets more challenging. you might be able to use replace for each but you'd always be limited to the 4 basic operators... if someone used exponents or mod, this would be harder.
Also possible in earlier Excel versions without VBA - subject to (i) there is always at least one value in the cells, (ii) the operator is always + and (iii) the cells are in a column.
Replace = with '= in that column, apply the substitution, say:
=LEN(A1)-LEN(SUBSTITUTE(A1,"+",""))+1
in a different column and Paste Special, Value the results for that other column. Then apply Text To Columns on the original column with ' as the delimiter.
*There is no way to do this without using a User Defined Function (UDF) written in Excel VBA. Something like this should work:
Public Function numsAdded(cell1 As Range)
Dim formulaString As String
formulaString = cell1.Formula
numsAdded = Len(formulaString) - Len(Replace(formulaString, "+", "")) + 1
End Function
Once you've added that to a VBA module, you can use it as a function in any cell in your spreadsheet.
*Edit - Apparently there is a way if you have Excel 2013, as teylyn suggests. If you use 2010 or earlier like me, you'll need VBA.
Try this:
=LEN(SUBSTITUTE(F16,"+","+"))
Note: F16 is only an example name for the cell you want to do the counting on.
Example:
F16=45+65+76 # Gives 186
F17=LEN(SUBSTITUTE(F16,"+","+")) # Gives 3
I have the following formula
=IF((GLOBAL_DATE-30)<G2,"1 Month",IF((GLOBAL_DATE-60)<G2,"2 Month",IF((GLOBAL_DATE-90)<G2,"3 Month","Older Than 3 Months")))
and I would like to write this into specific cells using the FormualR1C1 in VBA.
(the GLOBAL_DATE is a named cell on another sheet)
Thanks
Select the cell that has that formula. In the VBE, go to the Immediate Window and type
?Activecell.FormulaR1C1
and press enter. That will give you the R1C1 translation of your formula.
This is what I get using the Macro Recorder:
ActiveCell.FormulaR1C1 = _
"=IF((GLOBAL_DATE-30)<R[1]C[6],""1 Month"",IF((GLOBAL_DATE-60)<R[1]C[6],""2 Month"",IF((GLOBAL_DATE-90)<R[1]C[6],""3 Month"",""Older Than 3 Months"")))"
That is using relative cell addresses (R[1]C[6] is the cell one row below and 6 columns to the rigth from the ActiveCell. Alternatively, you can use absolute adresses by replacing R[1]C[6] by R2C7 (for row 2, column 7 = G2).
You can easily use VBA to translate those formulae that you entered in a sheet into a sytax that's suitable for VBA. I once wrote a sub for that purpose.