如果要逐单元格迭代,可以执行以下操作:
Sub PrintCellContentsToDebugWindow()
Dim ws As Worksheet
Dim rng As Range
Dim intCounterRow As Integer
Dim intCounterCol As Integer
Dim intMaxRow As Integer
Dim intMaxCol As Integer
Set ws = ActiveSheet 'OR '
'Set ws = ActiveWorkbook.Sheets("Sheet1") '
intCounterRow = 1
intCounterCol = 1
intMaxRow = 100
intMaxCol = 25
Do While intCounterCol <= intMaxCol
intCounterRow = 1
Do While intCounterRow <= intMaxRow
Set rng = ws.Cells(intCounterRow, intCounterCol)
Debug.Print rng.Address & " " & rng.Value
intCounterRow = intCounterRow + 1
Loop
intCounterCol = intCounterCol + 1
Loop
End Sub
上面的代码遍历前100行和前25列,并在visualbasic编辑器的Debug窗口中打印出单元格地址及其值。