基于列名和单元格值的Excel宏加粗

时间:2017-03-24 18:39:17

标签: excel vba excel-vba

我有下面的代码,但它只在A列中查找。我希望Macro首先找到哪个列,然后按单元格值进行搜索。

实施例。列名是" Days" (excel表格中的某个地方)和Cell Value是" Saturday"," Sunday"。所以这种方式如果周六或周日在那一行而不是那一列它不会大胆。这可能吗?在此先感谢您的帮助。

Sub Bold_Row_Based_on_Call_Value_and_Column_Name()

Dim cRow As Long
Dim rRow As Range
Dim LastRow As Long
'Gets the last row with data in it
LastRow = [A65000].End(xlUp).Row
'the look to move down the cells
For cRow = 1 To LastRow
'if statment so catch the values that are wanted
If Cells(cRow, 1) = "Saturday" Or Cells(cRow, 1) = "Sunday" Then
'the changes made to the rows
Rows(cRow).Font.Bold = True
End If
Next cRow
End Sub

1 个答案:

答案 0 :(得分:1)

将您的If声明更改为:

If WorksheetFunction.CountIf(Rows(cRow), "Sunday") > 0 Or WorksheetFunction.CountIf(Rows(cRow), "Saturday") > 0 Then
        Rows(cRow).Font.Bold = True
End If

这将检查“星期日”和“星期六”的行,如果它在那里,则加粗行。

编辑:根据您的评论,“日期”可以在任何列中,然后这应该有效:

Sub Bold_Row_Based_on_Call_Value_and_Column_Name()
Dim cRow As Long, LastRow As Long, daysCol As Long
Dim rRow As Range
'Gets the last row with data in it
LastRow = [A65000].End(xlUp).Row
'the look to move down the cells

daysCol = Rows(1).Find(what:="Days").Column

For cRow = 1 To LastRow
'if statment so catch the values that are wanted
    If Cells(cRow, daysCol) = "Saturday" Or Cells(cRow, dateCol) = "Sunday" Then
        'the changes made to the rows
        Rows(cRow).Font.Bold = True ' This will bold the entire row
        'Cells(cRow, daysCol).Font.Bold = True 'This will bold just the DAY cell
    End If
Next cRow
End Sub