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
Rangeto a specific sheet. And don't useActiveSheet. UseWith Worksheets("mySheet") | Set rn Source = .Range("SN_TP1"), for example (pipe is line separator).r2to 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.