VBA functions in MS Excel - vba

I'm new to VBA, and I have a question, i.e I have a mathematical function 1 + 2x¹ + 3x² + 4x³ + ... +10x⁹ and I need to resolve it into two ways:
I can use raising operations(analog pow in Pascal) and IF statement;
without rising operations and IF statement...
I have tried this one:
Public Function test(x)
test = 1 + 2*x^1 + 3*x^2 + 4*x^3 + 5*x^4 + 6*x^5 + 7*x^6 + 8*x^7 + 9*x^8 + 10*x^9
End Function
but I think it returns the wrong answer - 2441406 with calling =test(5)
So can anyone give any advice, or help with my problem?

If you can't use VBA for this, there is a formula solution. Assuming your variable x is in cell A1, you would use this formula in another cell (I used B1):
=SUMPRODUCT(ROW($1:$10)*A1^(ROW($1:$10)-1))
When A1 = 5, it returned 23803711 as expected.

You will need * as the multiper:
Public Function test(x)
test = _
1 _
+ 2 * (x ^ 1) _
+ 3 * (x ^ 2) _
+ 4 * (x ^ 3) _
+ 5 * (x ^ 4) _
+ 6 * (x ^ 5) _
+ 7 * (x ^ 6) _
+ 8 * (x ^ 7) _
+ 9 * (x ^ 8) _
+ 10 * (x ^ 9)
End Function

Related

Fetching Data from Bloomberg in VBA

I am working on a VBA project that fetches financial data from Bloomberg and calculates returns.
At the moment data for 2020 to 2022 is available.
How can I put a dynamic check in the code that checks data for 2023 is available and if it is then updates the calculation using data from 2021 to 2023? Thank you!!
Following is the code I am using right now:
Function getTotalGrowth(k As Integer)
Dim bbg As New BBG_Function
Dim gdpGrowth As Variant
ReDim indexData(5, 2)
indexData(1, 1) = "ECGDEU 20 Index"
indexData(2, 1) = "ECGDEU 21 Index"
indexData(3, 1) = "ECGDEU 22 Index"
indexData(4, 1) = "XYZ Curncy"
indexData(5, 1) = "ABC Index"
indexData(1, 2) = "ECGDUS 20 Index"
indexData(2, 2) = "ECGDUS 21 Index"
indexData(3, 2) = "ECGDUS 22 Index"
indexData(4, 2) = "XYZ Curncy"
indexData(5, 2) = "ABC Index"
gdpGrowth = ((1 + bbg.BDP(indexData(1, k), "PX_LAST") / 100) * (1 + bbg.BDP(indexData(2, k),
"PX_LAST") / 100) * (1 + bbg.BDP(indexData(3, k), "PX_LAST") / 100) ^ 3) ^ (1 / 5) - 1
End Function
How about this:
Dim y as Long, yStart as Long
if bbg.BDP("ECGDEU 23 Index", "PX_LAST") = 0 then
yStart=20
else
yStart=21
end if
for y=yStart to yStart+2
indexData(1, 1) = "ECGDEU " & y & " Index"
indexData(2, 1) = "ECGDEU " & y & " Index"
...
next

Index exceeds the number of array elements (1)

