如何遍历Excel VBA或VSTO 2005中的所有单元格 [英] How to iterate through all the cells in Excel VBA or VSTO 2005
问题描述
我需要简单地浏览Excel电子表格中的所有单元格,并检查单元格中的值。单元格可能包含文本,数字或空白。我不是很熟悉/舒适地使用范围的概念。因此,任何示例代码都将不胜感激。 (我确实尝试google了,但我发现的代码段并不完全满足我的需要)
I need to simply go through all the cells in a Excel Spreadsheet and check the values in the cells. The cells may contain text, numbers or be blank. I am not very familiar / comfortable working with the concept of 'Range'. Therefore, any sample codes would be greatly appreciated. (I did try to google it, but the code snippets I found didn't quite do what I needed)
谢谢。
推荐答案
Sub CheckValues1()
Dim rwIndex As Integer
Dim colIndex As Integer
For rwIndex = 1 To 10
For colIndex = 1 To 5
If Cells(rwIndex, colIndex).Value <> 0 Then _
Cells(rwIndex, colIndex).Value = 0
Next colIndex
Next rwIndex
End Sub
在 http://www.java2s.com/Code/VBA-Excel-Access-Word/Excel/Checksvaluesinarange10rowsby5columns.htm 作为一个函数来说明检查值的方法似乎是非常有用的
Found this snippet on http://www.java2s.com/Code/VBA-Excel-Access-Word/Excel/Checksvaluesinarange10rowsby5columns.htm It seems to be quite useful as a function to illustrate the means to check values in cells in an ordered fashion.
想像一下它是一个二维阵列,并应用相同的逻辑来循环细胞。
Just imagine it as being a 2d Array of sorts and apply the same logic to loop through cells.
这篇关于如何遍历Excel VBA或VSTO 2005中的所有单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!