2011-09-13 11 views
6

मैं एक्सेल 2007 में अपने सशर्त स्वरूपण के लिए रंग स्केल का उपयोग कर रहा हूं और मुझे सशर्त रूप से स्वरूपित कोशिकाओं के लिए भरने वाले रंग कोड को खोजने में कठिनाई हो रही है। मुझे आंतरिक पता है। रंग डिफ़ॉल्ट रंग मान देता है लेकिन यह सशर्त स्वरूपण का उपयोग करते समय मदद नहीं करता है। मैं रिले में आश्चर्यचकित हूं कि यह कितना मुश्किल है।मैं vba का उपयोग कर एक्सेल 2007 में सशर्त रूप से स्वरूपित सेल का भरण रंग मान कैसे प्राप्त करूं?

धन्यवाद।

+0

यहां देखें: http://stackoverflow.com/questions/996384/excel-2007- शर्त-formatting-how-to-get-cell-color – RBarryYoung

उत्तर

2

नीचे दिया गया कोड VBAExpress से लिया गया था, सभी क्रेडिट भी मूल लेखक - byundt।

यह आवश्यकता हो सकती है एक्सेल 2007 के लिए

Original Link

Function ConditionalColor(rg As Range, FormatType As String) As Long 
    'Returns the color index (either font or interior) of the first cell in range rg. If no _ 
    conditional format conditions apply, Then returns the regular color of the cell. _ 
    FormatType Is either "Font" Or "Interior" 
    Dim cel As Range 
    Dim tmp As Variant 
    Dim boo As Boolean 
    Dim frmla As String, frmlaR1C1 As String, frmlaA1 As String 
    Dim i As Long 

    'Application.Volatile 'This statement required if Conditional Formatting for rg is determined by the _ 
    value of other cells 

    Set cel = rg.Cells(1, 1) 
    Select Case Left(LCase(FormatType), 1) 
    Case "f" 'Font color 
     ConditionalColor = cel.Font.ColorIndex 
    Case Else 'Interior or highlight color 
     ConditionalColor = cel.Interior.ColorIndex 
    End Select 

    If cel.FormatConditions.Count > 0 Then 
     'On Error Resume Next 
     With cel.FormatConditions 
      For i = 1 To .Count 'Loop through the three possible format conditions for each cell 
       frmla = .Item(i).Formula1 
       If Left(frmla, 1) = "=" Then 'If "Formula Is", then evaluate if it is True 
        'Conditional Formatting is interpreted relative to the active cell. _ 
        This cause the wrong results If the formula isn 't restated relative to the cell containing the _ 
        Conditional Formatting--hence the workaround using ConvertFormula twice In a row. _ 
        If the Function were Not called using a worksheet formula, you could just activate the cell instead. 
        frmlaR1C1 = Application.ConvertFormula(frmla, xlA1, xlR1C1, , ActiveCell) 
        frmlaA1 = Application.ConvertFormula(frmlaR1C1, xlR1C1, xlA1, xlAbsolute, cel) 
        boo = Application.Evaluate(frmlaA1) 
       Else 'If "Value Is", then identify the type of comparison operator and build comparison formula 
        Select Case .Item(i).Operator 
        Case xlEqual ' = x 
         frmla = cel & "=" & .Item(i).Formula1 
        Case xlNotEqual ' <> x 
         frmla = cel & "<>" & .Item(i).Formula1 
        Case xlBetween 'x <= cel <= y 
         frmla = "AND(" & .Item(i).Formula1 & "<=" & cel & "," & cel & "<=" & .Item(i).Formula2 & ")" 
        Case xlNotBetween 'x > cel or cel > y 
         frmla = "OR(" & .Item(i).Formula1 & ">" & cel & "," & cel & ">" & .Item(i).Formula2 & ")" 
        Case xlLess ' < x 
         frmla = cel & "<" & .Item(i).Formula1 
        Case xlLessEqual ' <= x 
         frmla = cel & "<=" & .Item(i).Formula1 
        Case xlGreater ' > x 
         frmla = cel & ">" & .Item(i).Formula1 
        Case xlGreaterEqual ' >= x 
         frmla = cel & ">=" & .Item(i).Formula1 
        End Select 
        boo = Application.Evaluate(frmla) 'Evaluate the "Value Is" comparison formula 
       End If 

       If boo Then 'If this Format Condition is satisfied 
        On Error Resume Next 
        Select Case Left(LCase(FormatType), 1) 
        Case "f" 'Font color 
         tmp = .Item(i).Font.ColorIndex 
        Case Else 'Interior or highlight color 
         tmp = .Item(i).Interior.ColorIndex 
        End Select 
        If Err = 0 Then ConditionalColor = tmp 
        Err.Clear 
        On Error GoTo 0 
        Exit For 'Since Format Condition is satisfied, exit the inner loop 
       End If 
      Next i 
     End With 
    End If 

End Function 
7

