gpt4 book ai didi

vba - 以黄色突出显示当前选择

转载 作者:行者123 更新时间:2023-12-04 21:23:50 25 4
gpt4 key购买 nike

我只是希望所选范围为黄色,并在取消选择时返回无色。代码如下:

Option Explicit
Public previouscell As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Interior.Color = vbYellow
previouscell.Interior.ColorIndex = xlNone
Set previouscell = Selection
End Sub

问题是首先设置previouscell。我尝试将其放入 Worksheet_Activate() 中,但一打开工作簿就无法正常工作(仅当我更改工作表时,之后它才能正常工作。)

所以我也尝试在 ThisWorkbook.Workbook_open 中将其声明为公众:
Option Explicit
Public previouscell As Range
Private Sub Workbook_Open()
Set previouscell = ActiveCell
ActiveCell.Interior.Color = vbYellow
End Sub

但是它不识别变量 previouscell,因为我相信它不是从 ThisWorkbook 转移的(我在测试之前确实关闭并重新打开了工作簿)。有谁知道我需要为这个简单的任务改变什么?

最佳答案

( 第 4 次编辑 )

使用此工作簿代码,并删除您的工作表代码:

Option Explicit
Public previousCells As Range

Private Sub Workbook_Open()
SetSelectionYellow
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
SetSelectionYellow
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
SetSelectionYellow
End Sub

Private Sub SetSelectionYellow()
If Not previousCells Is Nothing Then previousCells.Interior.ColorIndex = xlNone
Set previousCells = Selection
previousCells.Interior.Color = vbYellow
End Sub

关于vba - 以黄色突出显示当前选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31708233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com