I trying to use matlab to solve a polynomial with variable parameters. I am using fsolve. The code is given below:
function my_fsolve3()
% Constant Parameters
A = 6.24;
B = 5.68e-5;
C = 1.7e-3;
D = 6.55e-8;
E = 5.3e-8;
F = 9.46e-1;
t = [0;600;1200;1800;2400;3000;10200;17400;24600;31800;...
39000;46200;53400;60600;67800;75000;82200;89400;96600;103800;...
111000;118200;125400;132600;139800;147000;154200;161400;168600;175800;183000];
G = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;....
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
H = [0.00;3.34E-01;6.79E-01;1.04E+00;1.41E+00;6.00E+00;1.07E+01;1.56E+01;2.07E+01;2.59E+01;...
3.14E+01;3.67E+01;4.18E+01;4.66E+01;5.09E+01;5.51E+01;5.90E+01;6.23E+01;6.56E+01;6.87E+01;...
7.12E+01;7.36E+01;7.59E+01;7.78E+01;7.95E+01;8.11E+01;8.24E+01;8.24E+01;8.23E+01;8.21E+01;8.20E+01];
I = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;...
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
J = [1.78E-07;7.41E-06;9.33E-06;1.20E-05;1.05E-05;1.74E-05;3.72E-05;3.55E-05;1.00E-04;4.07E-02;...
2.45E-01;6.17E-01;1.32E+00;2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;2.09E+00;1.82E+00;...
1.58E+00;2.29E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;1.12E+00;1.00E+00;8.51E-01];
K = [9.75;8.13;8.03;7.92;7.98;7.76;7.43;7.45;7.00;4.39;...
3.61;3.21;2.88;2.64;2.63;2.62;2.74;2.86;2.68;2.74;...
2.8;2.64;2.79;2.95;3.05;3.07;3.12;3.06;2.95;3.00;3.07];
for i = 1:length(t)
s(i) = fsolve(#(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
+ A.*x(i) + A.*B))-((I(i).*C.*x(i))/(x(i).^2 + C.*x(i) + C.*D))- 2.*((I(i).*C.*D)/(x(i).^2 ...
+ C.*x(i) + C.*D))- E/x(i), F);
end
L = 6 - log10(s);
plot(t,L)
hold on
plot (t,K,'v')
xlabel('Time (sec)')
ylabel('pH')
hold off
legend('pH-Mod','pH-Exp')
end
I do not know how can I ensure that the index value does not exceed the number of the array elements. In line 34, I indexed to the end of the independent variable, thinking that it will prevent the error. The error message is
Index exceeds the number of array elements (1).
Error in
my_fsolve3>#(x)x(i)+2.*G(i)-((H(i).*A.*x(i))/(x(i).^2+A.*x(i)+A.*B))-2.*((H(i).*A.*B)/(x(i).^2+A.*x(i)+A.*B))-((I(i).*C.*x(i))/(x(i).^2+C.*x(i)+C.*D))-2.*((I(i).*C.*D)/(x(i).^2+C.*x(i)+C.*D))-E/x(i)
(line 36)
s(i) = fsolve(#(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in my_fsolve3 (line 36)
s(i) = fsolve(#(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Your anonymous function has x as an argument which is initialized to the scalar F.
You try to access x(i) which fails for i>1 because x is scalar. Solution: replace x(i) by x.
s(i) = fsolve(#(x) x + 2.* G(i) - ((H(i).* A.*x)/(x.^2 + A.*x + A.*B))- 2.*((H(i).*A.*B)/(x.^2 ...
+ A.*x + A.*B))-((I(i).*C.*x)/(x.^2 + C.*x + C.*D))- 2.*((I(i).*C.*D)/(x.^2 ...
+ C.*x + C.*D))- E/x, F);
By the way, you do not need the dot operator in multiplications of scalars, a simple * will do in your code.

I need to solve an implicit equation in VBA

I want to give the other parameters that are mentioned in the function, and get a solution for a (the angle), but I get error: "invalid procedure call or argument" Run-time error 5.
I need to call the function in excel worksheet. It is a pretty long equation. Also, it could be that I enter a infinite loop but I don't know how to avoid that.
Function calculateangle(r, h, C, g, d, m, t, x, y As Single) As Single
Dim a As Single
a = 0
While y <> (d + r - r * Cos(a) + (x - (t - r + r * Sin(a))) * Tan(a) - (g
/ (2 * ((((C * m * (2 * g * (h - (d + r - r * Cos(a)))) ^
(1 / 2)) + m * (2 * g * (h - (d + r - r * Cos(a)))) ^ (1 / 2)) / (m +
0.04593)) ^ 2) * (Cos(a)) ^ 2)) * (x - (t - r + r * Sin(a))) ^ 2)
a = a + 0.01
Wend
MsgBox Round(a, 2)
End Function
One obvious issue is that you are using a Function but not returning a value.
This really is a complex piece of spaghetti! However, I suggest an approach like below which will help separate out various bits and thus make it easier to do debugging
Function calculateangle(<...all the bits ...>) As Double
Dim a As Double
Dim tTolerance as Double
dim f1 as Double ' sub sections to help untangle the spaghetti
Dim f2 as Double
Dim f3 as Double
Dim fFinal as Double
Dim tWithinTolerance as Boolean
tWithinTolerance = false
a = 0
tTolerance = 0.01
While not tWithinTolerance
f1 = d + r - r * Cos(a)
f2 = m*2*g*(h - f1)
f3 = x - (t - r + r * Sin(a))
fFinal = (f1 + f3 * Tan(a) - (g / (2 * ((((C * f2) ^
(1 / 2)) + f2 ^ (1 / 2)) / (m + 0.04593)) ^ 2) * (Cos(a)) ^ 2)) * f3 ^ 2)
tWithinTolerance = (Abs(y - fFinal) < tTolerance)
a = a + 0.01
Wend
Calculateangle = a ' note how this sets a return value for the function
End Function
I have left the rounding (which is a presentation issue) to the code that calls this function - this way you can display the answer to whatever level of detail you want!
(apologies if I have mangled any of the calculation on the way through - but you get the concept!)
For the author and those who want to deal with his solitaire. I hope I did not confuse anything in parentheses and simplifications.
Do
vCosA = Cos(a)
vCosADR = d + r * (1 - vCosA) ' d + r - r * vCosA '
vCosMGHADR = m * (2 * g * (h - vCosADR))
vSinAXTR = (x - (t - r * (1 - Sin(a)))) ' - r + r * Sin(a)
'((C * vCosMGHADR) + vCosMGHADR) == ((C + 1) * vCosMGHADR)
If (y = _
(vCosADR + vSinAXTR * Tan(a) - _
(g / _
(2 * _
( _
( _
((C + 1) * vCosMGHADR) / _
(m + 0.04593) _
) ^ 2 _
) * (vCosA ^ 2) _
) _
) * vSinAXTR ^ 2 _
)) Then Exit Do ' *** EXIT DO ***
a = a + 0.01
Loop

