I have an Excel sheet with complex numbers.
So in cell(1,1) there is e.g.: 40318,5705419227+153347,941302982i
If I want to get the absolute value of the number I can insert manually = IMABS(A1), which is giving me the right result: 158559,6993
But if I want to calculate the statement using vba:
string1 = WorksheetFunction.ImAbs(Cells(1, 1).value)
it gives me: 431363308773779
Can anyone explain?
Apparently, in VBA, that function requires the US decimal symbol. And your value is using the comma. The following seems to work:
WorksheetFunction.ImAbs (Replace(Cells(1, 1), ",", "."))
EDIT: This was also suggested by #pnuts in his comment above.
If your value of 40318,5705419227+153347,941302982i is located in Cell "A1", the correct reference to it would be:
String1 = WorksheetFunction.ImAbs(Cells(1, 1).Value2)
Related
I am trying to have some vba code to place the value of a formula and text into a specific cell. I want the code to say Leads:(value of formula). Currently my code runs the formula and places the value in the correct box. I just do not know how to add the text with it. The code I have is written below.
ws.Range("$B$1").Formula = "=COUNTIF(E:E,""Lead"")"
A custom Range.NumberFormat property will give you the displayed result while leaving the actual raw value in a numerical form for possible further computation or comparison.
with ws
with .range("B1") '<~~ no need for absolute $ anchors here
.formula = "=COUNTIF(E:E,""Lead"")"
.numberformat = "[=1]L\e\a\d\: 0;L\e\a\d\s\: 0"
end with
end with
Try this:
ws.Range("$B$1").Formula = "=""Leads:("" & COUNTIF(E:E,""Lead"")&"")"""
so that it ends up like the following when applied:
="Leads:(" & COUNTIF(E:E,"Lead")&")"
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'm trying to input a simple formula in the current active cell. The formula should include a rounding of a division of the cell to the left by the last cell with data on the same column (which is a total).
I'm using this:
Dim strAddress3 As String
strAddress3 = Range("G8").End(xlDown).Address
ActiveCell.FormulaR1C1 = "=+ROUND(RC[-1]/" & strAddress3 & ",2)"
Apparently, I can't use strAddress3 but I don't know why.
My apologies if this is a simple questions, I've been looking for an answer but couldn't find it.
.Address without further arguments returns the address in the xlA1 notation. I think you should use strAddress3 = Range("G8").End(xlDown).Address(true, true, xlR1C1 )
This problem seems very simple, yet I just can not find the solution (I am already loosing my mind about it :) )
OK, so I just want to put a certain value into an excel cell, using vba code, just as simple as this:
Cells(1,1).Value2 = "123,456"
The problem: this is a string (intentionally), but excel always convert it to number, and put that number into the cell, instead of the string that I wanted.
How can I force excel not to convert it, and just put into the cell exactly what I want (the string)??
Thanks,
Cells(1,1).Value2 = "'123,456"
note the single apostrophe before the number - this will signal to excel that whatever follows has to be interpreted as text.
Indeed, just as commented by Tim Williams, the way to make it work is pre-formatting as text. Thus, to do it all via VBA, just do that:
Cells(1, 1).NumberFormat = "#"
Cells(1, 1).Value = "1234,56"
This is probably too late, but I had a similar problem with dates that I wanted entered into cells from a text variable. Inevitably, it converted my variable text value to a date. What I finally had to do was concatentate a ' to the string variable and then put it in the cell like this:
prvt_rng_WrkSht.Cells(prvt_rng_WrkSht.Rows.Count, cnst_int_Col_Start_Date).Formula = "'" & _
param_cls_shift.Start_Date (string property of my class)
I have (what seems like it should be) a simple problem. I need to be able to tell excel (in vba) that a cell's contents are numeric, but I don't really want to apply any formatting to it. I need my trailing zeros left how they are. Am I missing something incredibly simple?
Update:
I'm getting xml data from a query and am placing it into the spreadsheets. But lets say one of the cells has 589.950000 I need to keep those additional zeros on display for the user (don't ask me why, they just want the precision) but excel converts it to 589.95. I also need for them to be able to do spreadsheet calculations on that cell (so I can't format it as text).
Update 2:
Further Clarification. Lets say I have 4 values that I'm going to place into a column
595.56000
15.00
90.00050
1919.120000000
is there one numeric format that I can apply to that column that will display those exact numbers?
You can't do this with one custom format.
What you can do is make a macro that does the input for you and modifies each cell format as it puts the value into it. You can use the Macro Recorder to get a handle on where to start with this.
I think you can do:
Val(Range("A1").Value)
Which will evalulate the text to a number
You mean you want to use the value of the cell as a numerical one but you don't want to change the real val of the cell ?
In vba, you can use CInt(yourvar) to convert a string to number. Thus, the value you get will sure be an integer.
[edit] Btw, if you want to display or set back the value to a cell with the format you want, you can use the Format ( expression, [ format ] ) function of Excel vba
[edit 2]
As you cannot predict how many zeros you will have, i can't see any number format that would work. You'd probably better use the Text format as Lance Robert suggested. To use formula on text cells, you can use the VALUE formula to convert the cell to use it as a number. If you need it on a range, you may have to use array formulas like this : {=SUM(IF(ISTEXT(A1:A4);VALUE(A1:A4);A1:A4))}.
Please let us know if you want some help on these formulas.
Regards,
Max
If you know the number of decimal places you need, you can use something like the following:
Range("A1").NumberFormat = "0.000000"
I suppose you could even get fancy and check the number of trailing 0s in the code and adjust the formatting as required.
UPDATE:
Here's a VBA function that takes the number as a string and returns the appropriate NumberFormat string.
Private Function trailing(strNum As String) As String
'From number entered as string, returns Excel Number format to preserve trailing zeroes in decimal.
Dim decpt As Integer
Dim aftdec As Integer
Dim strTmp As String
decpt = InStr(strNum, ".")
If decpt = 0 Then
strTmp = "0"
Else
aftdec = Len(strNum) - decpt
strTmp = "0."
If aftdec <> 0 Then
For i = 1 To aftdec
strTmp = strTmp & "0"
Next
End If
End If
trailing = strTmp
End Function
Can't believe I just stumbled on this...
I know it's old but might as well give very simple answer:
Custom format and simply use # symbol. Will be treated as integer and left exactly as typed in to cell.