在将sheetname传递给过程时input不匹配

我很抱歉,我确定我忽略了一些简单的事情,但是太多的时间已经过去了,我无法弄清楚为什么我无法将名为MAIN的工作表作为parameter passing给下面的“ Import过程的参数。 MAIN是工作簿中的工作表。

我的目标是拥有一个名为Import的通用过程,我可以在同一个工作簿中传递任何工作表,并通过简单地将工作表名称作为参数parameter passing,轻松地将以下列单元格关系存储到数组中。

 Sub Import(sheetname As Worksheet) 'Debug.Print (sheetname) cName = "Fund ID" cA = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "CUSIP" cB = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Description" cC = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Security Type" cD = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Currency" cE = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Price Date" cF = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Current Price" cG = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Prior Price" cH = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Change Price (%)" cI = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Change Price (%)" cJ = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "BPS Impact" cK = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Source" cL = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column cName = "Comment" cM = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column LastRow = Range("b65000").End(xlUp).Row For r = 2 To LastRow Row = Row + 1 aRecon1(Row, 1) = CStr(Cells(r, cA)) 'Fund ID ' added string value method 'aRecon1(Row, 1) = Cells(r, cA) 'Fund ID aRecon1(Row, 2) = Cells(r, cB) 'CUSIP aRecon1(Row, 3) = Cells(r, cC) 'Description aRecon1(Row, 4) = Cells(r, cD) 'Security Type aRecon1(Row, 5) = Cells(r, cE) 'Currency aRecon1(Row, 6) = Cells(r, cF) 'Price Date aRecon1(Row, 7) = Cells(r, cG).Value 'Current Price 'Debug.Print (Cells(r, cG).Value) Debug.Print (Cells(r, cG).Value) 'Debug.Print (Cells(r, cG)) aRecon1(Row, 8) = Cells(r, cH).Value 'Prior Price aRecon1(Row, 9) = Cells(r, cI) 'Change Price (%) aRecon1(Row, 10) = Cells(r, cJ) 'BPS Impact aRecon1(Row, 11) = Cells(r, cK) 'Source Debug.Print (Cells(r, cK)) aRecon1(Row, 12) = Cells(r, cL) Next r Sheets("Macro").Activate 

以下是如何传递工作表的简单示例

 Sub Main() GetName ThisWorkbook.Worksheets(1) End Sub Function GetName(wb As Worksheet) MsgBox wb.Name End Function 

对@罗里的问题的答案是重要的。

在你的代码中你有:

 Sub Import(sheetname As Worksheet) 

这很酷, 如果你像这样调用Import子类(只是示例代码):

 For Each oSheet in oWorkbook Import oSheet Next 

另一方面,如果你想像这样调用Import:

 strSheet = InputBox("Type the name of the sheet to import") Import strSheet 

那么你的代码需要是这样的:

 Sub Import(sheetname As String) Dim oSheet as Worksheet Set oSheet = Sheets(sheetname) 

您必须先检查您的调用例程

 Sub ROUTINE() Dim ws As Worksheet Set ws = Sheets("MAIN") Call import(ws) End Sub 

是必要的。 以下肯定会失败:

 Sub NotVeryGood() Dim ws As String ws = "MAIN" Call import(ws) End Sub 

(您的导入需要一个对象而不是对象名称