基于列名和单元格值的Excelmacros粗体

我有下面的代码,但它看起来只在列A中。我想要macros先find哪个列是前进,并通过单元格值进行search。

例。 列名称是“天”(在Excel表中的某处),单元格值是“星期六”,“星期天”。 所以如果星期六或星期天是在那一行,但不是那个列,它不会是粗体。 这可能吗? 在此先感谢您的帮助。

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 

把你的If语句改成这样:

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

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

编辑:根据您的评论,“date”可以在任何列,然后这应该工作:

 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