標題: [發問] 沒有設定物件變數或 With 區塊變數 [打印本頁] 作者: esperanto 時間: 2014-6-9 10:19 標題: 沒有設定物件變數或 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 '指定按鈕名稱
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