Question Only? why one vba code works and another wont, even though should have same results.

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
202
Office Version
  1. 2016
Platform
  1. Windows
I have a perplexing question. It’s not a problem because I can work with the results, its just understanding why it will not work with a modification in code that should return the same results but doesn’t.



This is my base code…..

VBA Code:
Dim WS As Worksheet

Set WS = ShGE03

With WS

Dim SDR_E As Long: SDR_E = 5 'StartDataRow_(column)E = ColorName

Dim LDR_E As Long: LDR_E = .Cells(Rows.Count, "E").End(xlUp).Row ' LastDataRow_(column)E = ColorName 'do not offset for blanks, will affect some code for (missing & duplicates)

Dim Rowdata As Long '''For Rowdata = SDR_E To LDR_E 'for reference

Dim TextChange As Long

TextChange = .Range("V5").Value

End With



I have one code that works well…..

VBA Code:
With WS

Call TurnOffStuff 'Turns off background Excel Functions that might interfere with calculations

For Rowdata = SDR_E To LDR_E

Dim RANGE_E As Range

Set RANGE_E = .Range("E" & Rowdata)

Dim RANGE_F As Range

Set RANGE_F = .Range("F" & Rowdata)

Dim RANGE_G As Range

Set RANGE_G = .Range("G" & Rowdata)

Dim RANGE_H As Range

Set RANGE_H = .Range("H" & Rowdata)

Dim RANGE_M As Range

Set RANGE_M = .Range("M" & Rowdata)



RANGE_E.Interior.Color = RGB(RANGE_F.Value, RANGE_G.Value, RANGE_H.Value)

If RANGE_M.Value >= TextChange Then

RANGE_E.Font.Color = RGB(0, 0, 0) 'black

Else

RANGE_E.Font.Color = RGB(255, 255, 255) 'white

End If

Next

Call TurnOnStuff 'Turns on background Excel Functions that might interfere with calculations

End With



This code works well in that it gives the actual cell color based on values in F, G, H. Such as this screenshot shows…..





But if I change my code to the LDR_E format it doesn’t work even though I believe it’s the same results….

VBA Code:
With WS

Call TurnOffStuff 'Turns off background Excel Functions that might interfere with calculations

Dim RANGE_E As Range

Set RANGE_E = .Range("E5:E" & LDR_E)

Dim RANGE_F As Range

Set RANGE_F = .Range(“F5:F" & LDR_E)

Dim RANGE_G As Range

Set RANGE_G = .Range(“G5:G" & LDR_E)

Dim RANGE_H As Range

Set RANGE_H = .Range (“H5:H" & LDR_E)

Dim RANGE_M As Range

Set RANGE_M = .Range(“M5:M" & LDR_E)

RANGE_E.Interior.Color = RGB(RANGE_F.Value, RANGE_G.Value, RANGE_H.Value)

If RANGE_M.Value >= TextChange Then

RANGE_E.Font.Color = RGB(0, 0, 0) 'black

Else

RANGE_E.Font.Color = RGB(255, 255, 255) 'white

End If

Next

Call TurnOnStuff 'Turns on background Excel Functions that might interfere with calculations

End With


Why will this not work, all I get is blank column E?
 

Attachments

  • GOODsceenshoot.png
    GOODsceenshoot.png
    49.6 KB · Views: 6

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Because the first code is using a single cell value for each part of the RGB while the 2nd code code is trying to put an array of values in each part of the RGB
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top