0

I am building a workbook that contains test results for control parts that company I work for manufactures. There are 28 parts with 6 performance criteria each.

I am trying to "grab" a value for each part and parameter to be used in conditional formatting formula: =rnSource +/- (rnSource*Tolerance).

Each part number has a SN and 6 test point results (TPs). I named the ranges (single cells) as SN_TPx (x, being 1, 2, 3,...6). These named cells are in a different worksheet than conditional formatting target worksheets (28 of these, 1/SN). Individual worksheet contain current run test results (sheets are named using a SN). Below code worked when I used SN_TPx in place of rnSource.

Reason: I can reuse the code with modification of one "Dim" rather than copy/paste code 168 times and changing cell references throughout the code (8 occurrences). I have seven other product lines to apply this to.

In my code I attempted: Dim rnSource As Range, then assign SN_TPx to rnSource using Set rnSource = SN_TPx. This results in:

Run-time error '424': Object required

I tried rnSource =:
SN_TPx.Value - '424' error

Range(SN_TPx).Value:

Run-time error '1004': Method 'Range' of object '_Global' failed

Range("SN_TPx").Value
Code runs but my Conditional Formatting formula results in:

Cell Value < rnSource-(rnSource*Tolerance)

which doesn't seem to return value of SN_TPx rather rnSource text (formatted value font is not affected)

Worksheet("Sheet3").Range(SN_TPx)

Compile error: Sub or Function not defined (Worksheet)

Worksheets("Sheet3").Range("SN_TPx")

Run-time error '9': Subscript out of range

Sub TP1_CondFormat_AV()
'***   Attempt to handle ranges (source and target) of CondFormat by setting dims and ranges upfront_
' then execute CondFormat routine for each set of dims and ranges

With ActiveSheet

'** par - define needed variables
Dim r1 As Range
Dim r2 As Range
Dim TP1_Range As Range
Dim TP2_Range As Range

'Dim rnSource As Variant ' fail
'Dim rnSource As Integer ' fail
Dim rnSource As Range
Dim rnTarget As Range
Dim wsSource As Worksheet
Dim wsTarget As Worksheet

'*** par - create a range called "rnTarget" which consists of r1 and r2 ranges
Set r1 = Range("L2:L4")
Set r2 = Range("L7:L1048576")
Set rnTarget = Union(r1, r2)
rnTarget.Name = "TP1"    'Name the rnTarget as "TP1"

Set rnSource = Range(SN_TP1).Value  '== troubleshooting, unable to point rnSource to "SN_TP1" value

End With

rnTarget.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=rnSource-(rnSource*Tolerance)"
rnTarget.FormatConditions(rnTarget.FormatConditions.Count).SetFirstPriority
    With rnTarget.FormatConditions(1).Font
        .Bold = True
        .Color = -4165632
        .TintAndShade = 0
    End With
    rnTarget.FormatConditions(1).StopIfTrue = False
    rnTarget.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=rnSource+(rnSource*Tolerance)"
    rnTarget.FormatConditions(rnTarget.FormatConditions.Count).SetFirstPriority
    With rnTarget.FormatConditions(1).Font
        .Bold = True
        .Color = -16776961
        .TintAndShade = 0
    End With
    rnTarget.FormatConditions(1).StopIfTrue = False

End Sub
4
  • In addition to @urdearboy comment, explicitly assign Range to a specific sheet. And don't use ActiveSheet. Use With Worksheets("mySheet") | Set rn Source = .Range("SN_TP1"), for example (pipe is line separator). Commented Apr 28, 2020 at 15:30
  • I'd recommend not setting r2 to cover every row. Are you really working on every single row on your sheet? Bets practice is to limit your code to the scope of your actual range. Commented Apr 28, 2020 at 15:33
  • the r1 and r2 with the Union is to account for a some headers in the Sheet (which I don't want to format). Reason for r2 covering every row is to allow for new data that will be added over the years to come. I don't know how to do dynamic range yet Commented Apr 28, 2020 at 15:44
  • thank you for your lightning quick replies. I do appreciate it Commented Apr 28, 2020 at 15:45

2 Answers 2

1

Thank you for your input @urdearboy and @Scott Holtzman. Your answers were correct and helped me figure out why my proposed method will not work.

When I Set rnSource = .Range("SN_TPx") it does assign SN_TPx value to rnSource (Debug.Print confirms it) but...worksheet does not recognize rnSource name. Thus, the Conditional Format formula will not work since it contains Cell < =rnSource-(rnSource*Tolerance)

[CondFormatFail.jpg][1]

This means that despite changing rnSource value by changing SN_TPx, in the end my whole workbook would end up with the same rnSource value...last one assigned to it.

For now I will have to hard code SN_TPx into my conditional formatting and apply to appropriate ranges. With time I will streamline it and make it more efficient.

Thank you again!

Sign up to request clarification or add additional context in comments.

Comments

0

If you are just trying to create a VBA Range variable for a Excel named range you just have to add quotes. Note that you should remove the .Value you currently have as that is a property of a range (example of how to access the value of your variable is below)


Set rnSource = Range("SN_TP1")
'Debug.Print rnSource.Value

6 Comments

This method almost worked for me. The conditional formatting ends up with a formula that looks like "Cell Value < rnSource - (rnSource*Tolerance)" but font is not formatted. How can I add a screenshot?
That sounds like a separate issue which is not the intent of this question. If you can reference the cell now, then your question is answered
Fair enough but I was attempting to replace SN_TPx with a single refernce that I can adjust to each TP and SN by changing it while defining it (Dim rnSource As Range than change 101_TP1 in Set rnSource) otherwise I have to change 8 times per SN per TP.
Sounds like you are trying to loop or something? You may need to revise your question because that’s not clear
in short, yes. I just have practically no experience so looping seems out of reach still. learning as I go. What got works but to apply it fully takes a lot of find/replace, not robust or scalable. You are also correct that my question is likely not clear...I am having a hard time to express what I need.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.