2016-07-30 4 views
1

私の質問に似た性質の質問があります。私はVBAで、アクティブなセルの日付とセルの色に一致するセルを見つけ、次の一致するセルを見つけた後、H列の対応するセルに移動し、シアンを強調表示します。これは私が望むだけで動作しますが、毎回実行マクロをクリックする必要があります。私は関数がすべての一致するセルで動作するようにします。私はDo Untilループを使用することを考えています。@http://www.excel-easy.com/vba/examples/do-until-loop.htmlが見つかりましたが、これを行うには、ループを止めるために一致するセルの数を知る必要があります。CountIFからアクティブの値と色に一致するセル数を取得する方法と、VBA ExcelでCountIFで検出されたセル数に等しいループコードを取得する方法はありますか?

私の作業コード:

Sub Test1() 
' 
' Test1 Macro 
' 
' 
Dim CellColor As Variant 
Dim SearchDate As String, FoundAt As String 


CellColor = Range("B" & ActiveCell.Row).Interior.Color 
SearchDate = Range("B" & ActiveCell.Row).NumberFormat 

    Range("B" & ActiveCell.Row).Select 
    Application.FindFormat.Clear 
    Application.FindFormat.NumberFormat = SearchDate 
    Application.FindFormat.Interior.Color = CellColor 

    Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _ 
     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _ 
     , SearchFormat:=True).Activate 

End Sub 

私は上記のリンクで見つかった使用についての考えていたループコード:

Dim cellCount As Integer 
cellCount = Application.WorksheetFunction.CountIf(Range("B1:B30"), "7/22/2016") ‘Count matching 

MsgBox cellCount ‘test to see if count works 

Dim i As Integer 
i = 1 

Do Until i > 6 
    Cells(i, 1).Value = 20 
    i = i + 1 
Loop 

私はCOUNTIFのこのコードを持っていますしかし、それには2つの問題があります。問題1、変数 "SearchDate"を使用する代わりに、コードに手動で日付 "7/22/2016"を入力する必要があります。問題2は、日付のみを検索し、日付を検索するセルの色ではなく私のスプレッドシート。

私の質問はこれのすべてです。アクティブなセルの日付と色の値を満たす日付の数を取得し、その数値をループ内で使用する変数に渡すにはどうすればよいですか?

もっと効率的な方法があれば教えてください。多くの事前に感謝!

参考写真:

Screenshot showing the spreadsheet of dates and colors.

+0

B8およびB13。したがって、H8とH13はシアンでハイライト表示されます。それはあなたが欲しいものですか? –

+0

Sorta、私はアクティブな細胞の細胞数を探したい。したがって、B4がアクティブなセルの場合は、セルをアクティブにするセルの数を調べたいと思います。この場合、カウントは3です。 – Munstr

答えて

0

私が正しくあなたの質問を理解している場合、これはあなたが次の関数は、列Bで一致するすべてのセルをカウントします

Sub Test1() 

Dim CellColor As Variant 
Dim SearchDate As String 

For i = 1 To 19 
    CellColor = Cells(i, 2).Interior.Color 
    SearchDate = Cells(i, 2).NumberFormat 

    If Cells(i, 2).Value <> "" Then 
     For j = i To 19 
      If i <> j And CellColor = Cells(j, 2).Interior.Color And SearchDate = Cells(j, 2).NumberFormat Then 
       Cells(j, 8).Interior.Color = RGB(0, 255, 255) 
      End If 
     Next j 
    End If 
Next i 

End Sub 
+0

私はできる限りこれを試してみます。私は19がどこから来るのか不思議です。 – Munstr

+0

乱数のみ、19行をスクリーンショットに表示されているように選択してください –

+0

ああ、意味があります。ありがとうございました。 – Munstr

0

を探しています何です セルをハイライト表示しません。あなた自身でこの部分を追加する必要があります。それは単にあなたのコードに次を追加使用するために

Dim x As Integer 
x = countMatchingCells 

機能:私は、あなたの質問を理解スクリーンショットでで発見されたB4の試合を提供している場合

Function countMatchingCells() As Integer 

Dim CellColor As Variant 
Dim SearchDate As String, FoundAt As String 
Dim i As Integer 
Dim counter As Integer 

CellColor = Range("B" & ActiveCell.Row).Interior.Color 
SearchDate = Range("B" & ActiveCell.Row).NumberFormat 
counter = 0 
'assuming we are working on sheet1: 
For i = 1 To Sheets(1).Cells(Sheets(1).Rows.Count, 2).End(xlUp).Row 
If Cells(i, 2).Interior.Color = CellColor And _ 
Cells(i, 2).NumberFormat = SearchDate Then 
countMatchingCells = countMatchingCells + 1 
'you can write more code here, for example if you want to highlight cells once you found a match 
End If 
Next i 
End Function 
+0

ありがとう、私はチャンスを得るときにこれを試してみましょう。 – Munstr

関連する問題