2011-09-19 2 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 인 모든 행의 서식을 변경할 수 있습니까? 그렇다면 .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 
0

난 당신이 VBA에 대해 뭔가를 물어 알고 있지만, 이런 경우에, 당신은 conditional formating 사용을 고려할 수 있습니다. 이 적용되지 있다면, 당신은

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

또는

Cells([Fill Me]).Interior.Color = vbRed 
로 갈 수 있습니다
관련 문제