[英]Passing a string variable to another procedure to assign a value to it in a loop
I want to pass aa string variable "Sheets(2).Cells(i, 41).Value"
to the procedure Sub prcdTariff_calc
that will assign in a loop a value to that variable in order to pass it on to another function, Function Tariff_calc
. 我想将字符串变量
"Sheets(2).Cells(i, 41).Value"
传递给过程Sub prcdTariff_calc
,该过程将在循环中为该变量分配一个值,以便将其传递给另一个函数Function Tariff_calc
。 The problem is with Sub prcdSum_year_IR()
. 问题出在
Sub prcdSum_year_IR()
。 I tried already to play around with the datatypes (String / Double) in the procedures and also CDbl(quality)
etc... Is there a way to pass on the variable quality
into the loop in Sub prcdTariff_calc
? 我已经尝试过在过程中使用数据类型(字符串/双
Sub prcdTariff_calc
CDbl(quality)
以及CDbl(quality)
等...是否有办法将变量quality
传递给Sub prcdTariff_calc
的循环? Thanks for advice. 谢谢你的建议。
Option Explicit
Dim price As String
Dim i As Byte
Public sum_tariff As Double
Dim tariff As Double, period As Double, quality As Double
Function Tariff_calc(a As Double, Optional b As Double = 1, Optional c As Double = 1) As Double
Tariff_calc = a * b * c
Debug.Print b
End Function
Function Price_calc(a As Double, Optional b As String = 1, Optional c As Double = 1) As String
Price_calc = Format(a * b * c, "0.00 €/MWh")
End Function
Sub prcdTariff_calc(Optional a As Double = 1, Optional b As Double = 1, Optional c As Double = 1)
For i = 2 To 43
If ThisWorkbook.Sheets(2).Cells(i, 1).Value = 1 Then
a = Sheets(2).Cells(i, 19).Value
'I also tried b = val(quality) here
'I experimented here by referring to newly created procedure Sub [prcdquality() quality = Sheets(2).Cells(i, 41) End Sub] and back here then b=quality; and it worked
sum_tariff = sum_tariff + Tariff_calc(a, Val(b), c) 'I tried val(b), but it still shows 0 in Debug.Print b above.
price = Price_calc(a, CStr(b), c)
lblPrice.Caption = lblPrice.Caption & price & vbCrLf
End If
Next i
End Sub
Sub prcdSum_year_Firm()'works fine
prcdTariff_calc
lblUnitCost.Caption = Format(sum_tariff, "0.00 €/MWh")
End Sub
Sub prcdSum_year_IR()
quality = Sheets(2).Cells(i, 41)
prcdTariff_calc b:=quality
lblUnitCost.Caption = Format(sum_tariff, "0.00 €/MWh")
End Sub
I resolved my problem by modifying the code by declaring the variables a,b,c inside the loop of procedure. 我通过在过程循环内声明变量a,b,c来修改代码,从而解决了我的问题。 It works fine now.
现在工作正常。
Function Tariff_calc(a As Double, Optional b As Double = 1, Optional c As Double = 1) As Double
Tariff_calc = a * b * c
End Function
Function Price_calc(a As Double, Optional b As String = 1, Optional c As Double = 1) As String
Price_calc = Format(a * b * c, "0.00 €/MWh")
End Function
Sub prcdTariff_calc(Optional a As Double = 1, Optional b As Double = 1, Optional c As Double = 1)
For i = 2 To 43
If ThisWorkbook.Sheets(2).Cells(i, 1).Value = 1 Then
a = Sheets(2).Cells(i, 19).Value 'annual tariff
If optIR.Value Then
b = Sheets(2).Cells(i, 41) 'quality factor
End If
If optQuarter.Value Then
c = Sheets(2).Cells(i, 21 + q) 'period factor Quarter
ElseIf optMonth.Value Then
c = Sheets(2).Cells(i, 28 + m) 'period factor Month
ElseIf optDay.Value Then
c = Sheets(2).Cells(i, 27)
End If
sum_tariff = sum_tariff + Tariff_calc(a, b, c)
price = Price_calc(a, CStr(b), c)
lblPrice.Caption = lblPrice.Caption & price & vbCrLf
End If
Next i
lblUnitCost.Caption = Format(sum_tariff, "0.00 €/MWh")
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.