2017-03-24 80 views
0

我有下面的代碼,但它只在列A中顯示。我想讓宏先找到列標題,然後按單元格值進行搜索。基於列名和單元格值的Excel宏到粗體

例子。列名稱是「天」(在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 
+1

比你先和另添加表的畫面/片段(一個運行宏後)?你的描述和例子很混亂! –

+0

如果列A中的單元格有「星期日」或「星期六」,您希望列A中的單元格變粗體?現在不是這樣嗎? – BruceWayne

回答

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 
+0

你好,對不起,這是我想要的相反,我只希望它大膽,如果列名稱「天」有一個「星期日」或「星期六」,並忽略其他列中的那些。 –

+0

這個專欄「天「(列A,B,C ...)??? –

+0

@DefcaTrick - 你只希望單詞「星期天」和/或「星期六」是否大膽?然後將'Rows(cRow).Font ...'改爲'Cells(cRow,1).Font.Bold = True'。否則,你能否更清楚你想要的粗體?這是有點不清楚... – BruceWayne

相關問題