2017-11-13 6 views
1

Iは、例えば、数字の列があると:のExcel VBA - 正と負の数の対を強調

1。 -1; 5; 4; 3; -3; -3; 3; -4;

複数のペアが表示されるという事実を考慮しながら、正と負の数(例:1と-1)のすべてのペアをハイライトできるマクロを作成したいとします(例3と - 3は両方とも2回出現する)。また、作業したい範囲を入力できるようにしたい。上記の例の場合

、すべての数字は5と7

を除いて強調表示されますここで私は上記の例では、これまで

Sub HighlightExercise() 

Set myRange = Application.InputBox(prompt:="Sample", Type:=8) 

myRange.Interior.ColorIndex = 2 

For Each cell In myRange 

If cell.Interior.ColorIndex = 30 Then Next 

Set CValue = Cell.Value 

myRange.Select 

Set CFind = Selection.Find(What:=CValue.Value * -1, After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False) 

If CFind.Value = Null Then Next 

If CFind.Interior.ColorIndex = 30 Then Next 

CFind.Interior.ColorIndex = 30 

CValue.Interior.ColorIndex = 30 

Next 

End Sub 

思い付いたものだ、それは「コンパイルエラーと言います、For Next For For "の条件があります。私は "Next Cell"と "Next iteration"を試しましたが、まだ何もありませんでした。私は何を得ていないのですか?

+0

のでわからない** 'if cell.Interior.ColorIndex = 30 Then Next'の構文?この行は何をすると思われますか?また、 'If CFind.Interior.ColorIndex = 30 Then Next'? –

答えて

6

の代わりに:あなたは、反対のためにテストし、trueの場合、それはコードを実行できるようにする必要があり

If cell.Interior.ColorIndex = 30 Then Next

If cell.Interior.ColorIndex <> 30 Then 

はまた、あなたが多くので.Valueを使用それを許可しない場所:

Set CValue = Cell.Value

であるべき:

Set CValue = Cell 

しかしCellが既に範囲であると、本当にそれが必要とされていません。

はあなたの変数を宣言することを忘れないでください:

検索でも
Dim myRange As Range 
Dim cell As Range 
Dim cfind As Range 

、我々はダウンし、現在のセルから検索したいので変更:

After:=ActiveCell

To

After:=cell 
.Selectを使用して

避けはちょうどあなたが範囲でやりたい:

Set cfind = myRange.Find... 

は、try:私は** VBAに精通しています

Sub HighlightExercise() 
Dim myRange As Range 
Dim cell As Range 
Dim cfind As Range 
Set myRange = Application.InputBox(prompt:="Sample", Type:=8) 

myRange.Interior.ColorIndex = 2 

For Each cell In myRange 

    If cell.Interior.ColorIndex <> 30 Then 

     Set cfind = myRange.Find(What:=cell.Value * -1, After:=cell, LookIn:= _ 
      xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
      xlNext, MatchCase:=False, SearchFormat:=False) 
     If Not cfind Is Nothing Then 
      If cfind.Interior.ColorIndex <> 30 Then 
       cfind.Interior.ColorIndex = 30 
       cell.Interior.ColorIndex = 30 
      End If 
     End If 
    End If 
Next 

enter image description here

関連する問題