解决Excel VBA中的运行时错误

我正在考虑第一次进入Excel VBA,并且有很多东西需要学习。

有了这个macros,我正在设置一个文件,允许我根据个人的名字从周报中复制/粘贴特定范围的数据。 从本质上讲,我希望能够让个人点击一个button,并自动获取数据。 以下是我到目前为止所做的一切:

小一(个人定义他们的名字):

Sub Button1_Click() Dim Name As Variant Name = InputBox("Enter your name as it appears on the report", "Enter Name") Worksheets("Summary").Range("A1").Value = Name Application.ScreenUpdating = True ActiveWorkbook.Save End Sub 

子2(macros从基于在子1中input的名称报告数据):

 Sub Report1_Click() Dim Name As Variant Set Name = Worksheets("Summary").Range("A1").Value 'check if Name is entered If Name = "" Then MsgBox ("Your name is not visible, please start from the Reference tab.") Exit Sub End If Dim SourceFile As Workbook Set SourceFile = Workbooks("filename.xlsm") 'check if source file is open If SourceFile Is Nothing Then 'open source file Set SourceFile = Workbooks.Open("C:\filename.xlsm") SourceFile.Activate Else 'make source file active SourceFile.Activate End If Dim DestFile As Variant Set DestFile = ActiveWorkbook.Worksheets("Input").Range("B2") Dim ActiveCell As Variant Set ActiveCell = Workbooks("filename.xlsm").Worksheets("Group").Range("A1") Cells.Find(What:=Name, After:=ActiveCell, LookIn:=xlFormulas _ , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False).Activate ActiveSheet.Range("$A$2:$DQ$11").AutoFilter Field:=1, Criteria1:=Name Range("A7:CD7").Select Selection.Copy DestFile.Activate ActiveSheet.Paste Application.ScreenUpdating = True ActiveWorkbook.Save End Sub 

我得到一个运行时错误13(“types不匹配”)在这一行的Sub 2,我不知道为什么:

Set Name = Worksheets(“Summary”)。Range(“A1”)。Value

我也在不同的地方(包括91,1004和9)得到了不同的错误,如果这个问题得到解决的话,可能会出现或者不会再出现。

更新我解决了前一行的错误13删除集(至less我想我做了),但现在我得到一个错误91:

Cells.Find(What:= Name,After:= ActiveCell,LookIn:= xlFormulas _,LookAt:= xlPart,SearchOrder:= xlByRows,SearchDirection:= xlNext,_ MatchCase:= False).Activate

再次,我是全新的,所以任何援助将不胜感激。

编辑我正在使用Excel 2011 for Mac,如果这很重要。

因为你正在激活一个不存在的对象。

最好检查Cells.Find()是否在尝试激活之前返回一个Range(或者Nothing)。

 'Create a temporary Range Dim temp_range As Range 'Notice that i removed the activation of the range at the end Set temp_range = Cells.Find(What:=Name, After:=activecell, LookIn:=xlFormulas _ , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False) 'before anything, test if it's nothing... If Not temp_range Is Nothing Then ' if not the you can activate.. temp_range.Activate ' and work on the result... Debug.Print temp_range.Value , temp_range.Address End If