2013-09-22 43 views
1

在我的项目中,我有两个函数,每个函数都有3个表单,我将它们设置为Select Case。每个功能分为3张。这些程序是更大程序的一部分,它将数据导入Excel工作簿,然后对数据进行格式化以执行一些分析。因此,执行每个子时必须遵循一步一步的顺序。尝试将两个Select Case函数合并为一个For循环参数

原因是因为发生在一张纸上的任何事情都必须发生在Function中的其他3个事件上。例如,如果一个人被排序,那么所有3人都必须被排序。考虑到这一点,我有一个程序可以自动填充第一个函数中的B6:AH6的一些数据,并带有三个表格。

这就是:

Public Function EmployeeSheets(Index As Long) As Excel.Worksheet 

    'This function indexes all of the Employee sheets 
    'to use in various loops during he instal process 
    '@param EmployeeSheets, are the sheets to index 

    Select Case Index 

     Case 1 : Return xlWSAllEEAnnul 
     Case 2 : Return xlWSAllEEHourly 
     Case 3 : Return xlWSAllEESalary 

    End Select 

    Throw New ArgumentOutOfRangeException("Index") 

End Function 

Sub copyFormulas() 

    Dim eeRefSheets As Excel.Worksheet 

    For i As Long = 1 To 3 Step 1 

     eeRefSheets = EmployeeSheets(i) 

     With eeRefSheets 
      Dim lngLr As Long 

      lngLr = .Cells.Find(What:="*", SearchDirection:=Excel.XlSearchDirection.xlPrevious, SearchOrder:=Excel.XlSearchOrder.xlByRows).Row 

      .Range("B6:AH6").AutoFill(.Range("B6:AH" & lngLr), Excel.XlAutoFillType.xlFillDefault) 

     End With 

    Next i 

End Sub 

因此,有没有问题,一切运作良好。只是现在我必须做同样的事情在这些片:

Public Function PositionSheets(Index As Long) As Excel.Worksheet 

'This function indexes all of the Position sheets 
'to use in various loops during he instal process 
'@param PositionSheets, are the sheets to index 

Select Case Index 

    Case 1 : Return xlWSAllPositionAnnul 
    Case 2 : Return xlWSAllPositionHourly 
    Case 3 : Return xlWSAllPositionSalary 

End Select 

Throw New ArgumentOutOfRangeException("Index") 

我想要做的是,而不是写我的For循环两次,均是选择案例指数组合成一个圈,并执行自动填充。

可以这样做,还是有更好的办法?

回答

1

努力使之作为子..和使用BYREF可变参考..

Public Sub EmplNPosSheets(Index As Long, ByRef ES As Excel.Worksheet, ByRef PS As Excel.Worksheet) 

    'This function indexes all of the Employee sheets 
    'to use in various loops during he instal process 
    '@param EmployeeSheets, are the sheets to index 

    Select Case Index 

     Case 1 
      ES = xlWSAllEEAnnul 
      PS = xlWSAllPositionAnnul 
     Case 2 
      ES = xlWSAllEEHourly 
      PS = xlWSAllPositionHourly 
     Case 3 
      ES = xlWSAllEESalary 
      PS = xlWSAllPositionSalary 

    End Select 

    Throw New ArgumentOutOfRangeException("Index") 

End Sub 

要使用它,做这样的..

Dim eeRefSheets As Excel.Worksheet 
Dim posRefSheets As Excel.Worksheet 

Set eeRefSheets = Application.Worksheets(0) 
Set posRefSheets = Application.Worksheets(0) 

EmplNPosSheets(i,eeRefSheets,posRefSheets) 
+0

谢谢,我用子和试图使用:EmplNPosSheets(我,eeRefSheets,posRefSheets)。但是告诉eeRefSheets和posRefSheets在被赋值之前通过引用传递。 –

+0

@JoseM。 ..你可以给它分配虚拟值..它已更新 – matzone