返回列表 上一主題 發帖

[發問] 沒有設定物件變數或 With 區塊變數

[發問] 沒有設定物件變數或 With 區塊變數

各位好

我的程式有階段錯誤''91''的問題,但我實在找不出來原因,
只好上來求教了。
程式碼如下:

Sub Scheduling()
With Worksheets("Scheduling")
    n = .Columns("A").Find("*", SearchOrder:=xlByRows, LookIn:=xlFormulas, SearchDirection:=xlPrevious).EntireRow.Row
    For i = 5 To n Step 2
        ProductID = Worksheets("Scheduling").Cells(i, 3)
            Set d = Sheets("KEYIN").Columns("AH").Find(ProductID, LookIn:=xlValues, lookat:=xlWhole)  '在資料庫中搜尋相等 料號 之值
            If Not d Is Nothing Then  ' 如果找到相等之值的話
                Product = Worksheets("KEYIN").Cells(d.Row, d.Column + 1)
            End If
        .Cells(i, 4) = Product
    Next i
End With

Worksheets("M").Activate
    For i = 3 To 23
        For Each od In ActiveSheet.Buttons
           If od.Name = ("Buttons " & i) Then
                od.Delete
           End If
        Next        
        For Each od In ActiveSheet.OLEObjects
           If od.Name = ("CheckBox" & CStr(i)) Or od.Name = ("CheckBoxb" & CStr(i)) Then
                od.Delete
           End If
        Next
        serial = 0
        
        machine = Worksheets("M").Cells(i, 3)
        Set c = Worksheets("Scheduling").Columns("B").Find(machine, LookIn:=xlValues, lookat:=xlWhole) '在資料庫中搜尋相等  之值
        If Not c Is Nothing Then  ' 如果找到相等之值的話
            firstAddress = c.Address
            serial = 1
            Do
                If Worksheets("Scheduling").Cells(c.Row, 1) = Date And serial = 1 Then                    
                    ProductID = Worksheets("Scheduling").Cells(c.Row, 3)
                    Product = Worksheets("Scheduling").Cells(c.Row, 4)
                    Demand = Worksheets("Scheduling").Cells(c.Row, 5)
                    Operater = Worksheets("Scheduling").Cells(c.Row, 7)
                    Starttime = Worksheets("Scheduling").Cells(c.Row, 9)
                    Worksheets("Scheduling").Cells(c.Row, 11) = 1
                    Range(Worksheets("Scheduling").Cells(c.Row, 1), Worksheets("Scheduling").Cells(c.Row + 1, 9)).ClearContents
                    Worksheets("M").Cells(i, 4) = Product
                    Worksheets("M").Cells(i, 5) = Demand
                    Worksheets("M").Cells(i, 7) = Operater
                    Worksheets("M").Cells(i, 12) = Starttime
                    
                    If Operater > 0 Then
                        SS = Worksheets("M").Cells(i, 1).Top '所選擇的目標位址
                        ll = Worksheets("M").Cells(i, 1).Left
                        Set ob = Worksheets("M").Buttons.Add(ll + 940, SS + 6, 33, 19) '加入按鈕
                        ob.Characters.Text = "完成"  '指定按鈕文字
                        ob.OnAction = "scheduleok"     '指定按鈕巨集
                        ob.Name = "Buttons " & i   '指定按鈕名稱

                        S = Worksheets("M").Cells(i, 7).Top '所選擇的目標位址
                        l = Worksheets("M").Cells(i, 7).Left
                        Set ob1 = Worksheets("M").OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
                            DisplayAsIcon:=False, Left:=l, Top:=S + 1, Width:=26, Height:=13)
                            ob1.Name = "CheckBox" & i
                            ob1.Object.Caption = "日"
                            ob1.Object.Font.Size = 9
                            ob1.Object.ForeColor = RGB(255, 128, 128)
                            'ob1.Object.AutoSize = True
                            ob1.Object.BackColor = Worksheets("M").Cells(i, 7).Interior.Color
                            'ob1.Object.SpecialEffect = 0

                    End If
                    
                ElseIf Worksheets("Scheduling").Cells(c.Row, 1) = Date And serial = 2 Then
                    ProductID = Worksheets("Scheduling").Cells(c.Row, 3)
                    Product = Worksheets("Scheduling").Cells(c.Row, 4)
                    Worksheets("M").Cells(i, 13) = Product
                End If
                serial = 2
            Set c = Worksheets("Scheduling").Columns("B").FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
         
    Next i
End Sub

還請各位高手們相助!

owen06 大:

抱歉,因檔案有公司的資料,不方便附檔。  Orz

我發現如果把

Loop While Not c Is Nothing And c.Address <> firstAddress

改成

Loop While Not c Is Nothing

似乎就沒問題了?!
這樣看來好像是因為當 FindNext(c) 找不到時,c沒有值所以發生問題。
可是我在別的程式中也是這樣使用 FindNext 配合Do....Loop,都沒發生問題啊?
我是照VBA裡的說明範例寫的,
為何在這段程式中會有"沒有設定物件變數或 With 區塊變數"的錯誤呢?

希望能有人指教小弟,我為了這問題卡了1天...........Orz

TOP

回復 4# stillfish00


    謝謝stillfish00大大

         這樣寫確實能解決問題,但
               Loop While Not c Is Nothing And c.Address <> firstAddress
         這行程式我之前都是這樣寫的,都可正常運作,為何在這段程式中就會出問題呢?

TOP

回復 6# stillfish00


    謝謝stillfish00大大

                 看來應該是這樣了,我沒注意到說明範例有先給c一個值。
                 大大真是明察秋毫 XD

TOP

        靜思自在 : 站在半路,比走到目標更辛苦。
返回列表 上一主題