PDA

View Full Version : Code for IF statement referencing two more sheets and color formatting



Dodo
04-06-2022, 02:11 PM
[SIZE=3]Hi! I need help with this code. I am only learning VBA and this is still too "complex" for me...

What I want:
For each cell (in a range I defy) of ws1 (blood measurements) IF a cell value in this range is > a specific cell value in ws2 (norm ranges for this blood measurement) AND the value (age of participant) in ws3 is below the value I enter in the code (i.e. 19 years) THEN color the cell (I chose yellow).

(So if in ws1 we start to look at value Z4 of participant 1 then the code has to compare the corresponding age value of this participant in ws3 which is cell D4).

I have to repeat this code then for each column in ws1 and change the references that occur for ws2. I think that I can do that without an issue but the first code part.. I give up for now.

Here is the code I got so far but it doesn't work obviously ��:

Sub KlinChemie()
Dim Cell As Range

Set ws1 = Worksheets("Blood samples")
Set ws2 = Worksheets("Normwerte Blut")
Set ws3 = Worksheets("Alter_berechnet")

For Each Cell In ws1.Range("Z4:Z93")
If (Cell.Value > ws2.Value("I13") And ws3.Value("D4:D93") <= 19) Then
Cell.Interior.Color = 65535
End If
Next Cell
End Sub

See sample file attached. I have been working in VBA "Tabelle7 (Blood samples)" to get this to execute.

Thank you for any help :D

SamT
04-06-2022, 05:58 PM
To Start fixing
ws2.Range("I13").Value

Also a multi-cell range cannot be referenced like =< 19. You must check each cell in the range individually

Sorry, I don't have Office on this computer, so I can't see your attachment.

Dodo
04-24-2022, 02:03 PM
To Start fixing
ws2.Range("I13").Value

Also a multi-cell range cannot be referenced like =< 19. You must check each cell in the range individually

Sorry, I don't have Office on this computer, so I can't see your attachment.

Thank you for your help! I found a solution now.

snb
04-24-2022, 02:22 PM
Kennst du 'Clever Excel Forum.de' ?