2016-07-16 45 views
1

我目前正在寫一個簡單的宏來確定誰在哪個時間段安排,並在另一個工作表上記錄該時間段。一切正常,直到最後一個時間段的人員的姓名與最後一個時間段相比較爲止。此時for循環「j」變爲-1並導致宏出錯。簡單的嵌套for循環出錯1004錯誤

我已經包括了我的全部代碼如下但這

Dim mySheet As Worksheet, masterSheet As Worksheet, myBook As Workbook 'Define your workbooks and worksheets as variables 

    Option Compare Text 'Makes string comparisons case IN-sensitive 

    Sub Watch_Bill() 

    Set myBook = Excel.ActiveWorkbook 
    Set masterSheet = Sheets("Musters") 
    MsgBox masterSheet.Name 

    Dim memberName, memberFirstWatch, memberSecondWatch As String 
    Dim lastRow As Integer 
    Dim watchStation, watch As String 

    lastRow = masterSheet.Range("A1").CurrentRegion.Rows.Count 
    'lastColumn = ActiveSheet.Range("A1").CurrentRegion.Columns.Count 

    'Cycle through each member of the duty section 
    For i = 2 To lastRow 
     memberName = masterSheet.Cells(i, 2).Value 

     'Cycle through watch bill to find member's watches 
     For j = 9 To 18  'Row 9 starts the section of the watchbill that contains watches 
      'MsgBox j 
      For k = 2 To 9 'Column 2 through 9 contain watches 
       watchStation = ActiveSheet.Cells(j, k).Value 

       'MsgBox watchStation 
       If InStr(watchStation, memberName) <> 0 Then 

        'Determine what watch station member is on 
        If j = 9 Or j = 10 Then 
         watch = "0700-1200" 
        ElseIf j = 11 Or j = 12 Then 
         watch = "1200-1700" 
        ElseIf j = 13 Or j = 14 Then 
         watch = "1700-2200" 
        ElseIf j = 15 Or j = 16 Then 
         watch = "2200-0200" 
        Else: j = 17 Or j = 18 
         watch = "0200-0700" 
        End If 
        'MsgBox "Found member" 

        'Check if member already had watch 
        If memberFirstWatch = "" Then 
         'MsgBox "member's first watch" 
         memberFirstWatch = watch 
        Else 
         'MsgBox "member's second watch" 
         memberSecondWatch = watch 
        End If 

        'Fill in member's watch times on muster sheet 
        masterSheet.Cells(i, 11).Value = memberFirstWatch 
        masterSheet.Cells(i, 12).Value = memberSecondWatch 
       End If 
      Next k 
     Next j 

     memberFirstWatch = "" 
     memberSecondWatch = "" 

    Next i 

    End Sub 

感謝所有幫助任何人都可以提供在線調試器指向是 watchStation = ActiveSheet.Cells(J,K).value的

。這讓我感到非常緊張,而且現在我還沒有弄明白這個問題。

+0

我想你需要將Else的末尾改成另一個ElseIf。因爲它現在是你設置j爲真(17或j = 18)。 –

回答

1

Doug是對的。我會試着解釋發生了什麼。任何人都可以隨時糾正我。

道格指出,問題是行

j = 17 Or j = 18 

由於沒有If,VBA嘗試將其評價爲j

j = (17 Or j = 18) 

現在是什麼17 Or j = 18賦值? j = 18的右邊是True,因爲j在那一刻是18。因此,我們有

j = (17 Or True) 

現在我們可以說,anything Or True始終是真實的,但我們可以去得更深一些。你如何使用帶號碼的Or?您可以使用二進制數的按位比較,例如

00001011 
Or 00010010 
----------- 
= 00011011 

我們在處理數字和布爾值時也是這樣。 False被存儲爲全0,並且True被存儲爲全1,這產生我們想要的精確行爲Not,And,Or,XOr。例如。 anything Or True變爲:

xxxxxxxx 
Or 11111111 
----------- 
= 11111111 

只使用1位也將表現出同樣的行爲,當然,但我們沒有1位數據類型。所以17 Or j = 18True,其被存儲爲11...11,當以整數讀取時,其爲-1

請注意,我忽略了數據類型的不同字節大小。

VBA爲你做了很多隱式轉換,這可能是很好的,特別是對於數字< - >字符串,但它可能會導致只會在代碼中稍後出現的問題。 例如不小心將一個數字存儲爲一個字符串,然後將其添加到實際的數字將工作(字符串將被轉換)。但是添加兩個數字字符串會連接它們。

+1

這就是爲什麼我愛你們。您不僅可以很快提供正確的答案,還可以花時間解釋爲什麼它是這樣。超級簡單的修復,我永遠不會去。再次感謝你! –