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

这是两个子程序,我重构成一个程序,将通过parameter passing参数。

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 

为什么当我调用ImpCNR (b2)时, testing子将无法打印b2

改变这个:

 ImpCNR (b2) 

对此:

 ImpCNR "b2" 

并改变这一点:

 Debug.Print (cell) 

对此:

 Debug.Print cell