Vba 在选定范围的行中循环时处理单个单元格
我有下面的代码来循环选择范围的每一行。但是,当只选择一个单元格时,代码将在工作表中的每一行循环,而不仅仅处理一个实例 我需要做什么才能使for循环在选择单个单元格时只处理一行Vba 在选定范围的行中循环时处理单个单元格,vba,excel,for-loop,row,Vba,Excel,For Loop,Row,我有下面的代码来循环选择范围的每一行。但是,当只选择一个单元格时,代码将在工作表中的每一行循环,而不仅仅处理一个实例 我需要做什么才能使for循环在选择单个单元格时只处理一行 Dim myRange as Range Dim currRow as Range Set myRange = Selection For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible) MsgBox currRow.Address Ne
Dim myRange as Range
Dim currRow as Range
Set myRange = Selection
For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible)
MsgBox currRow.Address
Next currRow
谢谢,我不知道为什么代码会这样。看起来不错。
但要得到您想要的,请尝试以下方法:
Dim myRange As Range
Dim currRow As Range
Set myRange = Selection
If myRange.Rows.count = 1 And myRange.Columns.count = 1 Then
For Each currRow In myRange.Rows
MsgBox currRow.Address
Next currRow
Else
For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible)
MsgBox currRow.Address
Next currRow
End If
希望这能起作用。只需在代码中添加一个if语句来处理隐藏行:
Dim myRange As Range
Dim currRow As Range
Set myRange = Selection
For Each currRow In myRange
If currRow.EntireRow.Hidden = False Then
'Place your code here.
Debug.Print currRow.Address
End If
Next currRow