Excel-VBA:将字符串变量传递给另一个过程以在循环中为其分配值

时间:2019-01-02 10:26:15

标签: excel vba excel-vba

我想将字符串变量"Sheets(2).Cells(i, 41).Value"传递给过程Sub prcdTariff_calc,该过程将在循环中为该变量分配一个值,以便将其传递给另一个函数Function Tariff_calc。 问题出在Sub prcdSum_year_IR()上。我已经尝试过在过程中使用数据类型(字符串/双精度),还尝试过CDbl(quality)等……是否有办法将变量quality传递到{{1}中的循环中}?感谢您的建议。

Sub prcdTariff_calc

1 个答案:

答案 0 :(得分:0)

我通过在过程循环内声明变量a,b,c来修改代码,从而解决了我的问题。现在工作正常。

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