1
您好我正在爲excel的VBA中的宏。我的代碼中有一個嵌套的for循環,如下所示。第二個循環不運行;即使第二個循環中的MsgBox()命令也不會導致任何操作,程序似乎會跳過嵌套循環而不報告錯誤。VBA嵌套for循環不會運行
在普通的英語本嵌套循環應:
1)取從在陣列categories_string(第i項第一個for循環的字符串)。
2)通過在Excel文件(第二for循環300+行中列「AE」迭代,「長度」是在「AE」數據的列)
3)尋找長度字符串匹配並將1添加到用變量設置爲0(if語句)填充的categories_value數組中的相應條目。
For i = LBound(categories_string) To UBound(categories_string)
For p = 1 To p = length
If Worksheets("sheet1").Cells(p + 2, "AE").Value = categories_string(i) Then
categories_value(i) = categories_value(i) + 1
End If
Next
Next
'For p = 1 To length' –
由於您已經知道如何使用數組,因此爲什麼不將列AE中的值放入數組中並循環使用呢?然後循環單元格本身會更快。 'AEArray = Worksheets(「sheet1」)。Range(「AE3:AE」&length).Value' then instead for for loop on this array instead。 –
你的答案解決了它。和偉大的建議!這僅僅是我使用VBA的第二天,因此任何建議都是值得讚賞的。 – PSwalker