返回列表 上一主題 發帖

[發問] 刪除活頁簿資料就無法執行VBA,出現沒有設定物件變數或WITH區塊變數

本帖最後由 kimbal 於 2010-7-22 18:42 編輯
各位前輩好!
我寫了一個程式,然後活頁簿的資料則是我自己先亂輸入拿來測試程式是否順利用的,就在我終於 ...
wiemanson 發表於 2010-7-22 17:37



sheet1如果沒有 林, 力, 4  的內容就會死掉

Cells.Find(What:="林", After:=ActiveCell) 這些, 如果找不到格子, 就會回傳nothing
這個時候, if Cells(x, 1) = nothing then 就會死掉
  1. Sub s1()
  2. Worksheets("Sheet1").Select
  3.   x = 1
  4.    Do
  5.       If Cells(x, 1) = "" Then
  6.         Exit Do
  7.       Else
  8.         y1 = Cells.Find(What:="林", After:=ActiveCell)
  9.         If y1 Is Nothing Then
  10.             y1s = "NOTHING"
  11.         Else
  12.             y1s = y1.Value
  13.         End If
  14.         y2 = Cells.Find(What:="4", After:=ActiveCell)
  15.         If y2 Is Nothing Then
  16.             y2s = "NOTHING"
  17.         Else
  18.             y2s = y2.Value
  19.         End If
  20.         y3 = Cells.Find(What:="力", After:=ActiveCell)
  21.         If y3 Is Nothing Then
  22.             y3s = "NOTHING"
  23.         Else
  24.             y3s = y3.Value
  25.         End If

  26.         
  27.         If Cells(x, 1) = y1s And Cells(x, 3) = y2s And Cells(x, 4) = "普通" And Cells(x, 6) = "否" Then
  28.          Cells(x, 7) = 23 * Cells(x, 5)
  29.         ElseIf Cells(x, 1) = y1s And Cells(x, 4) = "好" And Cells(x, 6) = "否" Then
  30.          Cells(x, 7) = 20 * Cells(x, 5)
  31.         ElseIf Cells(x, 1) = y1s And Cells(x, 4) = "不好" And Cells(x, 5) <= 20 And Cells(x, 6) = "否" Then
  32.          Cells(x, 7) = 300
  33.         ElseIf Cells(x, 1) = y1s And Cells(x, 6) = "否" And Cells(x, 4) = "其它" Then
  34.         Cells(x, 7) = "另外算"
  35.         ElseIf Cells(x, 1) = y3s And Cells(x, 4) = "好" And Cells(x, 6) = "否" Then
  36.          Cells(x, 7) = 10 * Cells(x, 5)
  37.         ElseIf Cells(x, 1) = y3s And Cells(x, 4) = "不好" And Cells(x, 6) = "否" Then
  38.          Cells(x, 7) = 2 * Cells(x, 5)
  39.         ElseIf Cells(x, 1) = y3s And Cells(x, 4) = "普通" And Cells(x, 5) <= 20 And Cells(x, 6) = "否" Then
  40.          Cells(x, 7) = 300
  41.         ElseIf Cells(x, 1) = y3s And Cells(x, 6) = "否" And Cells(x, 4) = "其它" Then
  42.         Cells(x, 7) = "另外算"
  43.         End If
  44.    
  45.          
  46.      End If
  47.          
  48.     x = x + 1
  49.     Loop
  50.                   
  51.      
  52. End Sub
複製代碼
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復  kimbal


  但是我正式的活頁簿資料中,都有我在程式裡面所要比對的字元!!

我的資料表主要是 ...
wiemanson 發表於 2010-7-22 19:34



   用2樓提供的代碼沒問題嗎?
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復  kimbal


    先謝謝前輩熱心指導,我直接把檔案丟上來好了!
wiemanson 發表於 2010-7-22 21:16



   正如上面所說,沒有 "去氫", 所以死掉啦~
貼上這個再試試運一下
  1. Sub s1()

  2. Worksheets("Sheet1").Select
  3.   x = 1
  4.    Do
  5.       If Cells(x, 1) = "" Then
  6.         Exit Do
  7.         End If
  8.         Set y1 = Cells.Find(What:="三群", After:=ActiveCell)
  9.         If y1 Is Nothing Then
  10.             y1 = ""
  11.         End If
  12.         Set y2 = Cells.Find(What:="去氫", After:=ActiveCell)
  13.         If y2 Is Nothing Then
  14.             y2 = ""
  15.         End If
  16.         Set y3 = Cells.Find(What:="金發", After:=ActiveCell)
  17.         If y3 Is Nothing Then
  18.             y3 = ""
  19.         End If
  20.         Set y4 = Cells.Find(What:="厚", After:=ActiveCell)
  21.         If y4 Is Nothing Then
  22.             y4 = ""
  23.         End If
  24.         If Cells(x, 1) = y1 And Cells(x, 3) = y2 And Cells(x, 4) = "鎳" And Cells(x, 6) = "否" Then
  25.          Cells(x, 7) = 11 * Cells(x, 5)
  26.         ElseIf Cells(x, 1) = y1 And Cells(x, 3) = y4 And Cells(x, 4) = "鎳" And Cells(x, 6) = "否" Then
  27.          Cells(x, 7) = 2 * Cells(x, 5)
  28.         ElseIf Cells(x, 1) = y1 And Cells(x, 4) = "鎳" And Cells(x, 6) = "否" Then
  29.          Cells(x, 7) = 5 * Cells(x, 5)
  30.         ElseIf Cells(x, 1) = y1 And Cells(x, 4) = "鉻" And Cells(x, 6) = "否" Then
  31.          Cells(x, 7) = 45 * Cells(x, 5)
  32.         ElseIf Cells(x, 1) = y1 And Cells(x, 4) = "青銅" And Cells(x, 6) = "否" Then
  33.          Cells(x, 7) = 12 * Cells(x, 5)
  34.         ElseIf Cells(x, 1) = y1 And Cells(x, 4) = "鎳" And Cells(x, 5) <= 20 And Cells(x, 6) = "否" Then
  35.          Cells(x, 7) = "斟酌"
  36.         ElseIf Cells(x, 1) = y1 And Cells(x, 6) = "否" And Cells(x, 4) = "其它" Then
  37.         Cells(x, 7) = "另外算"
  38.    
  39.    
  40.         ElseIf Cells(x, 1) = y3 And Cells(x, 3) = Cells.Find(What:="(3mm)", After:=ActiveCell) And Cells(x, 4) = "鎳" And Cells(x, 6) = "否" Then
  41.          Cells(x, 7) = "小支"
  42.         ElseIf Cells(x, 1) = y3 And Cells(x, 4) = "鎳" And Cells(x, 6) = "否" Then
  43.          Cells(x, 7) = 10 * Cells(x, 5)
  44.         ElseIf Cells(x, 1) = y3 And Cells(x, 4) = "鉻" And Cells(x, 6) = "否" Then
  45.          Cells(x, 7) = 2 * Cells(x, 5)
  46.         ElseIf Cells(x, 1) = y3 And Cells(x, 4) = "鎳" And Cells(x, 5) <= 20 And Cells(x, 6) = "否" Then
  47.          Cells(x, 7) = 40
  48.         ElseIf Cells(x, 1) = y3 And Cells(x, 6) = "否" And Cells(x, 4) = "其它" Then
  49.         Cells(x, 7) = "另外算"
  50.         
  51.         Else
  52.             
  53.         Cells(x, 7) = 0

  54.      
  55.         End If
  56.          
  57.          
  58.          
  59.     x = x + 1
  60.     Loop

  61.   End Sub
複製代碼
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

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