2016-12-01 55 views
-3

我想在同一個工作簿上運行循環,但它也沒有給我提供任何幫助。但是如果我直接在工作簿(CGDSOUSD)上運行這個VBA,它運行良好。所以我想知道如何讓VBA打開一個新文件後運行VBA。使用VBA打開一個Excel文件,並運行一個循環,但循環總是會被跳過

Dim rownumber As Integer 
Dim colnumber As Integer 
Dim total As Double 
colnumber = 1 
For colnumber = 1 To 23 
    If Cells(8, colnumber) = "DELTA" Then 
    total = 0 
    rownumber = 9 
    Do Until Cells(rownumber, colnumber) = "" And Cells(rownumber + 1, colnumber) = "" And Cells(rownumber + 5, 1) = "" 
     If Cells(rownumber, 1) = "" And (Cells(rownumber, 7).Value = "DSO TROPS" Or Cells(rownumber, 8).Value = "DSO TROPS" Or Cells(rownumber, 6).Value = "DSO TROPS") Then 
     total = total + (Cells(rownumber, colnumber).Value) 
     Else 
     End If 
     rownumber = rownumber + 1 
    Loop 
    Else 
    End If 
    colnumber = colnumber + 1 
Next colnumber 
total = Round(total, 2) 'will be imputed into E20 in risk tools 
MsgBox total 
+4

安置自己的代碼作爲文本,而不是作爲一個圖像 –

+0

你把這個代碼的加載事件......會發生什麼,如果你把一個斷點? – Hackerman

+0

你是什麼意思「根本不會工作」。你的意思是它不會編譯?或者它只是跳過循環?或者它在循環內的一條線上崩潰?或者它不符合你的期望? – YowE3K

回答

0

也許Do UntilFalse。 要遍歷單元格,我總是確定lastrow並使用for循環。 請參閱下面的基本示例。 ps:使用ActiveCell並激活一個。

'將單元格A1的值設置爲A4以進行測試。

Sub test() 
Dim lastrow As Long 

lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row 

For i = 1 To lastrow 

If Cells(i, 1) <> "" Then 
Cells(i, 2).Value = "not empty" 
End If 

Next i 

End Sub