Answers:
我认为可以解决此问题,但这确实取决于您的情况!
您可以创建一个宏,当选择更改时将触发该宏,并且该宏仅更改每个单元格的背景。当您“离开”该单元格时,它将把该行的背景值重置为白色,然后选择新行。
我将此添加到Visual Basic窗口中的Sheet1中。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlColorIndexNone
ActiveCell.EntireRow.Interior.ColorIndex = 34
End Sub
该屏幕快照是在应用程序失去焦点时拍摄的。
这可能很烦人,但是您可以轻松地添加一个按钮来打开或关闭此功能!
底片是(从我的头顶开始:它将删除您当前拥有的所有突出显示。因此,如果页面上具有突出显示(彩色单元格),则最好不要使用它!而且,它可能会与突出显示的行一起打印!
如果需要,您可以执行以下操作。尽管它可能是特定于工作表的
Dim wasActive As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If wasActive = Empty Then wasActive = "A1"
Range(wasActive).Interior.ColorIndex = "0"
ActiveCell.Interior.ColorIndex = "6"
wasActive = ActiveCell.Address
End Sub
这会将不活动的内容更改回白色,并将活动单元更改为黄色。并且在窗口处于非活动状态时仍会显示。不确定这是最好的方法,但它是否有效
这是@datatoo中代码的修改。它读取以前的值,以防止丢失当前的填充颜色。它还会更改文本颜色以使其更加突出。我将其添加到代码编辑器(Excel中的Alt-F11)的Excel工作表中。
单击此处以获取有关进行工作表更改事件的信息。
'VBA code for Excel to show active cell in worksheet when worksheet is out of focus
Dim wasActive As String
Dim originalFillColor As String
Dim originalTextColor As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Set up colors on load
If wasActive = Empty Then
wasActive = "A1"
originalFillColor = Range(wasActive).Interior.Color
originalTextColor = Range(wasActive).Font.Color
End If
'Reset previous cell to original color values; If statement prevents removal of grid lines by using "0" for clear fill color when white
If originalFillColor = 16777215 Then
Range(wasActive).Interior.ColorIndex = "0"
Range(wasActive).Font.Color = originalTextColor
Else
Range(wasActive).Interior.Color = originalFillColor
Range(wasActive).Font.Color = originalTextColor
End If
'Set new colors and change active cell to highlighted colors (black fill with white text)
originalFillColor = ActiveCell.Interior.Color
originalTextColor = ActiveCell.Font.Color
wasActive = ActiveCell.Address
ActiveCell.Interior.ColorIndex = "1"
ActiveCell.Font.ColorIndex = "2"
End Sub
使用形状突出显示选择。
注意:仅当切换到另一个Excel窗口时,它才有效。解决方法是,您可以打开一个空的Excel窗口并切换到该窗口,然后再切换到另一个应用程序以保持突出显示。
只需将其添加到您的ThisWorkbookcode(您的workBOOK,而不是工作表的代码)中即可。这将适用于工作簿中的每个工作表。
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
DeleteSelectionHighlight
End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
DeleteSelectionHighlight
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
On Error Resume Next
Dim shp As Shape
Application.ScreenUpdating = False
Set shp = ActiveSheet.Shapes("SelectionHighlight")
If Err.Number <> 0 Then
Set shp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 1, 1, 1, 1)
With shp 'Format shape to your preference
.Name = "SelectionHighlight"
.Line.ForeColor.RGB = RGB(226, 0, 0) ' Border color
.Line.Weight = 1.5
.Line.DashStyle = msoLineSolid
.Fill.Visible = msoFalse 'No background
'.Fill.ForeColor.RGB = RGB(0, 153, 0) 'Background color
'.Fill.Transparency = 0.95 'Background transparency
End With
End If
Dim oldZoom As Integer
oldZoom = Wn.Zoom
Wn.Zoom = 100 'Set zoom at 100% to avoid positioning errors
With shp
.Top = Wn.Selection.Top 'Tweak the offset to fit your desired line weight
.Left = Wn.Selection.Left 'Tweak the offset to fit your desired line weight
.Height = Wn.Selection.Height
.Width = Wn.Selection.Width
End With
Wn.Zoom = oldZoom 'Restore previous zoom
Application.ScreenUpdating = True
End Sub
Private Sub DeleteSelectionHighlight()
On Error Resume Next
Dim shp As Shape
Set shp = ActiveSheet.Shapes("SelectionHighlight")
shp.Delete
End Sub
您甚至可以通过调整代码来设置形状的格式。
优点是:
这个问题没有永久解决方案。
一种解决方法(可能会在一段时间后变得烦人)是在选定单元格时将其更改为突出显示,然后再次重新选择它们以降低颜色。
将此代码粘贴在Sheet1代码的后面,然后转到电子表格,选择一些单元格,选择其他单元格,然后重新选择第一个单元格以放下颜色
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cell As Range
For Each cell In Target.Cells
If cell.Interior.Color = RGB(60, 150, 230) Then
cell.Interior.Pattern = xlNone
Else
cell.Interior.Color = RGB(60, 150, 230)
End If
Next
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Interior.ColorIndex = xlColorIndexNone
Selection.Interior.Color = RGB(204, 204, 204)
End Sub
在标准模块中:
Option Explicit
Public s As Range
要在工作表中使用它:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set s = Selection
End Sub
在ThisWorkbook
:
Private Sub Workbook_Deactivate()
If s Is Nothing Then
Set s = Selection
Exit Sub
End If
s.Interior.ColorIndex = xlColorIndexNone
s.Interior.Color = RGB(204, 204, 204)
' This is optional formatting to make the cells look more like they're actually selected
s.Borders.Color = RGB(130, 130, 130)
s.BorderAround _
Color:=RGB(30, 130, 37), Weight:=xlThick
End Sub
Private Sub Workbook_Activate()
If s Is Nothing Then
Set s = Selection
Exit Sub
End If
s.Interior.ColorIndex = xlColorIndexNone
s.Borders.ColorIndex = xlColorIndexNone
End Sub