I am using:
For t = 1 To ActiveProject.Tasks.Count
With Tasks(t)
' DurSen is a Custom Field Name. It returns an application field Number (such as 188743770 for Number4)
Projectfield = FieldNameToFieldConstant("DurSen")
' the SpearmanRankCorrelation function occasionally produces a result like -6.06060606060606E-03.
.SetField (Projectfield), SpearmanRankCorrelation(ProjectDuration, TmpVec, NN)
End With
Next t
This is a number expressed as an exponent. Due to the presence of the "E" in the number, it fails the Setfield function. The .Setfield places the calculated value into the field referenced by ProjectField for a specific task, (t) but fails when the calculated value contains an "E".
Any ideas on avoiding this problem?
The SetField method expects the Value parameter to be a string. So the solution is to format the number as a string. (e.g. Format(-6.06060606060606E-03, "0.0#####") => -0.006061)
However, Microsoft Project will round the value to the nearest hundredth, so you will lose the precision.
Better to store numbers that require more than two decimal places in a text field and convert them back to numbers as needed in code.
I had a simpler answer hat I figured out yesterday. Instead of trying to deal with it as a number, I simply converted to a string, used the InStr method to see if an "E" was present and if it is, assign a value of 0.001 as the result, as any E value will be smaller than .01.
If InStr(SpearmanRankCorrelation(ProjectDuration, TmpVec, NN), "E") > 0 Then
.SetField (Dursen), 0.001
PD = 0.001
outputstr = ("575 - ID= " & tasks(t).ID & " SpearmanRankCorrelation= " & SpearmanRankCorrelation(ProjectDuration, TmpVec, NN) & " Replaced by 0.001")
Call Txt_Append(myfile, outputstr)
Else
.SetField (Dursen), SpearmanRankCorrelation(ProjectDuration, TmpVec, NN)
End If
Thank you, Rachel, for your answer. I am trying not to use Project Custom fields. The .Setfield above assigns the Result to the field which is identified by the value of Dursen ( which is an internal field reference).
Rachel, I tried your approach but whenever I encountered a result that was expressed with an "E" in the Result, the conversion failed. using >Setfield will only round to the nearest hundreth if the entire string is a numeric value (when converted). The >Setfield method does not work when there is an "E" in the expression.
Related
The logic isn't complex. The application uses a temporary table into which data for a report is appended. Once the report has been run, the data is cleared. Initially, the client told me that he would enter all the data (by taking attendance of people attending an event) would be done at once. Now, that has changed. I lookup the max ID in the temp table - if the number of rows is greater than 0. I then want to use dlookup to let the client know where she left off lest they create more problems.
Here's the logic:
Dim VMax As Variant
Dim VNFind As String
VMax = DMax("foodpantryid", "signin_sheet_data") 'This works correctly
MsgBox VMax 'This works correctly
VNFind = DLookup("last_name", "Dat_household_member", "[household_id]" = VMax) 'I get the type mismatch/error 13 here.
MsgBox VNFind 'so this never works
Additional information:
Household_id is a long integer. When I change it to integer, I get an error 94 (invalid use of null).
I have tried setting VMax to variant, integer, and long. Still no success. I would think variant would have worked.
The DLookup works if I don't include "[household_id]" = VMax.
I've tried including the =VMax in "" and that fails as well. That produces a 2471 error.
You've put your = outside the string delimiters.
This means: say your ID is 5. Then the parameter is "[household_id]" = 5. That's a comparison between the string "[household_id]" and the number 5, which causes a type mismatch, because you can't compare strings to numbers unless the string can be cast to a number.
Instead, you should include the = in your string, and the DLookUp should be:
DLookup("last_name", "Dat_household_member", "[household_id] = " & VMax)
Or, better yet, use parameters. This avoids most typecasting and string concatenation errors.
TempVars!VMax = VMax
DLookup("last_name", "Dat_household_member", "[household_id] = TempVars!VMax")
TempVars.Remove "VMax"
I want to set a result with 2 digits after the comma.
Is there any fast way to do this please ?
Because I need first to look for the result.
(If positif : add "+", negatif let the "-" and null "make it on 0.00)
Dim VariationX as Single
VariationX = 0,5
If VariationX >= 0.01 Then
VariationXString = "+" & VariationX
ElseIf VariationX = 0 Then
VariationXString = "0,00"
End If
VariationXString = Replace(VariationXString, ",", ".")
In this exemple
The result will be "+0.5"
But I want "+0.50" because in some case, the VariationX could be "10,50", "1,20", "-2.8" or simply "1". And I always need to have 2 digit after the comma.
Was going to use InStr, Len and Mid, but this would make a lot of line just for this.
Sorry to be boring, but this looks like a job for a custom number format...
Number formats are structured like so: [positive];[negative];[zero];[text]. So you want something like this:
"+"#,##0.00;"-"#,##0.00;#,##0.00
I get 0.00 for any zero value, +0.50 for 0.5, and -0.25 for -0.2463.
The representation of a value is different from that value. If you want to be able to use 0.5 in calculations, having it stored as "+0.50" is going to cause headaches - but having it stored as 0.5 and represented as +0.50 won't break anything.
If the presentation concern needs to be addressed in code (e.g. if you want that formatted string value to show up in some form's TextBox), then you can use the Strings.Format function to get the same result:
Public Function PrettifyVarianceFigure(ByVal value As Double) As String
PrettifyVarianceFigure = Strings.Format$(value, _
IIf(value > 0, "+", vbNullString) & _
"0" & Application.International(xlDecimalSeparator) & "00")
End Function
You can convert back to /round-trip to a Double by using the standard conversion functions*:
Debug.Print CDbl(PrettifyVarianceFigure(0.42)) ' outputs 0.42
*I doubt CDbl will handle a non-dot decimal separator though, so you might need to make a Strings.Replace call first.
I have a text made of some cells with concatenation. One of the items of the text is a number. How to make it a full number with all the necesarry comas etc.
I have: on stt 03/06 db corr PLN 60000000 val 03/06 pending
I need: on stt 03/06 db corr PLN 60,000,000.00 val 03/06 pending
I tried with CDbl(Sheets(1).Cells(i, 5).Value) but the number is still the same.
Please note that the source cell contains the number in correct format : 60,000,000.00 which is downloaded from DB2 database
Many thanks for any help how to achieve that
To concatenate a string and a number you just use &, then convert the result to a number with Val() function.
Then if you want to format the result number you can use Format() function. Then you get a formatted string.
Example:
j = Val("10" & 10)
s = Format(j, "##,###.00")
I think that the use Format Function, resolve your problem -> https://msdn.microsoft.com/en-us/library/office/gg251755.aspx
Maybe format(60000000,"##,##0.00")?
If your cell is already formatted appropriately, you can use its Text property:
Sheets(1).Cells(i, 5).Text
Note that because this is the displayed value, you can get #### returned if the column width is too narrow for the actual value, but it's still useful where you don't necessarily know the format ahead of time.
Alright, I am trying to create a user defined function that will accept one character input, and then return a String. Although I couldn't get the Char data type to work for VBA (not sure why?) I was able to get it working fine if I inputted the single character value as a String.
Currently, when the UDF is running within Excel, it will work as long as quotes are placed around the String input, as shown:
=tradeClass("o")
that will work and return the correct result. But what I really would like to do is be able to write the Excel function in this manner:
=tradeClass(o)
I wrote out code that will concatenate "" onto the ends of my user input:
Public Function tradeClass(class As String)
Dim result As String
Dim before As String
Dim after As String
before = """"
after = """"
class = before & class & after 'my attempt to concatenate " on either side of the class input
Select Case LCase(class)
Case "s"
result = "Sale"
Case "r"
result = "Redemption"
Case "i"
result = "Exchange In"
Case "o"
result = "Exchange Out"
Case "x"
result = "Ignore"
Case "k"
result = "Settle"
Case "m"
result = "Transfer"
Case "w"
result = "ML PR3 Redemption (No longer in use)"
Case Else
result = "Invalid Entry"
End Select
tradeClass = result
End Function
It returns a #VALUE! error in Excel, so I am starting to think that I will always have to enter quotation marks with my character. Is this the case?
I can't imagine what compelling reason you have to want to do this. If you were truly determined, there is a hack/workaround/kludge that will allow this to work.
I strongly advise you not to use this, but you asked a question to which I have an answer. So here goes:
When you pass an unquoted string of character(s) to a function in Excel, it assumes that it is a reference to another cell. This is obvious in a function call such as =tradeClass(A1). That function will pass the value of the cell A1 to the function tradeClass.
Excel also supports naming cells to make formulas easier to follow. For example, you could assign the name "StartYear" to cell A1. So instead of using =DATE(A1, 1, 1) you could use =DATE(StartYear, 1, 1) which will be easier to maintain.
Now, in your case, we can abuse this power and name a bunch of cells s, r, i, o, x, k, m, and w. In the cell named s we would enter the letter "s" as the cell's value and so on. So then when you call =tradeClass(o), Excel will look up the cell named o and get its value (which we set to "o") and pass the value "o" of the cell named o to your tradeClass function.
This is a dreadful abuse of the cell-naming capabilities of Excel and violates all sorts of good programming practice. But ask, and you shall receive.
DISCLAIMER: Please do not actually use this.
One final note: When, someday, someone comes along and changes the value of the cell named o to the letter "s", please refer to the above disclaimer.
Unfortunately, if you want this function to work on user input as specified, you're going to need to pass in with quotes. Try out a built-in Excel function, for example:
=LEN(String)
vs.
=LEN("String")
The second one gives you 6, while the first one gives you an error.
However, your code would work nicely if the trade classes you're after are in their own column, like this:
Public Function tradeClass(Str As Variant)
Str = CStr(Str) 'force the variant to a string
Select Case LCase(Str)
Case "s"
result = "Sale"
Case "r"
result = "Redemption"
Case "i"
result = "Exchange In"
Case "o"
result = "Exchange Out"
Case "x"
result = "Ignore"
Case "k"
result = "Settle"
Case "m"
result = "Transfer"
Case "w"
result = "ML PR3 Redemption (No longer in use)"
Case Else
result = "Invalid Entry"
End Select
tradeClass = result
End Function
I know it’s an old question but got the same error and found the reason and something like this worked for me: you shall use Public Function tradeclass(class) and then class = class.address. Hope this helps!
I am working in an ArcGIS attribute table (basically an Access Table) and I have set up some subtypes for a short integer field. The subtypes give a coded for each value and an associated descriptive value for the code. See the following:
Code Description
0 Low
1 Medium
2 High
I populate one of the coded values for each record in that field, so that each cell has either Low, Medium or High. Not the actual coded value of 0,1,2.
I want to field calculate a second field of type TEXT based a concatenation using this subtype field in conjunction with 2 other Text field. The concatenation works, except it returns the code for each record as opposed to the descriptive value. I would like the descriptive value. Does anyone know how to have the field calculator return this?
Thanks,
Mike
This is a field calculate using a VB Script parser, and showing the codeblock option.
Pre-Logic Script Code:
Dim ValueToConvert
Dim ConvertedValue
ValueToConvert = [YourSubtypeField]
Select Case ValueToConvert
Case "0"
ConvertedValue = "Low"
Case "1"
ConvertedValue = "Medium"
Case "2"
ConvertedValue = "High"
End Select
Output = [YourFirstFieldToConcat] & " " & ConvertedValue & " " & [YourSecondFieldToConcat]
CommonName =
Output