2016-11-22 84 views
0

更新了我的問題。我把我的表格的流程圖和截圖放在一起,這樣你們可以更好地理解。爲什麼當我嘗試運行我的VBA代碼時,我的excel停止響應?

Chart

Sheet

當我開始運行這個時,Excel停止響應。我似乎無法找到問題所在。我希望有人能幫幫忙!我研究了VBA代碼,但我認爲還有一些缺點?

Sub F110Loop() 

Dim x As Integer 'current amount 
Dim y As Integer 
Dim d As Double 'delta between Disbursement date and Cheque Release date 
Dim Current As Integer 
Dim Least As Integer 
Dim Dis As Worksheet 
Dim Cheque As Worksheet 
Dim wb As Workbook 

Set wb = ThisWorkbook 
Set Dis = wb.Sheets("Disbursements") 
Set Cheque = wb.Sheets("Cheque Info") 
wb.Activate 

For x = 4 To 600 
    Do While Dis.Cells(x, 9).Value > 1 
     'IF same amount, get row number to get corresponding date, reference that date 
     For y = 3 To 600 
      If Dis.Cells(x, 6).Value = Cheque.Cells(y, 5).Value Then 
       'THEN get delta 
       Current = Dis.Cells(x, 4).Value -Cheque.Cells(y, 2) 
       'IF current is less than the least delta 
      ElseIf Current < Least Then 
       'THEN update new value of delta 
       Current = Least 
      Else 
       'copy paste the date (from the least delta row) 
       Cheque.Cells(y, 2).Copy Destination:=Dis.Cells(x, 8) 
      End If 
     Next y 
    Loop 
Next x 

End Sub 
+0

你可以把一些信息關於單元格中的數據?這可能是因爲Excel VBA的內存量有限,所以循環大型工作表可能導致它變爲無效。 – McBoman

+0

這條線應該做什麼? (1)'Dis.Cells(x,4).Value -Cheque.Cells(y,2)= Current' – arcadeprecinct

+2

除了@CharlesWilliams已經提到的內容,您應該查看幾行: 4).Value -Cheque.Cells(y,2)= Current'。我相信這應該是'Current = Dis.Cells(x,4).Value - Cheque.Cells(y,2).Value'。另外,(2)'ElseIf Current Ralph

回答

4

也許你有一個無限循環,因爲你永遠不會改變Dis.Cells(X,9)

Do While Dis.Cells(x, 9).Value > 1 
' make no change to Dis.Cells(x, 9) 
Loop 
+0

嗨!謝謝你的迴應!它不會改變「For x = 4 to 600」? – Chesca

+0

@Chesca由於'do while'循環正在重複,因此x = 4到600'不能觸發'next x',請考慮將其更改爲'If Dis。單元格(x,9).Value> 1'' End If代替 –

1

你必須做一些事情,如:

x = 4 
Do while Dis.Cells(x,9).value > 1 
    x = x + 1 
loop 
+0

嗨@nofey,我如何限制它,以便它將繼續循環直到x = 600?謝謝! – Chesca