2017-01-25 13 views
0

私はいくつかの範囲(rngSrcとrngTgt)を渡す必要がある次のコードを持っています。本質的にはいくつかの範囲の内容を別のサブに渡す

Sub Con_CCC() 

Dim arr, rngSrc As Range, rngTgt As Range, rng As Range, cell As Range 
Dim c As ColorStop 
Dim isGreen As Boolean 
Dim e As Long 

Worksheets("Index Changes").Range("P7:P24").ClearContents 

Set rngSrc = Sheets("Output").Range("J13:J100") 
Set rngTgt = Sheets("Index Changes").Range("Y7") 

    For Each cell In rngSrc 
    isGreen = False 
    On Error Resume Next 
    With cell.Interior.Gradient.ColorStops 
    End With 
    e = Err.Number 
    On Error GoTo 0 
    If e = 0 Then 
    For Each c In cell.Interior.Gradient.ColorStops 
     arr = LongToRGB(c.Color) 
     If arr(2)/IIf(arr(1) = 0, 1, arr(1)) > 1.25 And arr(2)/IIf(arr(3) = 0, 1, arr(3)) > 1.25 Then 
      isGreen = True 
      Exit For 
     End If 
    Next c 
    Else 
    arr = LongToRGB(cell.Interior.Color) 
    If arr(2)/IIf(arr(1) = 0, 1, arr(1)) > 1.25 And arr(2)/IIf(arr(3) = 0, 1, arr(3)) > 1.25 Then isGreen = True 
    End If 
    If isGreen Then 
    If rng Is Nothing Then Set rng = cell.Offset(, -1).Resize(, 2) Else Set rng = Union(rng, cell.Offset(, -1).Resize(, 2)) 
    End If 
Next cell 

If Not rng Is Nothing Then rng.Copy: rngTgt.PasteSpecial xlPasteValues 

End Sub 

私は次のコードが含まれ、その後、私の他のサブ年代の異なるrngSrcとrngTgtセットを取り、サブが必要になります。

For Each cell In rngSrc 
    isGreen = False 
    On Error Resume Next 
    With cell.Interior.Gradient.ColorStops 
    End With 
    e = Err.Number 
    On Error GoTo 0 
    If e = 0 Then 
    For Each c In cell.Interior.Gradient.ColorStops 
    arr = LongToRGB(c.Color) 
    If arr(2)/IIf(arr(1) = 0, 1, arr(1)) > 1.25 And arr(2)/IIf(arr(3) = 0, 1, arr(3)) > 1.25 Then 
     isGreen = True 
     Exit For 
    End If 
Next c 
Else 
    arr = LongToRGB(cell.Interior.Color) 
    If arr(2)/IIf(arr(1) = 0, 1, arr(1)) > 1.25 And arr(2)/IIf(arr(3) = 0, 1, arr(3)) > 1.25 Then isGreen = True 
End If 
If isGreen Then 
If rng Is Nothing Then Set rng = cell.Offset(, -1).Resize(, 2) Else Set rng = Union(rng, cell.Offset(, -1).Resize(, 2)) 
End If 
Next cell 

If Not rng Is Nothing Then rng.Copy: rngTgt.PasteSpecial xlPasteValues 

答えて

0

その後、あなたの "メイン" のコードがなるのは、 "ドイト" の後

Option Explicit 

Sub doit(rngSrc As Range, rngTgt As Range) 
    Dim cell As Range 
    Dim arr, rng As Range 
    Dim c As ColorStop 
    Dim isGreen As Boolean 
    Dim e As Long 

    For Each cell In rngSrc 
     isGreen = False 
     On Error Resume Next 
     With cell.Interior.Gradient.ColorStops 
     End With 
     e = Err.Number 
     On Error GoTo 0 
     If e = 0 Then 
      For Each c In cell.Interior.Gradient.ColorStops 
       arr = LongToRGB(c.Color) 
       If arr(2)/IIf(arr(1) = 0, 1, arr(1)) > 1.25 And arr(2)/IIf(arr(3) = 0, 1, arr(3)) > 1.25 Then 
        isGreen = True 
        Exit For 
       End If 
      Next c 
     Else 
      arr = LongToRGB(cell.Interior.Color) 
      If arr(2)/IIf(arr(1) = 0, 1, arr(1)) > 1.25 And arr(2)/IIf(arr(3) = 0, 1, arr(3)) > 1.25 Then isGreen = True 
     End If 
     If isGreen Then 
      If rng Is Nothing Then Set rng = cell.Offset(, -1).Resize(, 2) Else Set rng = Union(rng, cell.Offset(, -1).Resize(, 2)) 
     End If 
    Next cell 

    If Not rng Is Nothing Then rng.Copy: rngTgt.PasteSpecial xlPasteValues 
End Sub 

をあなたのサブを呼ぶことにしましょう:Jeweller89 @

Option Explicit 

Sub Con_CCC() 
    Dim rngSrc As Range, rngTgt As Range 

    Worksheets("Index Changes").Range("P7:P24").ClearContents 

    Set rngSrc = Sheets("Output").Range("J13:J100") 
    Set rngTgt = Sheets("Index Changes").Range("Y7") 

    doit rngSrc, rngTgt '<--| call your 'DoIt()' sub passing 'rngSrc' and 'rngTgt' ranges 
End Sub 
+0

、あなたはそれを介して取得しましたか? – user3598756

+0

@ Jeweller89、あなたを助けようとしている人々に適切なフィードバックを与えてくれてうれしいです。ありがとうございました – user3598756

関連する問題