Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

InStr(ActiveCell.Formula...

Forumthread: InStr(ActiveCell.Formula...

InStr(ActiveCell.Formula...
Rolf
Guten Tag zusammen,
ich möchte, dass die Hintergrundfarbe einer Zelle automatisch vergeben wird, wenn in dieser Zelle sowohl ein "+"-Zeichen als auch ein "-"-Zeichen vorkommt. Also z.B. '-16,32+3,65+1,25'
Dazu habe ich folgendes gebastelt:

Private Sub Worksheet_Change(ByVal Target As Range)
If InStr(ActiveCell.Formula, "-") And InStr(ActiveCell.Formula, "+") Then
ActiveCell.Interior.ColorIndex = 6
End If
End Sub

Wenn ich nur "-" oder "+"-Werte abfrage (geänderte If-Verzweigung) funktioniert es.
In Kombination der beiden Parameter aber nur dann, wenn der negative Wert Was mache ich falsch?
Gruß, Rolf
Anzeige

5
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: InStr(ActiveCell.Formula...
02.07.2012 18:30:47
Hajo_Zi
Hallo Rolf,
ich hätte

If InStr(ActiveCell.Formula, "-")>0 or InStr(ActiveCell.Formula, "+") >0 Then

geschrieben

AW: InStr(ActiveCell.Formula...
02.07.2012 18:38:01
Rolf
Hallo Hajo,
Super, Danke!
Gruß, Rolf
Anzeige
ActiveCell statt Target?
02.07.2012 18:45:56
Erich
Hi ihr beiden,
ActiveCell ist die Zelle, in defr der C ursor nach der Änderung in Target steht, meist rechts oder unter Target.
Rolf, probier mal:

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
MsgBox ActiveCell.Address
MsgBox Target.Address
MsgBox .Formula
MsgBox InStr(.Formula, "+")
MsgBox InStr(.Formula, "-")
MsgBox InStr(.Formula, "-") And InStr(.Formula, "+")
MsgBox InStr(.Formula, "-") > 0 Or InStr(.Formula, "+") > 0
MsgBox InStr(.Formula, "-") > 0 And InStr(.Formula, "+") > 0
MsgBox InStr(.Formula, "-") * InStr(.Formula, "+")
If InStr(.Formula, "-") * InStr(.Formula, "+") Then
.Interior.ColorIndex = 6
Else
.Interior.ColorIndex = xlColorIndexNone
End If
End With
End Sub
Rückmeldung wäre nett! - Grüße aus Kamp-Lintfort von Erich
Anzeige
Ergänzung
02.07.2012 18:56:47
Erich
Hi Rolf,
hier noch ein paar Anzeigen und noch eine richtige Möglichkeit mehr:

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
'      MsgBox ActiveCell.Address
'      MsgBox Target.Address
If Target.Cells.Count = 1 Then
'        MsgBox .Formula
'        MsgBox InStr(.Formula, "+")
'        MsgBox InStr(.Formula, "-")
MsgBox InStr(.Formula, "-") And InStr(.Formula, "+")         ' falsch
MsgBox InStr(.Formula, "-") > 0 Or InStr(.Formula, "+") > 0  ' falsch
MsgBox InStr(.Formula, "-") > 0 * InStr(.Formula, "+") > 0   ' falsch
MsgBox InStr(.Formula, "-") > 0 And InStr(.Formula, "+") > 0   ' ok
MsgBox (InStr(.Formula, "-") > 0) * (InStr(.Formula, "+") > 0) ' ok
MsgBox InStr(.Formula, "-") * InStr(.Formula, "+")             ' ok
If InStr(.Formula, "-") * InStr(.Formula, "+") Then
.Interior.ColorIndex = 6
Else
.Interior.ColorIndex = xlColorIndexNone
End If
End If
End With
End Sub
Rückmeldung wäre nett! - Grüße aus Kamp-Lintfort von Erich
Anzeige
AW: Ergänzung
02.07.2012 19:25:50
Rolf
Hallo Erich,
Dank auch an dich.
Obwohl ich so was ausgeflipptes mit MsgBox momentan nicht brauche, ist es evtl. für andere Anwendungen recht hilfreich. Aber die Code-Zeile ".Interior.ColorIndex = xlColorIndexNone" hat mein Makro schon mal um 4 Zeilen reduziert. - Nach Eingabe eines Datums in Spalte D meiner Tabelle muss die Hintergrundfarbe nämlich wieder neutralisiert werden. Nochmals Danke.
Gruß, Rolf
Anzeige
;

Forumthreads zu verwandten Themen

Anzeige
Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige