Board logo

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

作者: wiemanson    時間: 2010-7-22 17:37     標題: 刪除活頁簿資料就無法執行VBA,出現沒有設定物件變數或WITH區塊變數

本帖最後由 wiemanson 於 2010-7-22 17:40 編輯

各位前輩好!
我寫了一個程式,然後活頁簿的資料則是我自己先亂輸入拿來測試程式是否順利用的,就在我終於成功順利執行後,匯入正式要算的資料進去活頁簿,居然就無法執行了!
不知道是出了什麼問題,後來我就在用我原本測試的活頁簿資料在去執行,結果又可以執行,然後我就一直試到底是哪裡出了問題,結果發現只要我刪除那測試用的活頁簿資料,就會跑出沒有設定物件變數或WITH區塊變數的錯誤視窗!

我的程式是寫在THISBOOK裡面,程式碼如下所示
Sub s1()
Worksheets("Sheet1").Select
  x = 1
   Do
      If Cells(x, 1) = "" Then Exit Do
        ElseIf Cells(x, 1) = Cells.Find(What:="林", After:=ActiveCell) And Cells(x, 3) = Cells.Find(What:="4", After:=ActiveCell) And Cells(x, 4) = "普通" And Cells(x, 6) = "否" Then
         Cells(x, 7) = 23 * Cells(x, 5)
        ElseIf Cells(x, 1) = Cells.Find(What:="林", After:=ActiveCell) And Cells(x, 4) = "好" And Cells(x, 6) = "否" Then
         Cells(x, 7) = 20 * Cells(x, 5)
        ElseIf Cells(x, 1) = Cells.Find(What:="林", After:=ActiveCell) And Cells(x, 4) = "不好" And Cells(x, 5) <= 20 And Cells(x, 6) = "否" Then
         Cells(x, 7) = 300
        ElseIf Cells(x, 1) = Cells.Find(What:="林", After:=ActiveCell) And Cells(x, 6) = "否" And Cells(x, 4) = "其它" Then
        Cells(x, 7) = "另外算"
        
        
        ElseIf Cells(x, 1) = Cells.Find(What:="力", After:=ActiveCell) And Cells(x, 4) = "好" And Cells(x, 6) = "否" Then
         Cells(x, 7) = 10 * Cells(x, 5)
        ElseIf Cells(x, 1) = Cells.Find(What:="力", After:=ActiveCell) And Cells(x, 4) = "不好" And Cells(x, 6) = "否" Then
         Cells(x, 7) = 2 * Cells(x, 5)
        ElseIf Cells(x, 1) = Cells.Find(What:="力", After:=ActiveCell) And Cells(x, 4) = "普通" And Cells(x, 5) <= 20 And Cells(x, 6) = "否" Then
         Cells(x, 7) = 300
        ElseIf Cells(x, 1) = Cells.Find(What:="力", After:=ActiveCell) And Cells(x, 6) = "否" And Cells(x, 4) = "其它" Then
        Cells(x, 7) = "另外算"
        
        
        End If
         
         
         
    x = x + 1
    Loop
                  
     
End Sub

我也爬文爬了很久,試過了把程式碼寫在模組裡面,結果也不行!
請問我要怎麼解決這問題呢!?
請各位前輩指導迷津!
謝謝!
作者: kimbal    時間: 2010-7-22 18:39

本帖最後由 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
複製代碼

作者: wiemanson    時間: 2010-7-22 19:34

回復 2# kimbal


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

我的資料表主要是呈現這樣,合計就是我所寫的程式所要計算出來的!

客戶名稱│重量公斤│合計
一一一一一一一一一一一一一一
    力        │20        │
    力        │53.8     │
    力        │14        │
    林        │21        │
    林        │13        │
   力         │10        │
作者: kimbal    時間: 2010-7-22 21:00

回復  kimbal


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

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



   用2樓提供的代碼沒問題嗎?
作者: wiemanson    時間: 2010-7-22 21:16

回復 4# kimbal


    先謝謝前輩熱心指導,我直接把檔案丟上來好了!
作者: kimbal    時間: 2010-7-22 21:47

回復  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
複製代碼

作者: wiemanson    時間: 2010-7-22 22:09

可以了!謝謝前輩解答!
真的超開心的!




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)