Is it possible to write the following code in VBA Excel 2016 more briefly?
Select Case NemberOfProduct(i)
Case 1
CounterPlot(1) = CounterPlot(1) + 1
SumPureRate(1) = SumPureRate(1) + H(i)
Case 2
CounterPlot(2) = CounterPlot(2) + 1
SumPureRate(2) = SumPureRate(2) + H(i)
Case 3
CounterPlot(3) = CounterPlot(3) + 1
SumPureRate(3) = SumPureRate(3) + H(i)
.
.
Case 12
CounterPlot(12) = CounterPlot(12) + 1
SumPureRate(12) = SumPureRate(12) + H(i)
End Select
You could either manualy use NemberOfProduct(i) expresion as value.
So your whole select case could be replaced with
CounterPlot(NemberOfProduct(i)) = CounterPlot(NemberOfProduct(i)) + 1
SumPureRate(NemberOfProduct(i)) = SumPureRate(NemberOfProduct(i)) + H(i)
Or even more simplify it by storing NemberOfProduct(i) inside variable and then use it.
Dim n as Integer
n = NemberOfProduct(i)
CounterPlot(n) = CounterPlot(n) + 1
SumPureRate(n) = SumPureRate(n) + H(i)
Related
I have a function in MS SQL Server just like this:
UPDATE r
SET
monthly =
(
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = #type_rel_aux
AND h.hcar_day > #date_month_before
AND h.hcar_day <= #date_base
)
FROM #Month_Table r
WHERE type = 1;
and thats the result (after update):
Seq monthly
2 102471,34
1 -5129,46
3 -29841,23
4 0
But when I execute the same update in a fuction in PostgreSQL, all the rows get the same value:
UPDATE Month_Table
SET variacao_mes_rs = (
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = v_type_rel_aux
AND h.hcar_day > v_date_month_before
AND h.hcar_day <= v_date_base) FROM Month_Table r WHERE type = 1;
Result (after update), all the same value of Seq 3:]
Seq monthly
1 -29841,23
2 -29841,23
3 -29841,23
4 -29841,23
I don't see the cause of the problem...
Does PostgreSQL have different rules on UPDATE?
Can anyone help me?
Remove the FROM clause from Postgres:
UPDATE Month_Table r
SET variacao_mes_rs = (
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = v_type_rel_aux
AND h.hcar_day > v_date_month_before
AND h.hcar_day <= v_date_base)
WHERE type = 1;
The FROM clause in an UPDATE behaves differently in the two databases, as you have discovered.
I am trying to make a simple VBA in Excel, to copy some data I am trying to regroup. I seems to work good for some part, but it skips a row and column everytime! The problem must be somewhere in the double For..Next.. I am using, but I can't find it:
The result I get:
For i = 1 To AantalPag 'HIERRR
GezSite = BeginCel.Offset(i + 1) 'HIERRR
For iweek = 1 To AantalWeek
GezWeek = BeginCel.Offset(0, iweek)
For i2 = 1 To AantalWeekData
If BeginCelData.Offset(i2 - 1) = GezWeek Then
For i3 = 1 To AantalSitesData
If BeginCelData.Offset(0, i3) = GezSite Then
Sommetje = Sommetje + BeginCelData.Offset(i2 - 1, i3 - 1)
Else
i3 = i3 + 1
End If
Next i3
'BeginCel.Offset(i, iweek) = Sommetje
'Sommetje = 0
Else
i2 = i2 + 1
End If
Next i2
BeginCel.Offset(i, iweek) = Sommetje
Sommetje = 0
iweek = iweek + 1
Next iweek
i = i + 1
Next i
The code below will print the numbers from 1 to 100 in the Immediate window.
For n = 1 to 100
Debug.Print n
Next n
The code below will print every other number in the Immediate window. This is because n is incremented both by n = n + 1 and then again by Next n.
For n = 1 to 100
Debug.Print n
n = n + 1
Next n
I don't understand why I cannot compare an array in VBA. I created an array that starts 0 1 2 3. I added a comparison due to subscript errors trying to compare 0 to 0 - 1 so it can only start comparisons at 1 and continue. Now I'm receiving a type Mismatch 13 and I can't figure out why the data type is different/not working. I'm guessing i in a for loop is not considered an int or something?
It fails at CoordinatesArray(i) = CoordinatesArray(i-1)
Code:
For i = 0 To NumLines - 1
coordx1 = (vLines(12 * i + 6))
coordy1 = (vLines(12 * i + 7))
coordz1 = (vLines(12 * i + 8))
CoordinatesArray(i) = Array(coordx1, coordy1, coordz1)
If i > 0 Then
If CoordinatesArray(i) = CoordinatesArray(i - 1) Then
coordx1 = (vLines(7))
You will need to compare each value in the jagged array separately:
For i = 0 To NumLines - 1
coordx1 = (vLines(12 * i + 6))
coordy1 = (vLines(12 * i + 7))
coordz1 = (vLines(12 * i + 8))
CoordinatesArray(i) = Array(coordx1, coordy1, coordz1)
If i > 0 Then
If CoordinatesArray(i)(1) = CoordinatesArray(i - 1)(1) And _
CoordinatesArray(i)(2) = CoordinatesArray(i - 1)(2) And _
CoordinatesArray(i)(3) = CoordinatesArray(i - 1)(3) Then
coordx1 = (vLines(7))
I have written the following code in VBA and I get a Type Mismatch error in the conditions of the ;SUMPRODUCT;.
For Day = 1 To DaysTill
For m = 0 To Rot
For n = 0 To StepsNumber
RArray((Day - 1) * (Rot + 1) + m, n) = Application.WorksheetFunction.SumProduct(--(Range("HT12:HT1048576") <= RotArray((Day - 1) * (Rot + 1) + m)), --(Range("HS12:HS1048576")) = Day, Range("HT12:HT1048576"), Range("HV12:HV1048576").Offset(, n)) * PriceStepArray(n)
Next n
Next m
Next Day
Does anyone know how to fix this?
This code doesn't find the correct output
for say n= 1 (although it gives the correct output for say n= 2,3,4..etc.)
if we put n= 1 to find x then the i loop will continue from 1 to 0, hence the first term in x should vanish and leftover should be the second term 5; but it gives 0 ?
Is there any limitation on the input n to run the for loop ?I would appreciate any help.
Function math(n As Integer) As Double
Dim i As Integer
Dim x As Double
For i = 1 To n - 1
x = (n - 1) * 2 + 5
sum = sum + x
Next i
math = sum
End Function
Why not simply:
Function math(n As Integer) As Double
Math = ((n - 1) * 2 + 5) * Abs((n - 1) - (n = 1))
End Function
???
if the answer is correct then Math = (n * 2 + 3) * Abs((n - 1) - (n = 1)) would be easier to understand and make much more sense
In the for loop, if you don't precise the Step, the variable will only increment by 1.
And here, you start at 1 to go to 0, so the loop won't execute, you need to test n to cover both cases :
Function math(n As Integer) As Double
If n < 0 Then Exit Function
Dim i As Integer
Dim x As Double
Dim Summ As Double
Select Case n
Case Is > 1
For i = 1 To n - 1
x = (i - 1) * 2 + 5
Summ = Summ + x
Next i
Case Is = 1
Summ = (n - 1) * 2 + 5
Case Is = 0
Summ = 5
Case Else
MsgBox "This case is not supported", vbInformation + vbOKOnly
Exit Function
End Select
math = Summ
End Function
If n = 1, you end up with For i = 1 To 0 which is incorrect and
should be expressed For i = 1 To 0 STEP -1.
So I suggest you add the STEP BYand make sure it is either 1 to -1 depending on N.