我遇到了突出顯示列使用範圍的問題。下面的代碼創建兩個工作表的副本,刪除一些值,然後應該突出顯示某些列。試圖突出顯示列的使用範圍
Sub CreateAnalysisSheets()
Dim cell, HlghtRng As Range
Dim i As Integer
Dim ref, findLast, findThis As String
Dim lastRow As Long
findLast = "2016"
findThis = "2017"
Application.ScreenUpdating = False
Sheets(1).Copy After:=Sheets(2)
ActiveSheet.Name = Left(Sheets(1).Name, InStr(1, Sheets(1).Name, " ")) & "Analysis"
Sheets(2).Copy After:=Sheets(3)
ActiveSheet.Name = Left(Sheets(2).Name, InStr(1, Sheets(2).Name, " ")) & "Analysis"
Sheets("RM Analysis").Select
For Each cell In ActiveSheet.UsedRange
If cell.Value = "NULL" Then
cell.ClearContents
End If
Next cell
For Each cell In Range("1:1")
ref = cell.Value
lastRow = Range("R" & Rows.Count & "C" & cell.Column).End(xlUp).Row
Set HlghtRng = Range(Cells(1, cell.Column) & Cells(lastRow, cell.Column))
If InStr(1, ref, findLast) > 0 And InStr(1, ref, "YTD") = 0 Then
HlghtRng.Interior.ColorIndex = 8
End If
Next cell
For Each cell In Sheets(4).UsedRange
If cell.Value = "NULL" Then
cell.ClearContents
End If
Next cell
Sheets("RM Analysis").Select
Application.ScreenUpdating = True
End Sub
,問題就出現在哪裏lastRow = Range("R" & Rows.Count & "C" & cell.Column).End(xlUp).Row
我得到一個Method 'Range' of Object '_Global' Failed
。我試過尋找解決這個問題的方法,但我試過的一切(ActiveSheet.Range
和Sheets("RM Analysis").Range
)還沒有工作。
任何人都可以看到我要去哪裏嗎?
感謝您的建議。我絕對同意'With ... End With',並且已經做出了這些修改,包括對xlR1C1語法的修復。現在我在'Set HlghtRng'位得到'應用程序定義或對象定義的錯誤'。我不確定爲什麼這麼簡單會給我帶來很多麻煩 –
請參閱上面的建議修改。 – Jeeped
完美!這麼小但很大的變化。謝謝你的幫助! –