Vba 如何通过硬编码列';s的标题名是?
我希望对两列数据运行一系列条件格式规则。我知道我可以选择一个列字母和该列中的所有单元格,但为了使我的宏更完整,我希望它搜索列的标题并在两个特定标题下的单元格上运行格式设置 我目前使用记录宏工具来查看这可能如何工作,这是我当前的设置。这个宏确实有效,我只想让它完全证明,并在工作表中搜索特定的列标题名称。我想在woorksheet中搜索列标题“标题1”和“标题2” 试试这个:Vba 如何通过硬编码列';s的标题名是?,vba,excel,Vba,Excel,我希望对两列数据运行一系列条件格式规则。我知道我可以选择一个列字母和该列中的所有单元格,但为了使我的宏更完整,我希望它搜索列的标题并在两个特定标题下的单元格上运行格式设置 我目前使用记录宏工具来查看这可能如何工作,这是我当前的设置。这个宏确实有效,我只想让它完全证明,并在工作表中搜索特定的列标题名称。我想在woorksheet中搜索列标题“标题1”和“标题2” 试试这个: Sub ApplyCF() Dim ws As Worksheet Dim rng As Range, fcel As R
Sub ApplyCF()
Dim ws As Worksheet
Dim rng As Range, fcel As Range, hrng As Range
Dim myheaders, header
Dim mycondition As String
myheaders = Array("Header 1", "Header 2")
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set rng = .Range("A1", .Range("A1").Offset(0 _
, .Columns.Count - 1).End(xlToLeft).Address)
'Debug.Print headers.Address
End With
For Each header In myheaders
Set fcel = rng.Find(header, rng.Cells(rng.Cells.Count))
If Not fcel Is Nothing Then
If hrng Is Nothing Then
Set hrng = fcel.EntireColumn
Else
Set hrng = Union(hrng, fcel.EntireColumn)
End If
End If
Next
'Debug.Print hrng.address
hrng.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT1", _
TextOperator:=xlContains
With hrng.FormatConditions(hrng.FormatConditions.Count)
With .Font
.Color = -16383844
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
.StopIfTrue = False
End With
hrng.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT2", _
TextOperator:=xlContains
With hrng.FormatConditions(hrng.FormatConditions.Count)
With .Font
.Color = -16751204
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 10284031
.TintAndShade = 0
End With
.StopIfTrue = False
End With
End Sub
我只假设您的标题在第一行。根据需要调整代码。您可以设置一个查找页眉的例程。完成此操作后,可以使用
.ModifyAppliesToRange
方法为所需列指定格式。请参阅post,它将为您提供一个如何操作的示例。我收到一条运行时错误消息,在“hrng.FormatConditions.Add Type:=xlTextString,String:=”CONTAINSTEXT1“,”处停止宏。您知道为什么吗?我将ws=Activesheet更改为在我正在处理的指定工作表上运行宏。您将代码放在哪里了?它应该在一个模块中。
Sub ApplyCF()
Dim ws As Worksheet
Dim rng As Range, fcel As Range, hrng As Range
Dim myheaders, header
Dim mycondition As String
myheaders = Array("Header 1", "Header 2")
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set rng = .Range("A1", .Range("A1").Offset(0 _
, .Columns.Count - 1).End(xlToLeft).Address)
'Debug.Print headers.Address
End With
For Each header In myheaders
Set fcel = rng.Find(header, rng.Cells(rng.Cells.Count))
If Not fcel Is Nothing Then
If hrng Is Nothing Then
Set hrng = fcel.EntireColumn
Else
Set hrng = Union(hrng, fcel.EntireColumn)
End If
End If
Next
'Debug.Print hrng.address
hrng.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT1", _
TextOperator:=xlContains
With hrng.FormatConditions(hrng.FormatConditions.Count)
With .Font
.Color = -16383844
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
.StopIfTrue = False
End With
hrng.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT2", _
TextOperator:=xlContains
With hrng.FormatConditions(hrng.FormatConditions.Count)
With .Font
.Color = -16751204
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 10284031
.TintAndShade = 0
End With
.StopIfTrue = False
End With
End Sub