首页 > 解决方案 > 将文本格式化为粗体

问题描述

此公式根据该行中包含特定值的单元格为整行着色。

For Each Cell In .Range("Y5:" & .Range("Y1500").End(xlDown).Address)
    If .Cells(Cell.Row, 25).value = "Super Project" Then
        Cell.EntireRow.Interior.Color = vR(WorksheetFunction.RandBetween(1, n))

    End If
Next
End With

如何使“B”列中的单元格也变为粗体?

标签: excelvba

解决方案


加粗单元格:Range.Font.Bold = True

您的目标单元格位于Col Y(Col 25) 中,因此要到达Col B(Col 2),您需要向后退(偏移值)23 列,结果是:

Cell.Offset(0,-23).Font.Bold = True


我强迫症的一面坚持建议你

  1. 正确缩进
  2. 修改循环范围(见代码)
  3. Y= 列25。不需要.Cells(Cell.Row, 25)。只分析变量,Cell
  4. 最后,区分您声明Cells对象变量Cell可能会造成混淆并导致错误。更改为类似的东西,这样区别就很明显了。CellMyCell

Dim MyCell as Range

With Sheets(1) 'Some Sheet
    For Each MyCell In .Range("Y5:Y" & .Range("Y" & .Rows.Count).End(xlUp).Row)
        If MyCell = "Super Project" Then
            MyCell.EntireRow.Interior.Color = vR(WorksheetFunction.RandBetween(1, n))
            MyCell.Offset(,-23).Font.Bold = True
        End If
    Next
End With

推荐阅读