संशोधित करने की आप fomatting की स्थिति (क्या सेल वर्तमान में है नहीं) तो तरह के इंटीरियर रंग का उपयोग कर सकते हैं, वहाँ इस संभालने पहली शर्त सेल पर लागू किया जाता है:

Range("A1").FormatConditions(1).interior.color 

यहाँ एक समारोह है कि सभी सशर्त स्वरूपों एक सेल के लिए रंग कोड वापस आ जाएगी है शामिल हैं। यदि कोई शर्त नहीं है तो यह कुछ भी वापस नहीं करेगा, और यदि कोई शर्त है लेकिन इसके लिए कोई रंग सेट नहीं है, तो यह आपको "कोई नहीं" बताता है।

Function ConditionalColor(ByVal cell As Range) 

Dim colors As String 
Dim i As Long 

For i = 1 To Range(cell.Address).FormatConditions.count 
    If Range(cell.Address).FormatConditions(i).Interior.Color <> 0 Then 
     colors = colors & "Condition " & i & ": " & _ 
     Range(cell.Address).FormatConditions(i).Interior.Color & vbLf 
    Else 
     colors = colors & "Condition " & i & ": None" & vbLf 
    End If 
Next 

If Len(colors) <> 0 Then 
    colors = Left(colors, Len(colors) - 1) 
End If 

ConditionalColor = colors 

End Function 

अद्यतन: मामले में आप उत्सुक हैं (मैं) था, कि Excel का उपयोग करता है रंग कोड वास्तव में बीजीआर, आरजीबी है नहीं। तो अगर आप आरजीबी मूल्यों के लिए कोड में परिवर्तित करना चाहता था, तो आप इस का उपयोग कर सकते हैं:

Function GetRGB(ByVal cell As range) As String 

Dim R As String, G As String 
Dim B As String, hexColor As String 
hexCode = Hex(cell.Interior.Color) 

'Note the order excel uses for hex is BGR. 
B = Val("&H" & Mid(hexCode, 1, 2)) 
G = Val("&H" & Mid(hexCode, 3, 2)) 
R = Val("&H" & Mid(hexCode, 5, 2)) 

GetRGB = R & ":" & G & ":" & B 
End Function 
5

हाय जवाब आपके द्वारा दिए गए काम नहीं किया क्योंकि मैं एक रंग पैमाने का उपयोग कर रहा है, तो यह सामान्य 3 शर्त मान वापस नहीं करता है ।

अधिक खोज के बाद मुझे एक कामकाज मिल गया जो काम करता है। यह डेटा लेना और इसे शब्द में रखना है, फिर इसे वापस एक्सेल में कॉपी करें जिससे सीमा सेल में एक वास्तविक रंग में जा सके ताकि आंतरिक हो। रंग काम करेगा। मैंने किसी ऐसे व्यक्ति को पाया जो इसे ले गया और उसे वीबीए में डाल दिया। अगर कोई और ऐसा करने की तलाश में है तो link यहां है।

+0

यह सही है, फ़ंक्शन की दूसरी पंक्ति 'अगर रेंज (सेल .Address) .FormatConditions (i) .Interior।रंग <> 0 फिर ' उत्पन्न करता है> त्रुटि 438' ऑब्जेक्ट इस प्रॉपर्टी या विधि का समर्थन नहीं करता है ' > ऐसा इसलिए है क्योंकि हम 3 से अधिक स्थितियों का उपयोग करते हैं। आपका कामकाज (शब्द में चिपकाना और फिर एक्सेल में वापस) सबसे अच्छा विकल्प प्रतीत होता है। – ProtoVB

+1

क्या किसी को पता है कि एक्सेल से वर्ड में पेस्ट करते समय क्या होता है। अगर हम समझते हैं कि हम रंग कोड निकाल सकते हैं। – ProtoVB

-3

आसान तरीका: स्क्रीन को स्प्रेडशीट प्रिंट करें। इसे पेंट में पेस्ट करें। रंग खोजने के लिए पिपेट टूल का उपयोग करें। रंग संपादित करें पर क्लिक करें।

बूम अपने आरजीबी जानकारी मिली है कि आप इनपुट एक्सेल में वापस कर सकते हैं

2

मैं एक जवाब है कि Excel 2007 या उससे पहले के साथ लेकिन Excel 2010 से काम करता है के बाद आप उपयोग कर सकते हैं की जरूरत नहीं है निम्नलिखित (रेंज बदल रहा है सूट करने के लिए):

Range("A1").DisplayFormat.Interior.ColorIndex 

सौभाग्य से, जबकि सॉफ्टवेयर है जिसके लिए मैं इसकी आवश्यकता Excel 2003 पर समर्थित है के बाद से, मैं केवल वास्तव में यह एक परीक्षण प्रक्रिया में की आवश्यकता होती है, और परीक्षण मॉड्यूल उत्पादन संस्करणों से निकाल दिया जाता है।

संबंधित मुद्दे