将两个子过程重构为一个并将参数作为参数传递

时间:2015-10-20 16:19:34

标签: excel vba excel-vba

这是我重构为一个过程的两个子过程,它将通过参数传递参数。

Sub ImportCNR()

MyPath = Range("b2")                                'Defines cell that contains path to source that have been saved down
Workbooks.Open (MyPath)                             'Opens workbook that have been saved down
Set tempbook = ActiveWorkbook                       'Names  workbook for future closing
LR = Range("A65000").End(xlUp).Row                  'finds last row in edits



ReDim aCNR(1 To LR, 1 To 4)
cRow = 0


 cName = "Entity ID"
 CA = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "Share Class"
 cB = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "Exchange Rate"
 cC = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "Net Assets"
 cD = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column

     For r = 2 To LR
         cRow = cRow + 1
         aCNR(cRow, 1) = Sheets(1).Cells(r, CA) 'Fund Number
         aCNR(cRow, 2) = Sheets(1).Cells(r, cB) 'class
         aCNR(cRow, 3) = Sheets(1).Cells(r, cD) / Sheets(1).Cells(r, cC) 'TNA


     Next r


tempbook.Close
End Sub

Sub ImportRelationships()

MyPath = Range("b4")                                'Defines cell that contains path
Workbooks.Open (MyPath)                             'Opens workbook that have been saved down
Set tempbook = ActiveWorkbook                       'Names  workbook for future closing
LR = Range("A65000").End(xlUp).Row                  'finds last row in edits



ReDim aRel(1 To LR, 1 To 4)                         '
rRow = 0


 cName = "Hedge Entity Id"
 CA = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "entity id"
 cB = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
 cName = "share class"
 cC = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column

     For r = 2 To LR
         rRow = rRow + 1
         aRel(rRow, 1) = Sheets(1).Cells(r, CA) 'side pocket
         aRel(rRow, 2) = Sheets(1).Cells(r, cB) 'Fund
         aRel(rRow, 3) = Sheets(1).Cells(r, cC) 'class


     Next r


tempbook.Close
End Sub

到目前为止,这是我的重构:

Sub testing()
'ImpCNR (b2,"Entity ID", "Share Class","Exchange Rate","Net Assets")
ImpCNR (b2)

End Sub

Sub ImpCNR(cell As String)
'Sub ImpCNR(cell As String, cName1, cName2, cName3 As String, Optional cName4 As String)

Debug.Print (cell)
'Debug.Print (cName1)
'Debug.Print (cName2)

End Sub

为什么我的testing点在调用b2时无法打印ImpCNR (b2)

1 个答案:

答案 0 :(得分:2)

改变这个:

ImpCNR (b2)

对此:

ImpCNR "b2"

并改变这一点:

Debug.Print (cell)

对此:

Debug.Print cell