excel - 为行着色的宏编程

标签 excel vba

我正在尝试编写一个宏,为找到 0 的行着色
当前列中的值。我不确定如何解决它,因为
它只在右侧进行第一个范围选择,而忽略其余部分。

如何告诉命令选择当前右侧的所有单元格
一个到 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)

任何帮助表示赞赏,

泰德

最佳答案

您是否尝试遍历工作表的所有行并更改当前选定列为 0 的任何行的格式?如果是这样,以下将在不使用 .Select 的情况下工作(.Select 是邪恶的)。

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

关于excel - 为行着色的宏编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7473804/

相关文章:

excel - 平均值的最大值优于

vba - 使我的 .accdb 成为 .accde 后,表单事件不会触发

vba - 屏幕更新效果

excel - 在 OneDrive 中创建新文件夹

python - 从 Python : macros may be disabled 运行 VBA 代码

excel - 完成行的自动格式

vba - IE导航到新页面后如何获取html文档?

通过 VBA 更改行高从 Access 导出 Excel

excel - 使用 VBA 和 WinSCP 同步本地和远程文件

ms-access - 文本框为空问题