0

リストを検索して、最初に「spectraseven」が含まれる列のすべてのエントリを見つけるマクロを作成しています。これは、これらのレコードを各エントリのシートにコピーするためのものです。

このマクロは、ブックにシートが1つしかない場合に機能しますが、それ以上ある場合はobject or variable with block not set、最後に矢印が付いた行にエラーが表示されます。(If FoundCell.Address = FirstAddr Then

Function mySheetData(SheetName As String) As Boolean

'
'By Joe Was
    'This adds a sheet and names it "Test."
    'SheetName = Sheets(1).Range("a1")
    Sheets.Add.Name = SheetName

    'This selects your new sheet and moves it after sheet "Sheet3," which could be any sheet name.
    Sheets(SheetName).Select
    Sheets(SheetName).Move After:=Sheets(Worksheets.Count)

    'this selects the sheet with the data and its range.
    Sheets(1).Select
    Range("A1:c20").Select

    'This will copy and paste the data to your new sheet "Test."
    Selection.Copy
    Sheets(SheetName).Select
    ActiveSheet.Paste

    'At this point your data will be on the new sheet and selected for the next step.

End Function
'copy from template sheet
'add information from each summary sheet to the tech sheets

'Function MoveToTables(manName As String, startCell As Integer, cellRange As String) As Boolean
Sub testWild()
startCell = 1
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddr As String
Dim technum As String

cellRange = "e1:e500"
topCount = startCell
With Range("e1:e500")
    Set LastCell = .Cells(.Cells.Count)
End With
Dim findString As String
findString = "SPECTRASEVEN*"
Set FoundCell = Sheets(1).Range(cellRange).Find(what:=findString, After:=LastCell)

If Not FoundCell Is Nothing Then
    FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
    Debug.Print FoundCell.Address
    Count = FoundCell.Row
    technum = Right(FoundCell, 4)
    Set FoundCell = Range(cellRange).FindNext(After:=FoundCell)
    temp = mySheetData(technum)
    ' vv This is the line with the error vv
    If FoundCell.Address = FirstAddr Then
             Exit Do
    End If
Loop
End Sub
4

1 に答える 1

0

問題は、テストが発生する前に次のセルに進んでいることですNothing

この例は、1 桁の数字を出力するためのものです。

Sub test1()

Dim j

j = 1
Do Until j = 10
    Debug.Print j
    j = j + 1
    If j = 10 Then Debug.Print "oops, why no exit?"
    Debug.Print j
Loop

End Sub

このコードを実行すると、ループが終了したように見えても、j=10 のときに出力されるメッセージが表示されます。
インクリメントがループの最後に発生するように修正して、テストが機能するようにすると、次のようになります。

Sub test2()

Dim j

j = 1

j = j + 1
Do Until j = 10
    Debug.Print j
    If j = 10 Then Debug.Print "oops, why no exit?"
    Debug.Print j
    j = j + 1
Loop

End Sub

ループが開始する前に 1 つ (テストを開始する前に j がたまたま 9 になった場合に備えて) と最後に 1 つ、2 つのインクリメントがあることに注意してください。したがって、ループは適切なポイントでテストできます。

于 2012-08-23T17:38:58.777 に答える