Cannot include a cell-formula into VBA code in MS Excel - formula

I am trying a simple VBA program, that looks like this:
Function NotWorking() As Single
If Range("C3") = 0 Then
NotWorking = "=0.6*7"
Else
NotWorking = 1.2
End If
End Function
If cell C3 has 0 as a value, the function NotWorking has to return in the current cell the result of *=0.6*7*, which is 4,2. If the value in C3 is not 0, then the current cell has to return 1,2.
In my case, things aren't the way I expected. In the current cell I type =NotWorking(). If C3 is anything but 0, I obtain 1,2, but when I appropriate 0 to C3, the current cell returns #VALUE.
Why does this code not work?
Best regards and thanks in advance!

I notice that "=0.6*7" is a string, not a number, but 1.2 is a number.
You have specified that the function returns a single, but "=0.6*7" isn't a single.

Don't try to return it from the routine, but reach out to the sheet from within the routine.
Sub NotWorking()
Dim src As Range
Dim dst As Range
Set src = Range("C3")
Set dst = Range("E3")
If src.Value = 0 Then
dst.Formula = "=0.6 * 7"
Else
dst.Value = 1.2
End If
End Sub

Related

skip missing values in a for loop

I have a folder with 1000 tifs. I'd like to read 4 tifs, do some calculation and read the next 4 tifs. The problem is, that for example tif number 500 is missing. Now my current program stops right bevor tif number 500.
So my idea is that I check if the path exists with file_test and /directory and skip all missing values in the foor loop.
Directory: Set this keyword to return 1 (true) if File exists and is a directory. true =1, false = 0
for j = 0, 1102 do begin
PathEx = File_test(e:\Meteosat\Tiff\2016\06\17\MSG_201606170100_B4_L.tif', directory)
if PathEx = 1 then
B = READ_TIFF(e:\Meteosat\Tiff\2016\06\17\MSG_201606170100_B4_L.tif, GEOTIFF=tags)
if PathEx = 0 then
print, 'missing' and continue
end
I want to skip all missing paths. I don't know how to do this. I Also read something about
.CONTINUE
But I have no clue how this works too.
thank you!
pampi
I am not sure you are using the correct syntax. Your code should be something like the following:
FOR j=0L, 1102L DO BEGIN
PathEx = FILE_TEST('e:\Meteosat\Tiff\2016\06\17\MSG_201606170100_B4_L.tif', /DIRECTORY)
IF (PathEx[0] EQ 0) THEN CONTINUE ;; This will jump to the next index
b = READ_TIFF('e:\Meteosat\Tiff\2016\06\17\MSG_201606170100_B4_L.tif',GEOTIFF=tags)
ENDFOR
You should probably have a list of file names and then index those. Suppose you call them something like filenames and this is a [N]-element string array. Then you would do something like the following within the above loop:
file = filenames[j]
PathEx = FILE_TEST(file[0],/DIRECTORY)
IF (PathEx[0] EQ 0) THEN CONTINUE ;; This will jump to the next index
b = READ_TIFF(file[0],GEOTIFF=tags)
I should also mention that the variable b should either be an array or you should have an array defined outside the loop that you can store data into, otherwise all your operations within the FOR loop will be lost.

ASP.NET determine number of digits after decimal point on GridView.RowDataBound

I am binding data to my GridView control and in one of the columns I have a price column which is of type double.
I also have a key in the web.config file that tells me how many digits to display after decimal point.
I want to display the data bound field according to that key in the web.config.
I saw this post on how to set the number of digists after decimal point: DataBinding Eval To 2 Decimal Place Doesn't Show 0
but I need a way of doing that with a variable that tells how many decimal places to use.
I tried this:
Private Sub grdData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdData.RowDataBound
Dim numOfDigitsAfterDecimal = Integer.Parse(ConfigurationManager.AppSettings("numOfDigitsAfterDecimal"))
Dim d As String = ""
For Z As Integer = 0 To numOfDigitsAfterDecimal - 1
d = d + "#"
Next
e.Row.Cells(3).Text = String.Format("{0:0.{1}}", Double.Parse(e.Row.Cells(3).Text), d)
End Sub
but it does not work, however if I do:
e.Row.Cells(3).Text = String.Format("{0:0.##}", Double.Parse(e.Row.Cells(3).Text))
it does work and displays 2 decimal places.
you can use N0 or N1 or N2 and so the number will show the decimal place. check this article
Example :
number.ToString("N3")
// N: 1,054.32
// N0: 1,054
// N1: 1,054.3
// N2: 1,054.32
// N3: 1,054.322

Array.IndexOf always returns -1

This is my code.
Public Sub SomeFucntion(ByVal test As Short)
SomeOtherFucntion(MyArray)
If Array.IndexOf(MyArray, test) <> -1
//....
End If
End Sub
test return value 10.
But i am getting IndexOf value as -1.
Inside QuickWatch in VS 2005 when i put the value as 10 instead of test i am getting the correct index.
My array is single dimensional array which now has 2,5 and 10. Since its got 10 it should ideally return 2 as the index.
The problem might be caused by the fact that your array contains integers, but the value you are looking for is a short. Consider the following example:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, value)) ' Prints -1
If the array contains integers, you need to convert your short into an integer first, for example, by using CInt:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' Prints 0
Edit: Note that the declared type has nothing to do with this. Let's declare the array as Object, since that's what you mentioned in your comment (note that the following example requires Option Strict Off, which is bad):
Dim myArray As Object = New Integer() {5}
Dim value As Object = 5S ' Short literal
Console.WriteLine(Array.IndexOf(myArray, value)) ' still returns -1
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' returns 0
Note: You can make that conversion implicit by declaring your function as Public Sub SomeFunction(ByVal test As Integer).
Dim test As Short = 5
Dim MyArray() As Short = {1, 3, 4, 5, 7, 3}
If Array.IndexOf(MyArray, test) <> -1 Then
MessageBox.Show("Index Found.")
End If
The above works. since test is declared as short, make sure MyArray is also declared as short.

