I am getting a runtime error on this exact line.
Range("Aqual").Offset(0, i).Interior.color = rg.Offset(0, i + 11).Interior.color
I am simply trying to match color of a range in one worksheet to another range in a different worksheet. I have tried various troubleshooting rewrites of this line and tried to contain this out into a separate sub and it still fails. (I have these troubleshooting lines commented out and they all also fail.) I still can't get it and have wasted days trying to figure this out. The Range("Aqual")
is a named range in sheet1
"F8". The for
loop is so it moves across 6 columns. My worksheets are not protected nor is my workbook.
Option Explicit
Dim rg As Range
Dim i As Integer
Function posABC(ByVal A As String, ByVal B As String, ByVal C As String, ByVal D As String, ByVal G As Integer) As String
On Error GoTo errormsg
Application.EnableEvents = False
Dim output As String
Dim code As String
Dim priKVA As String
Dim secKVA As String
Dim readCode As String
Dim secOffset As Integer
Dim writeRange As Range
Dim readRange As Range
output = "empty code"
code = A & B & C
If G >= 0 And G <= 3 Then
secOffset = 0
Else
secOffset = G - 3
End If
For Each rg In Range("tblPosA")
readCode = rg.Value & rg.Offset(0, 1).Value & rg.Offset(0, 2).Value
If readCode = code Then
priKVA = rg.Offset(0, 4).Value
If D = "1" Then
secKVA = rg.Offset(1, 5 + secOffset).Value
Else
secKVA = rg.Offset(0, 5 + secOffset).Value
End If
For i = 0 To 5
Range("Aqual").Offset(0, i).Interior.color = rg.Offset(0, i + 11).Interior.color
' Set writeRange = Range("Aqual").Offset(0, i)
' Set readRange = rg.Offset(0, i + 11)
' writeRange.Interior.color = readRange.Interior.color
' Call matchColor(Range("Aqual").Offset(0, i), rg.Offset(0, i + 11))
' Range("F8").Interior.color = RGB(0, 255, 0)
' Select Case True
' Case rg.Offset(0, i + 11).Interior.color = vbRed
' Range("Aqual").Offset(0, i).Interior.color = vbRed
' Case rg.Offset(0, i + 11).Interior.color = vbGreen
' Range("Aqual").Offset(0, i).Interior.color = vbGreen
' Case rg.Offset(0, i + 11).Interior.color = vbYellow
' Range("Aqual").Offset(0, i).Interior.color = vbYellow
' End Select
Next i
Exit For
End If
Next rg
output = "Primary kVA= " & priKVA & vbNewLine & "Secondary kVA= " & secKVA
posABC = output
Application.EnableEvents = True
errormsg:
MsgBox Err.Description & " " & Err.Number
End Function
If use this sub for the operation it still fails.
Sub matchColor(ByVal writeRange As Range, ByVal readRange As Range)
writeRange.Interior.color = readRange.Interior.color
End Sub
And for some reason I don't know. This sub does work when I was trying to isolate the failure.
Sub colortest()
Dim writeRange As Range
Dim readRange As Range
Dim rg As Range
Set rg = Sheet7.Range("L6")
Set writeRange = Range("Aqual").Offset(0, 0)
Set readRange = rg.Offset(0, i)
writeRange.Interior.color = readRange.Interior.color
End Sub