使用VBA打开一个Excel文件,并运行一个循环,但循环总是会被跳过
问题描述:
我想在同一个工作簿上运行循环,但它也没有给我提供任何帮助。但是如果我直接在工作簿(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
答
也许Do Until
是False
。 要遍历单元格,我总是确定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
安置自己的代码作为文本,而不是作为一个图像 –
你把这个代码的加载事件......会发生什么,如果你把一个断点? – Hackerman
你是什么意思“根本不会工作”。你的意思是它不会编译?或者它只是跳过循环?或者它在循环内的一条线上崩溃?或者它不符合你的期望? – YowE3K