chart datapoint operand usage

In the code snippet below I am attempting to compare two data points as I am trying to put all low value points in a pie chart dataset into on "Other" datapoint.
I am getting an "Cannot apply operator <> to operands of type System.....DataPoint and System.....DataPoint" on this code line " If pointValue <= minValue AndAlso Me.series.Points(index) <> colectedDataPoint "
Dim collectedValue As Double = 0.0
For index As Integer = 0 To Me.series.Points.Count - 1
Dim pointValue As Double = Math.Abs(Me.series.Points(index).YValues(0))
If pointValue <= minValue AndAlso Me.series.Points(index) <> colectedDataPoint Then
' Add point value to the collected value
collectedValue += pointValue
' Add point to supplemental series
Me.supplementalSeries.Points.Add(Me.series.Points(index).Clone())
' Remove point from the series
Me.series.Points.RemoveAt(index)
index -= 1
End If
Next
but yet if I write the same loop in C# that equivalent line is fine;
this.series.Points[index] != colectedDataPoint
anyone have a solution since the whole project is written in vb.net and i'd prefer to keep it uniform.

Assistance with Lua Cosine, wrong results returned

Ok, first up, this is NOT for a class, test, or other student type activity.
I'm a scripter for a game, and am trying to implement the math library for all to use, and unfortunately, all I have available to me is very basic lua. The implemented version cannot be changed, and does not include any libraries. For those wondering, its for scripting in Fold.It.
Here's what I have...
math={}
math.fact = function(b) if(b==1)or(b==0) then return 1 end e=1 for c=b,1,-1 do e=e*c end return e end
math.pow = function(b,p) e=b if(p==0) then return 1 end if(p<0) then p=p*(-1) end for c=p,2,-1 do e=e*b end return e end
math.cos = function(b,p) e=0 p=p or 10 for i=1,p do e=e+(math.pow(-1,i)*math.pow(b,2*i)/math.fact(2*i)) end return e end
To clarify above, math.fact returns factorial, which is returning accurate to about 10 points of precision, and is a new function I've done to aid in cosine calculation.
The math.pow is also a new function to handle returning powers, also working as expected.
The issue is with the cosine function. Its returning unexpected values. Here's an easier to digest version (I've been writing my library stuff ultra lean)...
function math.cos(value,precision)
result=0
precision=precision or 10
for i=1,precision do
result=result+(math.pow(-1,i)*math.pow(value,2*i)/math.fact(2*i))
end
return e
end
The problem is, with those functions, for print(math.cos(90)) it returns 4.77135... when I'm expecting -0.44807... (based on calc in scientific mode, or using an online tool to cos(90)).
I'm also having issues with sin and tan, however they are similarly written to cos, which seems to have been done in many languages. If I can figure out what I'm doing wrong, I can get them all fixed.
EDIT: Corrected typo
First, your lua doesn't run. Second, you need to make your variables local. Third, cosine starts with a one.
The problem is because the Taylor series you are using only converges on the correct values of cosine close to zero. You would have to use a far more terms of the series to get it to handle 90 correctly. You can fix this for your implementation two ways:
Add a pi constant. Then use a while loop to adjust the value such that abs(value) < 2*pi:
math.pi = 3.14159265358
while value > math.pi*2 do
value = value - math.pi * 2
end
while value < -math.pi*2 do
value = value + math.pi * 2
end
Or - find or implement a version of fmod in lua.
Here is the corrected code (you can minify it):
math={}
math.fact = function(b)
if(b==1)or(b==0) then
return 1
end
local e=1
for c=b,1,-1 do
e=e*c
end
return e
end
math.pow = function(b,p)
local e=b
if(p==0) then
return 1
end
if(p<0) then
p=p*(-1)
end
for c=p,2,-1 do
e=e*b
end
return e
end
math.cos = function(b,p)
local e=1
b = math.correctRadians(b)
p=p or 10
for i=1,p do
e=e+(math.pow(-1,i)*math.pow(b,2*i)/math.fact(2*i))
end
return e
end
math.pi = 3.1415926545358
math.correctRadians = function( value )
while value > math.pi*2 do
value = value - math.pi * 2
end
while value < -math.pi*2 do
value = value + math.pi * 2
end
return value
end
interactive lua run:
imac:~ root$ lua -i temp.lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print( math.cos( 90 ) )
-0.44807359244883
>

Resources