2011-09-19 45 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