2014-12-03 65 views
0

是否有可能使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 

我很感谢每一个答案。

感谢

+0

“后者由函数Hours创建......”后者是指“u”还是“THours”?您能否定义“由......构建”的含义?课程数组可以传递给函数,但是我不清楚你到底想要什么,我有这样的问题......这些函数是独立的吗?它们是从代码调用还是作为工作表函数?范围是由用户选择还是由用户选择范围参考汇总在代码中?函数应该做什么? – 2014-12-03 20:16:17

+0

嗨马克,没有第一个函数通过一个工作表,并有三列作为变量(范围),它是一个工作表函数,它在某种意义上给出了总和第二个函数Thours(总小时数)循环遍历由数组生成的人员的所有工作表名称,单位为t他“get_sheet_names()”函数(我没有写这里的代码)并使用小时函数。我的问题是,对于Thours的代码,我使用t和u作为变量变量,但是我想要有范围变量。我试图使用小时(.Sheets(r).t,.Sheets(r).u,..)。但是这不起作用 – user44155 2014-12-03 21:06:11

+0

那么必须遍历所有工作表的工作表函数不应该使用范围,因为当您在工作表中移动时,这些范围引用会发生变化。当然,除非你想从范围引用中提取地址并使用前面的地址,但是如果你使用类似C:F而不是C2:F50的方式,那么这是非常好的,因为不是所有的表都有相同的行数(我猜测)。为什么不只是以参数的形式提供你想总结的列数? – 2014-12-04 01:53:09

回答

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