【问题标题】:Resolving runtime errors in Excel VBA解决 Excel VBA 中的运行时错误
【发布时间】:2015-11-18 06:03:40
【问题描述】:

我第一次涉足 Excel VBA,还有很多东西要学。

使用此宏,我正在设置一个文件,该文件允许我根据个人姓名从每周报告中复制/粘贴特定范围的数据。本质上,我希望能够让个人单击一个按钮并自动提取数据。到目前为止,这是我汇总的内容:

Sub 1(个人定义自己的名字):

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

Sub 2(宏根据在 Sub 1 中输入的名称从报告中提取数据):

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

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

设置名称 = Worksheets("Summary").Range("A1").Value

我在不同的点(包括 91、1004 和 9)也遇到了不同的错误,如果这个问题得到修复,这些错误可能会或可能不会再次出现。

更新我通过删除 Set 解决了上一行的错误 13(至少我认为我做到了),但现在我收到错误 91:

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

再次重申,我是新人,因此我们将不胜感激。

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

【问题讨论】:

  • 不需要把名字放到工作表上。变量可以很好地保持它。
  • 我不必设置变量以确保它从第一个子例程中提取值吗?我可能误读了您的评论(归咎于缺乏经验)。
  • 您不能将这样的变量“设置”为对象的值。简而言之,“设置”某物需要一个空的 object 变量并将其与预先存在的对象对齐。所以你可以将 xyz 调暗为 Range 并设置 xyz = Range("A1"),但你不能将 xyz 调暗为字符串并设置 xyz = Range("A1").Text
  • 另外,你不应该在多个 subs 中使用相同的变量。如果您希望两个 subs 都引用同一个变量,则需要在主代码模块中的 subs 之外并将其调暗为公共变量。
  • 两个独立的变量可以引用同一个值吗?就像我有 Sub 1 的变量标记为 X 和 Sub 2 的变量标记为 Y,但它们都从同一个地方拉出来,那会工作吗?

标签: macos vba excel


【解决方案1】:

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

最好在尝试激活之前检查 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

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 2016-11-08
    • 1970-01-01
    • 2019-12-15
    相关资源
    最近更新 更多