
Excel (2003 および 2007) では、ウィンドウがフォーカスされていないときに、どのセル、行、または列が選択されているかが表示されないのは非常に不便です。通常、別のアプリケーションで作業しているときに、現在のセルまたは行を参照したいのです。
フォーカスされていないときにセル/行を強調表示する回避策や修正方法はありますか? セルをコピー (Ctrl+C) できることは知っていますが、毎回それを実行するのは少し面倒です。
答え1
回避策はあると思いますが、それは本当に状況によって異なります。
選択が変更されたときに実行され、各セルの背景を変更するだけのマクロを作成できます。セルを「離れる」と、行の背景値が白にリセットされ、新しい行が選択されます。
これを Visual Basic ウィンドウの Sheet1 に追加しました。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlColorIndexNone
ActiveCell.EntireRow.Interior.ColorIndex = 34
End Sub
このスクリーンショットは、アプリケーションがフォーカスを失ったときに撮影されました。
面倒かもしれませんが、この機能のオン/オフを切り替えるボタンを簡単に追加できます。
欠点は、(思いつく限りでは)現在のハイライト表示がすべて削除されることです。したがって、ページにハイライト表示(セルに色を付けている)がある場合は、これを使用しない方がよいでしょう。また、ハイライト表示された行も印刷される可能性があります。
答え2
以下は @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
答え3
必要に応じて、このようなことができます。シート固有のものかもしれませんが
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
これにより、アクティブでないものは白に戻り、アクティブセルは黄色に変わります。ウィンドウがアクティブでないときも表示されます。これが最善の方法かどうかはわかりませんが、機能します。
答え4
図形を使用して選択範囲を強調表示します。
注: これは、別の Excel ウィンドウに切り替える場合にのみ機能します。回避策として、ハイライトを維持するために、別のアプリケーションに切り替える前に空の Excel ウィンドウを開いてこのウィンドウに切り替えることができます。
これを ThisWorkbookcode (シートのコードではなくワークブックのコード) に追加するだけです。これはワークブック内のすべてのシートで機能します。
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
コードを微調整することで、好みに合わせて形状をフォーマットすることもできます。
利点は次のとおりです。
- Excelがクラッシュしたり停電したりしても、元の書式設定は失われません。
- アクティブシートを変更する別のブックからCTRL+[を使用しても、元の書式設定は失われません。
- CTRL+Cソリューションと比較すると、他のExcelウィンドウに変更を加えてもハイライトが失われません。