2017-07-14 74 views
-3

我不知道爲什麼聲明不起作用。沒有錯誤發生,但同時在表單中沒有結果。VBA:如果聲明不起作用

Sub won() 
    Dim ws As Worksheet 
    Dim lastrow As Long 

    Set ws = ThisWorkbook.Sheets("Data imput") 
    lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 

    For i = 3 To lastrow 
     If ws.Cells(i, 6) = "customer" Then 
     ws.Cells(i, 16) = 1 
     End If 
    Next i 

    MsgBox "done!" 
End Sub 
+0

也許它的工作,它的計算結果爲'FALSE'? – David

+0

嗨,可能是一個愚蠢的答案,但是是「客戶」正確書寫, –

+0

它也不顯示False。 – aannie

回答

0

這可能是因爲您正在搜索列1內的LASTROW就做了6列如下:

lastrow = ws.Cells(ws.Rows.Count, 6).End(xlUp).Row 
+0

它現在有效!謝謝! – aannie

0

你忘了定義變量i。

Dim i As Integer 

爲防止以後出現此錯誤把下面的代碼行的頂部宏

Option Explicit 
+0

將'i'變量聲明爲'Long'可能會更好,因爲'Integer'是16位,介於-32,768和32,768之間,而'Long'是32位,介於-2,147,483,648和2,147,483,648之間 – Tehscript

+0

True,Long將是因爲你已經將lastrow聲明爲Long。 – sourceCode