2016-06-10 21 views
0

fand ich diesen Code auf Stackoverflow VBA (highlight Hardcode cell (i.e.1234) in Excel) after model is builtMarkieren Sie die Zellen in einem Bereich, der „hartcodiert“ sind und nicht die Formel abgeleitet

Public Sub hightlightNoFormulas() 
    Dim yourRange as Range, rangeNoFormula as Range 
    Set yourRange = Range("A1:A100") 
    Set rangeNoFormula = yourRange.SpecialCells xlCellTypeFormulas 

Then loop through your range, excluding any values that have formulas 

    Dim rng as Range 
     For Each rng in yourRange 
      If Intersect(rng,rangeNoFormula) Is Nothing Then 
       rng.interior.Color = 65535 
      End If 
     Next rng 
    Exit Sub 

Obwohl die "Set rangeNoFormula = yourRange.SpecialCells xlCellTypeFormulas" Abschnitt Fehler heraus in Excel 2010. Ich gesucht haben Code, der Zellen in einem auswählbaren Bereich hervorhebt, die "fest codiert" sind und nicht von der Formel abgeleitet sind (dh wo die Zellenformel übertippt wurde). Kann mir bitte jemand helfen? Vielen Dank.

+0

Wenn Sie ändern ~ xlCellTypeFormulas ~ To ~ xlCellTypeAllFormatConditions. ~ Oder ~ xlCellTypeSameFormatConditions. ~ Macht es Ihnen helfen. – skkakkar

+0

Werfen Sie einen Blick auf die erste Antwort, wenn Sie VBA-Spezialzellen google und es zeigt Ihnen, dass die Syntax falsch ist. 'yourrange.specialcells (xlCellTypeFormulas)' – gtwebb

Antwort

1

ich diesen Code ended zu verändern und ein Eingabefeld Hinzufügen um den Bereich auszuwählen, den ich untersuchen möchte. Der Code funktioniert gut für meine Bedürfnisse. Danke allen!

Public Sub hightlightNoFormulas() 
    Dim yourRange As Range, rangeNoFormula As Range 

On Error GoTo GetOut  'if no hard coded cells are found in the range, go to "GetOut" to exit Sub 

    'Set Range as popup input box. 
Set yourRange = Application.InputBox(_ 
Prompt:="Select a range.", _ 
Title:="INPUT RANGE", _ 
Default:=Selection.Address, Type:=8) 

    'Type: Value Meaning 
    '0 A Formula 
    '1 A Number 
    '2 Text (a string) 
    '4 A logical value (True or False) 
    '8 A cell reference, as a Range object 
    '16 An error value, such as #N/A 
    '64 An array of values 

    On Error GoTo GetOut  'if no hard coded cells are found in the range, go to "GetOut" to exit Sub 

    Set rangeNoFormula = yourRange.Cells.SpecialCells(xlCellTypeConstants)  'identifies cells without formulas 
    rangeNoFormula.Interior.Color = 42495 

GetOut:  'exits the sub 

End Sub 
0

Bereiche haben eine integrierte Boolean-Funktion namens HasFormula(), die Sie verwenden können. Ihr Code würde wie folgt aussehen (Ich reinigte es ein kleines bisschen zu hoch):

Public Sub hightlightNoFormulas() 
Dim yourRange as Range, rng as Range 
    Set yourRange = Range("A1:A100") 

    For Each rng in yourRange 
     If Not rng.HasFormula Then 
      rng.interior.Color = 65535 
     End If 
    Next rng 
End Sub 

ich von http://www.exceltrick.com/how_to/find-cells-containing-formulas-in-excel/ diese Idee bekam

+0

Danke - es funktioniert super! – XLmatters

0

Hier ist eine andere Lösung das gleiche Beispiel geschrieben werden. BGeorge sieht auch korrekt aus.

Public Sub hightlightNoFormulas() 
    Dim yourRange As Range 
    Dim rangeNoFormula As Range 
    Dim rng As Range 

    Set yourRange = Range("A1:A100") 
    Set rangeNoFormula = yourRange.SpecialCells(xlCellTypeFormulas) 

    For Each rng In yourRange 
     If Intersect(rng, rangeNoFormula) Is Nothing Then rng.Interior.Color = 65535 
    Next rng 
End Sub 
+0

Danke. Ja, BGeorges Version funktionierte wie beabsichtigt. – XLmatters

3

Sie brauchen nicht eine Schleife .............. nur Farbkonstanten:

Public Sub hightlightNoFormulas() 
    Dim yourRange As Range, rangeNoFormula As Range 
    Set yourRange = Range("A1:A100") 
    Set rangeNoFormula = yourRange.Cells.SpecialCells(xlCellTypeConstants) 
    rangeNoFormula.Interior.Color = 65535 
End Sub 
+0

Noch einfacher, danke – XLmatters