0

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

但由于某种原因,我无法让 ExcelTOI 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)
4

2 回答 2

2

您在示例中搜索的是 stringstrSearch但不是TOI Score. 将适当的行更改为:

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

等等...

于 2013-03-08T06:53:03.843 回答
0

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

尝试改变:

Set oRange = ActiveSheet.Rows(45)

到:

Set oRange = sh.Rows(45)

于 2013-03-09T01:31:41.457 回答