VBA用于下一个 循环通过一个范围



对于范围数据的第一行,一切正常。当我执行"下一个单元格"时,它向下移动到第二行数据,但第一个值来自第二列数据。

示例:数据来自列 K:N。第一个数据在第 2 行。当它转到"下一个单元格"时,该行变为 3,但 cell.value = L 列第 3 行的值,而不是 K 列第 3 行的值。这正常吗?当我移动到下一个 C 并且行更改时,它不应该从最左侧的列开始吗?谢谢!

Function Modifer_Analysis()
Dim modRng As Range
Dim cell As Range
Dim i As Integer
Dim col As Integer
Dim rowTrack As Double
Dim modComp As String
Range("K2:N17914").Select
Set modRng = Selection
rowTrack = 2
For Each cell In modRng
If rowTrack = cell.row Then
If i = 0 And IsEmpty(cell.Value) = False Then
modComp = cell.Value
Else
End If
If i = 1 And IsEmpty(cell.Value) = False Then
modComp = modComp & "," & cell.Value
Else
End If
If i = 2 And IsEmpty(cell.Value) = False Then
modComp = modComp & "," & cell.Value
Else
End If
If i = 3 And IsEmpty(cell.Value) = False Then
modComp = modComp & "," & cell.Value
Else
End If
i = i + 1
Else
i = 0
rowTrack = cell.row
modComp = ""
End If
Next cell

End Function

目前还不清楚你想要实现什么,但也许更像这样的东西将是一个好的开始:

Function Modifer_Analysis()
Dim cell As Range, rw As Range
Dim modComp As String, sep As String
'loop over rows
For Each rw In Range("K2:N17914").Rows 'needs a worksheet qualifier here
modComp = ""
sep = ""
For Each cell In rw.Cells
'loop over cells in row
If Len(cell.Value) > 0 Then
modComp = modComp & sep & cell.Value
sep = ","
End If
Next cell
Next rw
End Function

最新更新