2014-09-08 2 views
0

그런 어리석은 질문에 대해 유감스럽게 생각합니다. 나는 프로그래머가 아니며 인터넷을 통해 발견 한 비트로 매크로를 만들고 있습니다. 단지 코드의 라인의 쿠페와 함께 떠났을 때 나는 대답을 얻을 수 없다.Excel VBA는 조건부 형식 지정을위한 수식으로 다른 셀의 값을 사용합니다.

조건부 작성 비트를 제외하고 매크로에 필요한 모든 작업을 수행했습니다. 것은 내가 현재 가지고있는 셀 위의 4 행의 값보다 큰 셀에 형식 (녹색 셀)을 적용한 다음 다른 형식 (적색 셀)을 낮은 값의 셀에 적용하는 것입니다. 같은 셀 (위의 4 행)의 값이 무엇이든간에이 값은 음수가되어야합니다.

문제는 제가 매크로에 서있는 셀의 4 행보다 위에있는 값을 찾는 방법을 알지 못한다는 것입니다. 그리고 한 번 마쳤습니다. 매크로를 사용하여 기호를 변경하는 방법을 알려줍니다. (-1을 곱하면)

여기에 문제가있는 코드 조각이 있습니다. 당신이 볼 수 있듯이

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ 
     Formula1: 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    With Selection.FormatConditions(1).Font 
     .Color = -16752384 
     .TintAndShade = 0 
    End With 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 13561798 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ 
     Formula1: 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    With Selection.FormatConditions(1).Font 
     .Color = -16383844 
     .TintAndShade = 0 
    End With 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 13551615 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 

, 나는 Formula1을 떠 났어요 : 빈, 나는 4 개의 셀 나는

에있어 하나의 위의 값을 찾기 위해 매크로를 얘기하고 싶은 곳이다 다음 수식 1 : 비어있는 셀은 4 셀 위의 셀 값을 찾은 다음 -1로 값을 변경하거나 값을 음수로 변경하는 데 걸리는 모든 값을 필요로합니다.

여기에서 나를 도울 수 있기를 바랍니다. (내 기본 언어가 아닌 영어와 빈약 한 맞춤법을 유감스럽게 생각합니다.)

+0

의 중복 가능성 http://stackoverflow.com/questions/10802281/excel-vba-conditional-formatting-based-on-next-cell-value) – StorymasterQ

답변

0

실제로 포맷 조정을 사용할 필요가 없습니다. 어쨌든이 작업을 수행하는 방법을 보여주는 작업 코드는 다음과 같습니다. (-1)을 곱하면 위험합니다. 매크로를 두 번 이상 실행하면 어떻게 될지 상상해보십시오.

Option Explicit 
Sub conditional_formatting1() 
Dim rng As Range 
Set rng = Application.Selection 
Dim c As Range 

For Each c In rng.Cells 
    c.FormatConditions.Delete 

    If c.Value < c.Offset(-4).Value Then 

     'setting color to red 
     c.Interior.Color = RGB(255, 0, 0) 
     'multipyling -1 
     c.Value = c.Value * (-1) 

    Else 

     With c 


      .FormatConditions.Add xlExpression, Formula1:="=" & c.Address & ">" & c.Offset(-4) 
      'implementatio of what you wanted to do below is commented. 
      'uncomment it if you want to use it or continue using what I have done below 

'   With .FormatConditions(1).Font 
'    .Color = -16383844 
'    .TintAndShade = 0 
'   End With 
'   With .FormatConditions(1).Interior 
'    .PatternColorIndex = xlAutomatic 
'    .Color = 13551615 
'    .TintAndShade = 0 
'   End With 

      'changing cell color to green 
      c.Interior.Color = RGB(0, 255, 0) 
     End With 

    End If 
Next c 


End Sub 
[다음 셀 값에 기초하여 조건부 서식 엑셀 VBA (
+0

많은 분들께 감사드립니다. 코드를 약간 수정하여 전체 매크로와 함께 작업하게 만들었습니다. –

+0

마크를 다시 한 번 제공해 주셔서 감사합니다. 제발 또는 추가 정보를 게시하십시오. 감사 – luvlogic

관련 문제