编程宏来着色行

时间:2011-09-19 15:57:33

标签: excel vba

我正在尝试编写一个宏,用于为找到0的行着色 当前列中的值。我不确定如何解决它,因为 它只会向右忽略其余的第一个Range选择。

如何告诉命令选择当前右侧的所有单元格 一列到T列?

  Sub FindAndColor()
  '
  ' FindAndColor Macro
  '
  ' Keyboard Shortcut: Ctrl+Shift+D
  '
      Cells.Find(What:="0", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
          xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
          False, SearchFormat:=False).Activate
      Range(Selection, Selection.End(xlToRight)).Select
      Range(Selection, Selection.End(xlToRight)).Select
      Range(Selection, Selection.End(xlToRight)).Select
      Range(Selection, Selection.End(xlToRight)).Select
      Selection.Style = "Bad"
  End Sub

另外,我想把它放在当前一个下方的单元格中?有什么需要 代替“A1”,以便它是当前的单元而不是A1。

  Range("A1").Offset(0,1)

任何帮助表示赞赏,

泰德

2 个答案:

答案 0 :(得分:3)

您是否尝试遍历工作表的所有行并更改当前所选列为0的任何行的格式?如果是这样,下面的内容将不使用.Select(.Select is evil)。

Sub FindAndColor2()
    'This code will find each row with 0 in the currently selected column
    '   and color the row from column 1 to 20 (A to T) red and set the
    '   font to bold.
    Dim row As Long
    Dim col As Long


'Get the column of the current selection
col = Selection.Column
'Loop through every row in the active worksheet
For row = 1 To ActiveSheet.UsedRange.Rows.Count
    'If the cell's value is 0.
    If ActiveSheet.Cells(row, col).Text = 0 Then
        'Put your formatting inside this with.  Note that the 20 in .cells(row, 20)
        '   is column T.
        With ActiveSheet.Range(ActiveSheet.Cells(row, 1), ActiveSheet.Cells(row, 20))
            .Interior.Color = vbRed
            .Font.Bold = True
            .Style = "Bad"
            'Other formatting here
        End With
    End If
Next row
End Sub

答案 1 :(得分:0)

我知道你问了一些关于VBA的事情,但在这种情况下,你可能想考虑使用conditional formating。如果它不适用,您可以使用

Range([Fill Me]).Interior.Color = vbRed

Cells([Fill Me]).Interior.Color = vbRed
相关问题