Vba 宏仅在可见单元格上运行
我想知道如何使宏只在可见(未隐藏)单元格上运行? 我应该向该代码添加什么:Vba 宏仅在可见单元格上运行,vba,excel,macros,Vba,Excel,Macros,我想知道如何使宏只在可见(未隐藏)单元格上运行? 我应该向该代码添加什么: Dim cell As Range For Each cell In ActiveSheet.Cells.SpecialCells(xlCellTypeVisible) With Application.FindFormat.Interior .PatternColorIndex = xlAutomatic .Color = 16711935 .Tint
Dim cell As Range
For Each cell In ActiveSheet.Cells.SpecialCells(xlCellTypeVisible)
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = 16711935
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True).Activate
Next cell
Exit Sub
如果它没有检查公式,您可以更改
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible)
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible)
到
或者,如果是检查公式,请更改
到
这将防止它在未隐藏的空白单元格上运行 如果没有检查公式,您可以更改
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible)
ActiveSheet.Cells.SpecialCells(xlCellTypeVisible)
到
或者,如果是检查公式,请更改
到
这将防止它在未隐藏的空白单元格上运行 这将激活具有特定格式的所有可见单元格
Sub Example()
Dim SearchRange As Range
Dim c As Range, FoundRange As Range
Dim firstAddress As String
On Error Resume Next
Set SearchRange = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not SearchRange Is Nothing Then
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = 16711935
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Set c = SearchRange.Find(What:="", After:=SearchRange.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True)
If Not c Is Nothing Then
firstAddress = c.Address
Set FoundRange = c
Do
Set c = SearchRange.Find(What:="", After:=c, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True)
If Not c Is Nothing Then
'Do Something
Set FoundRange = Union(FoundRange, c)
End If
Loop While Not c Is Nothing And c.Address <> firstAddress
FoundRange.Activate
End If
End If
End Sub
子示例()
将搜索范围变暗为范围
尺寸c作为范围,FoundRange作为范围
将第一个地址设置为字符串
出错时继续下一步
设置SearchRange=ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
错误转到0
如果不是SearchRange则为Nothing
使用Application.FindFormat.Interior
.PatternColorIndex=xlAutomatic
.Color=16711935
.TintAndShade=0
.PatternTintAndShade=0
以
设置c=SearchRange.Find(What:=“”,After:=SearchRange.Cells(1,1),LookIn:=xlformals,LookAt:=_
xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False_
,SearchFormat:=True)
如果不是,那么c什么都不是
firstAddress=c.地址
设置FoundRange=c
做
设置c=SearchRange.Find(What:=“”,After:=c,LookIn:=xlFormulas,LookAt:=_
xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False_
,SearchFormat:=True)
如果不是,那么c什么都不是
“做点什么
Set FoundRange=Union(FoundRange,c)
如果结束
循环而不是c为Nothing,c.Address为firstAddress
FoundRange,激活
如果结束
如果结束
端接头
这将激活具有特定格式的所有可见单元格
Sub Example()
Dim SearchRange As Range
Dim c As Range, FoundRange As Range
Dim firstAddress As String
On Error Resume Next
Set SearchRange = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not SearchRange Is Nothing Then
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = 16711935
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Set c = SearchRange.Find(What:="", After:=SearchRange.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True)
If Not c Is Nothing Then
firstAddress = c.Address
Set FoundRange = c
Do
Set c = SearchRange.Find(What:="", After:=c, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True)
If Not c Is Nothing Then
'Do Something
Set FoundRange = Union(FoundRange, c)
End If
Loop While Not c Is Nothing And c.Address <> firstAddress
FoundRange.Activate
End If
End If
End Sub
子示例()
将搜索范围变暗为范围
尺寸c作为范围,FoundRange作为范围
将第一个地址设置为字符串
出错时继续下一步
设置SearchRange=ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
错误转到0
如果不是SearchRange则为Nothing
使用Application.FindFormat.Interior
.PatternColorIndex=xlAutomatic
.Color=16711935
.TintAndShade=0
.PatternTintAndShade=0
以
设置c=SearchRange.Find(What:=“”,After:=SearchRange.Cells(1,1),LookIn:=xlformals,LookAt:=_
xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False_
,SearchFormat:=True)
如果不是,那么c什么都不是
firstAddress=c.地址
设置FoundRange=c
做
设置c=SearchRange.Find(What:=“”,After:=c,LookIn:=xlFormulas,LookAt:=_
xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False_
,SearchFormat:=True)
如果不是,那么c什么都不是
“做点什么
Set FoundRange=Union(FoundRange,c)
如果结束
循环而不是c为Nothing,c.Address为firstAddress
FoundRange,激活
如果结束
如果结束
端接头
您的代码现在如何工作?它是如何功能失调的?功能失调是当前代码在整个工作表中搜索,即使搜索的值在隐藏行中-它找到了它(不应该找到它)好的,但我看到你有一个可接受的答案,所以我不会进一步评论。我有。仍然感谢您的好意:)您的代码现在运行得如何?它是如何功能失调的?功能失调是当前代码在整个工作表中搜索,即使搜索的值在隐藏行中-它找到了它(不应该找到它)好的,但我看到你有一个可接受的答案,所以我不会进一步评论。我有。仍然感谢您的好意:)是的,但宏不必对找到的单元格执行任何操作。它只需找到它们并选择:)是的,但宏不必对找到的单元格执行任何操作。它只需要找到它们并选择:)我想它不会检查公式,因为我唯一感兴趣的是细胞颜色:)谢谢你的帮助,顺便说一下:)-Alex,Thomas Inzina我想它不会检查公式,因为我唯一感兴趣的是细胞颜色:)谢谢你的帮助顺便说一下:)-Alex,Thomas Inzina