返回列表 上一主題 發帖

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

[發問] 刪除活頁簿資料就無法執行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

我也爬文爬了很久,試過了把程式碼寫在模組裡面,結果也不行!
請問我要怎麼解決這問題呢!?
請各位前輩指導迷津!
謝謝!

回復 2# kimbal


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

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

客戶名稱│重量公斤│合計
一一一一一一一一一一一一一一
    力        │20        │
    力        │53.8     │
    力        │14        │
    林        │21        │
    林        │13        │
   力         │10        │

TOP

回復 4# kimbal


    先謝謝前輩熱心指導,我直接把檔案丟上來好了!

Book1.rar (9.1 KB)

TOP

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

TOP

        靜思自在 : 不要隨心所欲,要隨心教育自己。
返回列表 上一主題