I am working on a complex Excel VBA application and trying to use Countifs function. The problem is it is giving me a Type mismatch error I am unable to point a finger to. It would be too complex to paste the entire code here but I am pasting what I think are the relevant lines of the code
Public NumSecTypes As Integer
Public NumSecurities As Integer
.
.
.
Dim i as Integer
Dim AvgPoSSizeCountif As Double
Dim SecType As String
.
.
.
Sheets("Output").Select
AvgPoSSizeCountif = WorksheetFunction.CountIfs(Range(Cells(i + 1, (17 + 2 * NumSecTypes)), Cells(i + 1, (16 + 2 * NumSecTypes + NumSecurities))), Range(Cells(6, (17 + 2 * NumSecTypes)), Cells(6, (16 + 2 * NumSecTypes + NumSecurities))), SecType, Range(Cells(i + 1, (21 + 2 * NumSecTypes + 2 * NumSecurities)), Cells(i + 1, (20 + 2 * NumSecTypes + 3 * NumSecurities))), ">0")
Can anyone tell me what is wrong with this? The error is in the "AvgPoSSizeCountif = ..." line
Many thanks in advance. Any help would be appreciated. Kindly let me know if you need any other information
The syntax for the CountIfs function should be
CountIfs( criteria_range1, criteria1, [criteria_range2, criteria2, ...])
It looks like you are missing the criteria that matches Parameter 1. This is what your function looks like below:
WorksheetFunction.CountIfs(
// Parameter 1
Range(Cells(i + 1, (17 + 2 * NumSecTypes)), Cells(i + 1, (16 + 2 * NumSecTypes + NumSecurities))),
// Parameter 2
Range(Cells(6, (17 + 2 * NumSecTypes)), Cells(6, (16 + 2 * NumSecTypes + NumSecurities))),
// Parameter 3
SecType,
// Parameter 4
Range(Cells(i + 1, (21 + 2 * NumSecTypes + 2 * NumSecurities)), Cells(i + 1, (20 + 2 * NumSecTypes + 3 * NumSecurities))),
// Parameter 5
">0"
)
Related
I've been using terraform for a while but by no means am I experienced with code. I've been using the below block to generate descriptions for my hosts.
resource "vra_deployment" "test_instance" {
count = var.instance_count
catalog_item_name = var.catalog_item_name
description = "${var.hostname_prefix}${format("%02d", count.index+2)}-${replace(lower(var.region), "-","")}"
This will give me a host with a description like host02-region. At the moment I've only used this with the count variable set to 1.
I'd like to make it so that if I set the count variable to 2, it will build 2 hosts with the description only in even numbers, ie host02-region and host04-region.
Would anyone be able to show me how to do this please?
This is just a basic math question. I would change count.index+2 to (count.index + 1) * 2.
(0 + 1) * 2 = 2
(1 + 1) * 2 = 4
(2 + 1) * 2 = 6
(3 + 1) * 2 = 8
I just wrote this code to perform an iterative calculation. It finds the X,Y coordinates where an unknown parabola is tangent to a known circle. It is based on other iterative functions I've written that have worked flawlessly. I'm stumped as to what the issue is. Here is the code:
Public Function Jext(PA As Double, Xcl As Double, Ycl As Double, Ctr As Double, Rnl As Double, Finl As Double) As Double
Pi = 3.14159265358979
tol = 0.00000001
Dim x(20) As Double
Dim Yc(20) As Double
Dim Yp(20) As Double
Dim A(20) As Double
Dim Diff(20) As Double
Dim It As Integer
Dim hF As Double
Dim Sf As Double
'Xcl is the horizontal position of the root radius center line
'Ycl is the vertical position of the root radius center line
'Ctr is root radius. It's an approximation of the trochoid and is typically the cutter tip radius
'Rnl is the radius to the point where the load line intersects the tooth centerline. It is the apex of the parabola
'x is the horizontal position that is common to the parabola and circle. It is the independent variable
'Yc is the vertical position on the circle at point x
'Yp is the vertical position on the parabola at point x
'A is the leading term of the parabolic equation
'Diff is the calculated difference in vertical positions of circle and parabola
'Set initial guess values. x(0) is 5% of the radius (left edge of circle) and x(1) is 95% of the radius(bottom of the circle)
x(0) = (Xcl - Ctr) + 0.05 * Ctr
x(1) = (Xcl - Ctr) + 0.95 * Ctr
Yc(0) = Ycl - (Ctr ^ 2 - (x(0) - Xcl) ^ 2) ^ 0.5
Yc(1) = Ycl - (Ctr ^ 2 - (x(1) - Xcl) ^ 2) ^ 0.5
A(0) = Tan(WorksheetFunction.Acos((Xcl - x(0)) / Ctr) + Pi / 2) / (2 * x(0))
A(1) = Tan(WorksheetFunction.Acos((Xcl - x(1)) / Ctr) + Pi / 2) / (2 * x(1))
Yp(0) = A(0) * x(0) ^ 2 + Rnl
Yp(1) = A(1) * x(1) ^ 2 + Rnl
Diff(0) = Yp(0) - Yc(0)
Diff(1) = Yp(1) - Yc(1)
For It = 1 To 19 Step 1
x(It + 1) = x(It) - (Diff(It) - 0) * (x(It - 1) - x(It)) / (Diff(It - 1) - Diff(It))
Yc(It + 1) = Ycl - (Ctr ^ 2 - (x(It + 1) - Xcl) ^ 2) ^ 0.5
A(It + 1) = Tan(WorksheetFunction.Acos((Xcl - x(It + 1)) / Ctr) + Pi / 2) / (2 * x(It + 1))
Yp(It + 1) = A(It + 1) * x(It + 1) ^ 2 + Rnl
Diff(It + 1) = Yp(It + 1) - Yc(It + 1)
If Abs(Diff(It + 1)) < tol Then Exit For
Debug.Print Diff(It + 1)
Next It
hF = Rnl - Yp(t + 1)
Sf = 2 * x(t + 1)
Jext = 1 / (Cos(Finl) / Cos(PA) * (6 * hF / Sf ^ 2 - Tan(Finl) / Sf))
End Function
I put a stop at the "Next It" line to check the values as it went through the iterative loops. When I execute the code, all of the values are as expected and the value of Abs(Diff(It+1)) is not small enough to exit the for loop in the IF statement. I put the Debug.Print statement in there to make sure it was getting that far in the code and it did print Diff(It+1). So it executes everything to that point. Then when I continue the function it just stops and returns a #VALUE error in the spreadsheet. I've no idea why it won't continue the for loop. Anyone see something I've missed?
I have this code from an old book that does not work in my Excel. It is supposed to calculate the slope and intercept of a Deming regression from a range of numbers, x and y
Function Deming(XValues, YValues)
Dim MeanX(), MeanY()
Ncells = XValues.Count
ReDim MeanX(Ncells / 2), MeanY(Ncells / 2)
For x = 2 To Ncells Step 2
MeanX(x / 2) = (XValues(x - 1) + XValues(x)) / 2
MeanY(x / 2) = (YValues(x - 1) + YValues(x)) / 2
SumX = SumX + MeanX(x / 2): SumY = SumY + MeanY(x / 2)
SumX2 = SumX2 + (MeanX(x / 2)) ^ 2
SumY2 = SumY2 + (MeanY(x / 2)) ^ 2
SumXY = SumXY + MeanX(x / 2) * MeanY(x / 2)
SumDeltaX2 = SumDeltaX2 + (XValues(x - 1) - XValues(x)) ^ 2
SumDeltaY2 = SumDeltaY2 + (YValues(x - 1) - YValues(x)) ^ 2
Next
XBar = SumX / N: YBar = SumY / N
Sx2 = (N * SumX2 - SumX ^ 2) / (N * (N - 1))
Sy2 = (N * SumY2 - SumY ^ 2) / (N * (N - 1))
Sdx2 = SumDeltaX2 / (2 * N)
Sdy2 = SumDeltaY2 / (2 * N)
rPearson = (N * SumXY - SumX * SumY) / Sqr((N * SumX2 - SumX ^ 2) * (N *
SumY2 - SumY ^ 2))
lambda = Sdx2 / Sdy2
U = (Sy2 - Sx2 / lambda) / (2 * rPearson * Sqr(Sx2) * Sqr(Sy2))
Slope = U + Sqr(U ^ 2 + 1 / lambda)
Intercept = YBar - Slope * XBar
Deming = Array(Slope, Intercept)
End Function
Does this have a bad syntax or not?
First this is not old code, this is simply bad code.
Anything in VBA, which does not compile, when someone writes Option Explicit on the top of the Module/Worksheet is a bad syntax. This is a rule of a thumb. And in the case of the code, if this one is pasted to the editor the following line is red:
rPearson = (N * SumXY - SumX * SumY) / Sqr((N * SumX2 - SumX ^ 2) * (N *
SumY2 - SumY ^ 2))
This is because it should be in 1 line, and not in 2.
So, concerning the question - how to update it - as a first step, make sure the code compiles with Option Explicit on top (Option Explicit statement). So, write Option Explicit and then go to Debug>Compile on the VBEditor's ribbon. VBeditor will highlight the problem. The first one is that Ncell is not defined:
Then find a way to define it, e.g. write Dim Ncells as Variant or as anything else you may consider useful on the top of the highligted line. It could be that just declaring a variable is not enough, as there is a calculation XBar = SumX / N in the code. There, N should be declared and assigned to a value. If it is only declared, it will be 0, and then a division by 0 will be an error. Thus, probably something like this should be written, depending on the logic: Dim N as Double: N = 1
Perplexed by the function of using a second = sign in vba. eg. s = Int(xVal) + (xVal = n + 1)
I had been deciphering some code and came across the following line which is perplexing me somewhat and despite some extensive research and debugging I seem to be struggling to find the answer:
s = Int(xVal) + (xVal = n + 1)
and
p(i, 3) = A(i)(s + 3 + (s = n)) + (s = n) * (p(i, 1) - p(i, 2))
My question is what is the function of the comparisons within the parentheses after the first assignment = sign?
TIA
(s = n)
If both s and n have the same value then this evaluates to True, which can be coerced to its underlying value of -1 by other arithmetic operations.
Eg:
? True * 1 '>> -1
? False * 1 '>> 0
So this:
s = Int(xVal) + (xVal = n + 1)
is like writing:
If xVal = n + 1 Then
s = Int(xVal) + -1
else
s = Int(xVal) + 0
end if
or:
s = Int(xVal) + IIf(xVal = n + 1, -1, 0)
I tried to use erfc but it says argument not optional.
An example is given below
For j = 0 To 150
f = 1
For m = 1 To j
f = f * m
Next
Application.WorksheetFunction.Erf = (Application.WorksheetFunction.Erf) + (-1) ^ j * b ^ (2 * j + 1) / ((2 * j + 1) * f)
Next
Application.WorksheetFunction.ErfC = 1 - 2 / Sqr(3.14) * Application.WorksheetFunction.Erf
MsgBox (Application.WorksheetFunction.ErfC)
xf1 = (wa + 2 * sp) * q / (4 * cl ^ 2 * 3.14 * hf)
xf2 = Exp(b ^ 2) * Application.WorksheetFunction.ErfC
xf3 = 2 * b / Sqr(3.14) - 1
xf = xf1 * (xf2 + xf3)
According to the MSDN documentation at least one parameter needs to be passed to the Erf method:
Name Required/Optional Data Type Description
Arg1 Required Variant Lower_limit - the lower bound for integrating ERF.
Arg2 Optional Variant Upper_limit - the upper bound for integrating ERF.
If omitted, ERF integrates between zero and
lower_limit.
Therefore, calling it with zero parameters (e.g. Application.WorksheetFunction.Erf) will give you an "argument not optional" error.
You also won't be able to set Erf to a value, i.e.
Application.WorksheetFunction.Erf = ...
is invalid.