是否可以在excel中选择仅位于具有数据的单元格之上的单元格(需要选择的单元格下方的单元格需要具有数据)?如果列中的每个单元格都有数据,则只应选择顶部单元格。如果单元格上方或下方没有数据单元格,则不应选中它。谢谢
我发现这个宏可以选择范围内的所有数据单元,是否可以为"下面的数据添加一个IF"进入这个?我试过,我对VBA非常环保。谢谢。
Sub SelectNotBlackRange()
'Update20131220
Dim Rng As Range
Dim WorkRng As Range
Dim OutRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
If Rng.Value <> "" Then
If OutRng Is Nothing Then
Set OutRng = Rng
Else
Set OutRng = Union(OutRng, Rng)
End If
End If
Next
If Not OutRng Is Nothing Then
OutRng.Select
End If
End Sub
答案 0 :(得分:0)
以下是典型列的示例,列 C :
Sub CSelect()
Dim kolumn As String, N As Long
Dim i As Long
kolumn = "C"
N = Cells(Rows.Count, kolumn).End(xlUp).Row
For i = 1 To N
If Cells(i, kolumn).Value <> "" And Cells(i + 1, kolumn).Value <> "" Then
Cells(i, kolumn).Select
Exit Sub
End If
Next i
End Sub
注:
这将仅选择符合选择标准的第一个单元格。
修改#1:强>
子选择符合条件的列中的所有单元格:
Sub CSelectALL()
Dim kolumn As String, N As Long
Dim i As Long, rng As Range
kolumn = "C"
N = Cells(Rows.Count, kolumn).End(xlUp).Row
Set rng = Nothing
For i = 1 To N
If Cells(i, kolumn).Value <> "" And Cells(i + 1, kolumn).Value <> "" Then
If rng Is Nothing Then
Set rng = Cells(i, kolumn)
Else
Set rng = Union(rng, Cells(i, kolumn))
End If
End If
Next i
If rng Is Nothing Then
Else
rng.Select
End If
End Sub