我有两个工作表1和2。我在工作表2中有一个超链接,它将带我到工作表1中的单元格A2,并用黄色突出显示。但是同时它也删除了工作表1中单元格A1的颜色。
如何保持工作表1中单元格A1的颜色?
我正在工作表2中使用此vba代码
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim i As Long
For i = 1 To ThisWorkbook.Sheets.Count
Sheets(i).UsedRange.Interior.ColorIndex = -4142
Next
ActiveSheet.Cells.Interior.ColorIndex = red
Selection.Cells.Interior.ColorIndex = 6
End Sub
答案 0 :(得分:0)
假设您仍要删除.UsedRange
内的所有其他颜色,则可以在删除所有颜色之前保存单元格A1
的颜色,然后再为A1
重新着色。尝试以下代码:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim i As Long, lngCI As Long
For i = 1 To ThisWorkbook.Sheets.Count
lngCI = Sheets(i).Cells(1, 1).Interior.ColorIndex
Sheets(i).UsedRange.Interior.ColorIndex = -4142
Sheets(i).Cells(1, 1).Interior.ColorIndex = lngCI
Next
ActiveSheet.Cells.Interior.ColorIndex = red 'Don't think this works, you could try .Color = RGBRed
Selection.Cells.Interior.ColorIndex = 6
End Sub
答案 1 :(得分:0)
要回答您的问题:将-4142更改为2
但是,此代码引发了很多问题。为什么在工作表中而不是在单独的模块中?涉及“红色”的行未执行-您打算做什么?为避免此类问题,请始终在模块顶部包含以下代码,并使您知道该错误。
Option Explicit
其他建议:
(2) use breaks to pause your code and then step through it as it executes
(3) use ranges to define the cells you are manipulating (e.g., dim r as range)
(4) use `r.select` as you debug your code to see which cell is being manipulated
关于(4),请注意在执行.select
时激活了哪个工作表-调试完成后,请记住删除所有.select
语句。