94451
Goto Top

In einer Funktion im Makro Cellfarben Manipulieren

Hi,

ich möchte eine Funktion schreiben welche nach "Errechnung" ein anderes Feld Farblich markiert.

Ich weiß, das könnte man über bedingte Formatierung machen indem man ein weiteres Feld Manipuliert, aber das geht so um 3 Ecken und bedarf zusatzaufwand :/ ... am liebsten hätte ich das alles im Code

meine Funktion

Function chknum(val)
    'Debug.Print val.Address '(False, False, xlR1C1)  
    chknum = val * 2
    'Call color  
    'Worksheets("Tabelle1").Range("C9").Interior.Color = 12961275  
    'Range("$C$5").Interior.Color = 12961275  

End Function

eigentlich kann man im Code schon erkennen was ich vorhabe bzw. was ich versucht habe

ich schreibe in Feld A1 eine Zahl
ins Feld A2 steht chknum(A1)

jetzt soll (hier C5 oder C9 <- erstmal egal wo) eine Farbliche Veränderung gemacht werden...
Leider kann ich weder einen Call noch (was schöner wäre) direkt die Farbe verändern.

Kennt jemand den "richtigen Weg" oder wenn es den nicht gibt einen Trick (wie gesagt, am liebsten wäre mir reiner VBA-Code um hier das einfach auf viele Mappen und Zellen anwenden zu können)?

Vielen Dank
Gruß RoadRunner

Content-Key: 314112

Url: https://administrator.de/contentid/314112

Printed on: April 25, 2024 at 09:04 o'clock

Mitglied: 129813
129813 Sep 01, 2016 updated at 06:57:11 (UTC)
Goto Top
Hi.
Zitat von @94451:
Ich weiß, das könnte man über bedingte Formatierung machen indem man ein weiteres Feld Manipuliert,
Why manipulating another field ??? You can use a conditional formatting based on a formula face-wink .. no additional field necessary.
aber das geht so um 3 Ecken und bedarf zusatzaufwand :/
No
... am liebsten hätte ich das alles im Code
To be able to manipulate other cells from "formula functions" you need Application.Volatile
Wie kann man in Excel in einer Zelle alle Großbuchstaben finden und farbig machen?
But using Application.Volatile is very bad practice cause it slows down massively and can cause crashes. It's better to use conditional formatting based on formulas.

Regards
Mitglied: 94451
94451 Sep 01, 2016 at 07:34:55 (UTC)
Goto Top
Why manipulating another field ??? You can use a conditional formatting based on a formula .. no additional field necessary.

nun wie beschrieben möchte ich das nicht... die Formeln die dahinter stecken sind etwas "komplexer" ... <- hier werden zig Daten "Zwischen geprüft"... es wird eine Validierung...

Application.Volatile
Leider habe ich viele "funktionierende" beispiele gefunden womit ich sehen kann welche Farbe Zelle XY hat... aber ich konnte keine "Funktion" finden mit welcher ich Farben "Setzen" / Verändern kann...
Mitglied: 129813
129813 Sep 01, 2016 updated at 07:43:00 (UTC)
Goto Top
Zitat von @94451:

Application.Volatile
Leider habe ich viele "funktionierende" beispiele gefunden womit ich sehen kann welche Farbe Zelle XY hat... aber ich konnte keine "Funktion" finden mit welcher ich Farben "Setzen" / Verändern kann...
??? You have not read the link! You set Application.Volatile in the head of your function, that's it.
After that you will be able to set the colors of other cells, because normaly this is not allowed inside a formula function, with the above addition it can be done, but is bad practice.
Worksheets("Tabelle1").Range("C9").Interior.Color = vbRed  

I would do it with placing your code inside the Worksheet_Change() event and check the cells for changes and react accordingly, instead of using a formula function.
Mitglied: 94451
94451 Sep 01, 2016 at 07:51:16 (UTC)
Goto Top
Doch,

ich habe es gelesen

und genau so:
Function chknum(val As Range)
Application.Volatile
Worksheets("Tabelle1").Range("C9").Interior.color = vbRed  
'val.Interior.color = 12961275  

    Debug.Print val.Address '(False, False, xlR1C1)  

    chknum = val.Value * 2
End Function

umgesetzt... aber es funktioniert nicht...

die idee mit Worksheet Chance ist nicht schlecht... darüber denk ich mal nach ...

=> es wäre trotzdem interessant wie ich das SO lösen kann!? <- brauch ich hierfür noch eine Bibliothek?
Mitglied: 129813
129813 Sep 01, 2016 updated at 08:13:45 (UTC)
Goto Top
Seems not to work with Interior.Color, sorry , but assigning Font.Color works. That's why i said better don't use it face-wink

Make it with the worksheet change event:

Example to react to a change in specific range of cells:
Private Sub Worksheet_Change(ByVal Target As Range)
    Set rngChange = Range("A1:A20")  
    If Not Application.Intersect(rngChange, Target) Is Nothing Then
        Range("B1").Interior.Color = vbRed  
    End If
End Sub