(VBA) Square equalities - Sub or function not defined

I'm facing some problems with my exercise test in uni, but after editing some code it seems like something is wrong there.
Public Function Uzd(x) As String
If x = 1 Or x = 2 Then
Uzd = (2 * x + 3) / sqrt(x ^ 2 + 3 * x + 2)
Else
Uzd = "Incorrect data"
End If
End Function
After running code I get - Sub or function not defined
The function you're trying to use is located in the VBA type library, in the Math module:
You can avoid such typos by using IntelliSense and fully-qualifying the global-scope functions you use:
Uzd = (2 * x + 3) / Math.Sqr(x ^ 2 + 3 * x + 2)

vb basic text is not a member of double

I have a problem of conversion. When I write txta1 = txta1.Text, it shows "text is not a member of double." I'm not sure why, can someone explain this to me?
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim treatments As String
Dim ssb, ssw, msb, msw, sigmaA, sigmaB, sigmaC, sigmaabc, squaresigmaA, squaresigmaB, squaresigmaC, squaresigmaabc, f As Double
Dim txta1, txta2, txta3, txta4, txta5, txtb1, txtb2, txtb3, txtb4, txtb5, txtc1, txtc2, txtc3, txtc4, txtc5 As Double
'Data Input
txta1 = txta1.Text
treatments = lblTreatments.Text
'Calaculation
sigmaA = txta1 + txta2 + txta3 + txta4 + txta5
sigmaB = txtb1 + txtb2 + txtb3 + txtb4 + txtb5
sigmaC = txtc1 + txtc2 + txtc3 + txtc4 + txtc5
sigmaabc = sigmaA + sigmaB + sigmaC
squaresigmaA = txta1 ^ 2 + txtb2 ^ 2 + txtb3 ^ 2 + txtb4 ^ 2 + txtb5 ^ 2
squaresigmaB = txtb1 ^ 2 + txtb2 ^ 2 + txtb3 ^ 2 + txtb4 ^ 2 + txtb5 ^ 2
squaresigmaC = txtc1 ^ 2 + txtc2 ^ 2 + txtc3 ^ 2 + txtc4 ^ 2 + txtc5 ^ 2
squaresigmaabc = squaresigmaA + squaresigmaB + squaresigmaC
ssb = ((sigmaA) ^ 2 / 5 + (sigmab) ^ 2 / 5 + (sigmac) ^ 2 / 5) - (sigmaabc) ^ 2 / 15
ssw = squaresigmaabc - ((sigmaA) ^ 2 / 5 + (sigmab) ^ 2 / 5 + (sigmac) ^ 2 / 5)
msb = ssb / (3 - 1)
msw = ssw / (15 - 1)
f = msb / msw
'Data output
lblSSB.Text = ssw
lblSSW.Text = ssw
lblMSB.Text = msb
lblMSW.Text = msw
lblF.Text = f
End Sub
As the compiler is trying to tell you, you declared txta1 as a Double, which has no Text property.
You should choose different names for your controls and your local variables.
You've declared txta1 as a Double in line 4. I assume you also have a TextBox called txta1 on your form. Use a different name like txta1Value for the Double, so it doesn't clash with the name of the TextBox. Then you can say
txta1Value = Double.Parse(txta1.Text)
(This will blow up if someone types something other than a valid Double; how you want to deal with that is up to you.)