2013-03-05 59 views
2

這是我的代碼。代碼本身似乎工作,但由於某種原因,條件語句在第32645行後停止。我嘗試將所有變量切換爲Long,並沒有幫助。VBA循環有條件停止在任意數量的行之後被評估

另外,如果我使代碼在第32646行開始,但隨後在隨後的一行(〜18000)處停止,代碼將起作用。它所停止的數據似乎沒有相似之處。最初我嘗試了一個指定開始和結束行的for循環,但是這也不起作用,所以我嘗試了while循環(理論上都應該可以,但似乎都不行)。

它需要能夠處理130,000 +行,任何想法爲什麼這可能會發生?

的問題

迴路沒有錯誤,並通過添加一個消息框停止運行,我知道我行增加變量在最後一行結束了,但根據工作簿時,條件語句停止在後評估任意數量的行。

如果我從第一次運行停止的下一行開始運行腳本,它可以工作,但同樣適用於(可能不同)任意數量的步驟。

我已經做了我所有的變量 「龍」 型。我使用Option Explicit。

data_row = CLng(InputBox("Please enter the row number of the first data entry")) 
Worksheets.Add(After:=Worksheets(1)).name = "Formatted_Output" 

Set ws = Sheets("Formatted_Output") 
Worksheets(1).Activate 
LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column 
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).row 
data_col = LastCol 

'loop through entries to get account and permissions 
increment = data_row 
Do Until increment = LastRow 
    If Cells(data_row, data_col) = "" Then 
     data_col = data_col - 1 
    Else 
    ' ~> For illegal ==> access denied permission that throws errors 
     If Cells(data_row, data_col).Value = "==>access denied" Then 
      permissions = "access denied" 'remove illegal = 
      ws.Cells(output_row, 3).Value = permissions 'print permissions to output file 
     Else 
      permissions = Cells(data_row, data_col).Value 'cell should be permission cell of each row 
      ws.Cells(output_row, 3).Value = permissions 'print permissions to output file 
     End If 

     data_col = data_col - 1 'domain/account cell is now highlighted 

     If InStrRev(Cells(data_row, data_col).Value, "?") > 0 Then 

      account = Split(Cells(data_row, data_col).Value, "?")(1) & Str(unknown_count) ' separate domain name and unknown id 
      unknown_count = unknown_count + 1 ' counting the number of unkown accounts found 
      ws.Cells(output_row, 2) = account 'print unknown account Id to output 

      domain_bit = Split(Cells(data_row, data_col).Value, "?")(0) '' get separate domain name from id cell 
      data_col = data_col - 1 'domain second from end cell is now highlighted 

      Do While data_col > 0 'generate domain from rest of row 
       domain_bit = Cells(data_row, data_col).Value & domain_bit 'domain built backwards 
       data_col = data_col - 1 'check next column for more of location name 
      Loop 

      ws.Cells(output_row, 1) = domain_bit 
       'data_col = LastCol 
       'data_row = data_row + 1 
       'output_row = output_row + 1 

     ElseIf InStrRev(Cells(data_row, data_col).Value, "\") > 0 Then 

      account = Split(Cells(data_row, data_col).Value, "\")(1) 'separate account ID 
      ws.Cells(output_row, 2) = account 'print account ID to oputput 

      domain_bit = Split(Cells(data_row, data_col).Value, "\")(0) 
      data_col = data_col - 1 'domain second from end cell is now highlighted 

      Do While data_col > 0 'generate domain from rest of row 
       domain_bit = Cells(data_row, data_col).Value & domain_bit 'domain built backwards 
       data_col = data_col - 1 'check next column for more of location name 
      Loop 

      ws.Cells(output_row, 1) = domain_bit 'output to file 

     Else 
         account = Cells(data_row, data_col).Value 'account is just whole cell whether empty or one word no path 
      ws.Cells(output_row, 2) = account 'print account ID to oputput 

      data_col = data_col - 1 'domain second from end cell is now highlighted (since no domain in account cell) 
      Do While data_col > 0 'generate domain from rest of row 
       domain_bit = Cells(data_row, data_col).Value & domain_bit 'domain built backwards 
       data_col = data_col - 1 'check next column for more of location name 
      Loop 

      ws.Cells(output_row, 1) = domain_bit 'output to file 
     End If 

    data_col = LastCol 
    data_row = data_row + 1 
    output_row = output_row + 1 
    End If 

'Next increment 
ws.Range("E" & 1) = unknown_count 
increment = increment + 1 

If increment = LastRow Then 
    MsgBox (Str(increment) & "=" & Str(LastRow)) 
End If 

Loop 
+6

32646將是正確的2^15的邊界,例如上一個有符號的16位整數。你可能錯過了某個地方的'長'定義,所以你仍然在處理16位整數,而不是整個日誌。 – 2013-03-05 16:16:07

+0

簡單的問題 - 這似乎非常接近'Integer'數據類型的上限 - 您是否將所有計數器定義爲'Long'?這可能是你的問題... – 2013-03-05 16:17:19

+0

嗨,感謝評論。我將每個數字定義爲Long。 Ctrl-F整數,沒有結果了。這是令人困惑的部分,因爲我認爲肯定是問題所在。 – 4lert 2013-03-05 16:22:48

回答

1

我相信你的第一個首要if語句不這樣做,因爲它應該,這是如果它被發現是真data_row變量沒有增加禁用自動排增加。

如果這是你想要的,那麼increment變量需要在if語句內移動,因爲它會隨着變量的增加而變化,而不考慮data_row變量。

我覺得for-next會更好:

for data_row = 1 to LastRow 
    'do some stuff 
next 

這將循環中的所有行1至LastRow每次迭代。


添加更好的答案:

Do Until increment > LastRow 
    ... 
    data_col = LastCol 
    data_row = data_row + 1 
    output_row = output_row + 1 
    increment = increment + 1 
    End If 

    If increment > LastRow Then 
     MsgBox (Str(increment) & ">" & Str(LastRow)) 
    End If 
Loop 
+0

感謝您的評論,但是第一條if語句試圖找到數據在該特定行的開始位置(每行都有數據,並非全部數據都有相同的列數)。 找到數據後,其他人會根據嵌套條件。因此,我只在計算else後才增加。 – 4lert 2013-03-06 12:09:23

+0

但是,您不會在放置「increment + 1」變量的位置放置在「data_Row + 1」的下方 – glh 2013-03-06 12:15:52

+0

每空白最後一列變量'increment'更接近'LastRow'變量,而沒有實際分析行。 – glh 2013-03-06 12:20:17