0

I'm trying to create a macro for Excel to grab data from different worksheets and put it onto a summary page. Because I'm new to VBA my current method is to search for "TOI Score" (always found in row 45), offset that cell by one column to the right, copy from that cell to .End(xlDown) and .PasteSpecial with the transpose = true at the bottom of that worksheet, then search for the next instance of TOI Score and repeat. Eventually it will copy the copied data to the summary page.

For some reason though, I cannot get Excel to find TOI Score within this particular script. I know that it can find it using a bare-bones macro that's only purpose is to find it, but I can't figure out what is messing it up in this one. At first it was simply not finding the string, but I did something and now I'm getting run-time error 91 (the one where it says I'm referencing a non-object or empty object). Thanks for any help you can give...

Here's the script up to where I get the error:

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

あなたの例で検索しているのは string ですstrSearchが、 ではありません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 に答える