2011-09-19 23 views
1

我想编写一个宏,颜色它发现当前列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) 

赞赏任何帮助,

特德

回答

3

您是通过纸张的所有行试图循环和改变任何一行在当前所选列是0的格式?如果是这样,下面将不使用。选择(。选择是邪恶的)。

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 
0

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

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

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