使用范围作为变量而不是变量

时间:2014-12-03 19:45:05

标签: excel excel-vba vba

是否可以将tu作为范围变量而不是函数THours中的变量,后者是由函数Hours构建的?

Public Function Hours(x As Range, y As Range, w As Range) As Double

Dim n As Integer
Dim z As Double


For n = 1 To y.Columns.count

    If (y.Cells(1, n) > 0) And (y.Cells(1, n) <> "") Then
        z = z + (w.Cells(1, n) * x.Cells(1, n) / y.Cells(1, n))
    End If


Next n

Hours = z
End Function

Public Function THours(t As Variant, u As Variant, w As Variant) As Double
 Dim z As Double
 Dim r As String
 Dim Ressource As Variant

    z = 0
    ' this is just an array
    Ressource = get_sheet_names()

    With ActiveWorkbook
        For i = LBound(Ressource) To UBound(Ressource)
           r = Ressource(i)
           z = z + Hours(.Sheets(r).Range(t), .Sheets(r).Range(u), .Sheets(r).Range(w))
        Next i
    End With

    THours = z
End Function

我很感激每一个答案。

由于

1 个答案:

答案 0 :(得分:0)

我认为这可能是您所寻找的,但很难根据您提供的少量信息来判断。

此外,我对您的问题感到困惑,因为在Hours函数中,代码正在执行您想要在THours函数中执行的操作。

Public Function Hours(x As Range, y As Range, w As Range) As Double
    Dim n                       As Integer
    Dim z                       As Double

    For n = 1 To y.Columns.Count
        If (y.Cells(1, n) > 0) And (y.Cells(1, n) <> "") Then
            z = z + (w.Cells(1, n) * x.Cells(1, n) / y.Cells(1, n))
        End If
    Next n

    Hours = z
End Function

Public Function THours(t As Range, u As Range, w As Range) As Double
    Dim z                       As Double
    Dim Ressource               As Variant

    z = 0
    ' this is just an array
    Ressource = get_sheet_names()

    With ActiveWorkbook
        For i = LBound(Ressource) To UBound(Ressource)
            r = Ressource(i)
            z = z + Hours(t.Value, u.Value, w.Value)
        Next i
    End With

    THours = z
End Function