运行时错误91,但我有一切Set

时间:2013-03-08 06:28:42

标签: excel-vba vba excel

我正在尝试为Excel创建一个宏来从不同的工作表中获取数据并将其放到摘要页面上。因为我是VBA的新手,我现在的方法是搜索“TOI得分”(总是在第45行找到),将该单元格向右偏移一列,从该单元格复制到.End(xlDown).PasteSpecial以及该工作表底部的transpose = true,然后搜索TOI Score的下一个实例并重复。最终,它会将复制的数据复制到摘要页面。

出于某种原因,我无法让Excel在此特定脚本中找到TOI Score。我知道它可以使用一个简单的宏来找到它,它的唯一目的就是找到它,但我无法弄清楚在这个中弄乱了什么。起初它根本就没有找到字符串,但我做了一些事情,现在我得到了运行时错误91(它说的是我引用的是非对象或空对象)。感谢您提供任何帮助......

以下是我收到错误的脚本:

Sub GrabMyData()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim CopyRng As Range, CopyHere As Range, CopyHereLastCol As Long, CopyHereLastRow As Long
Dim aCell As Range, bCell As Range, oRange As Range, fullRange As Range
Dim myLastRow As Long, myLastCol As Long
Dim Last As Long
Dim strSearch As String
Dim t As Long



On Error GoTo Err

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Summary Sheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True


Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Summary Sheet"


For Each sh In ActiveWorkbook.Worksheets
    If sh.Name <> DestSh.Name Then

        strSearch = "TOI Score"

        Set oRange = ActiveSheet.Rows(45)       

        Set aCell = oRange.Find(What:=strSearch, LookIn:=xlValues, _
        Lookat:=xlPart, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

2 个答案:

答案 0 :(得分:2)

您在示例中搜索的内容是字符串strSearch,但不是TOI Score。将适当的行改为:

Set aCell = oRange.Find(What:=strSearch,...

依旧......

答案 1 :(得分:0)

您正在遍历工作表并使用变量sh来存储当前工作表。当您使用变量oRange设置要搜索的范围时,您始终会查看活动工作表而不是sh指定的工作表

尝试更改:

Set oRange = ActiveSheet.Rows(45)

为:

Set oRange = sh.Rows(45)