vba 我可以隐藏/显示选定单元格周围的黑色边框吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11641536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:02:18  来源:igfitidea点击:

Can I hide/show the black border around a selected cell?

excelvbaexcel-vba

提问by user1543250

If we protect sheets using the ribbon UI, and if every cell is locked or no selection is enabled, the rectangle cursor disappears.

如果我们使用功能区 UI 保护工作表,并且如果每个单元格都被锁定或未启用选择,则矩形光标将消失。

But since I have some cells interact with the user, I am not going to lock every cell. Now the rectangle only appear on the unlocked cells.

但是由于我有一些单元格与用户交互,所以我不会锁定每个单元格。现在矩形只出现在未锁定的单元格上。

And since I have to make those locked cells selectable by VBA, the following code was added:

由于我必须让 VBA 可选择这些锁定的单元格,因此添加了以下代码:

Worksheets("sheet1").Protect Password:="******", _
    UserInterfaceOnly:=True

And now since VBA can select every cell, no matter locked or unlocked, that rectangle cursor appears everywhere.

现在由于 VBA 可以选择每个单元格,无论是锁定还是解锁,矩形光标都会出现在任何地方。

Now I want that rectangle itself to be hidden; is it possible?

现在我想隐藏这个矩形本身;是否可以?

回答by Doug Glancy

Add this bit of code:

添加这段代码:

Worksheets("sheet1").EnableSelection = xlNoSelection

回答by SeanC

I can't make it vanish completely, but I can hide it well. I note from a comment that you highlight the current cell, so this code added to that highlighting should help hide the cursor:

我不能让它完全消失,但我可以很好地隐藏它。我从注释中注意到您突出显示了当前单元格,因此添加到该突出显示中的这段代码应该有助于隐藏光标:

' Code generated by record macro, and not tidied up
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThick
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

This will turn the border black, and it will be white (with a very thin black outline) when the cell is selected.

这将使边框变黑,当单元格被选中时它会变成白色(带有非常细的黑色轮廓)。

The alternative would be to change all the cells to have a grey thick outline, so it will have the same color outline when the box is selected. Programatically, the change to the code would be to remove the colorindex line, and add

另一种方法是将所有单元格更改为具有灰色粗轮廓,因此在选择该框时它将具有相同颜色的轮廓。以编程方式,对代码的更改将是删除 colorindex 行,并添加

    .ThemeColor = 1

in its place, and to change the TintAndShade line to this:

在它的位置,并将 TintAndShade 行更改为:

    .TintAndShade = -0.5

regardless of the method used, you will still end up with a thin black line in the current cell.

无论使用何种方法,您仍然会在当前单元格中看到一条细黑线。

回答by MisterBic

There is no way to actually hide the rectangle.

没有办法真正隐藏矩形。

回答by Mahdi Jazini

1) Hide A Column

1) 隐藏一列

2) Move to A1

2)移动到A1

回答by Nathalii.

I don't know who wrote this so i could give credit, but it seems to work.

我不知道这是谁写的,所以我可以给予信任,但它似乎有效。

Private Declare Function ShowCursor Lib "USER32" _ 
(ByVal fShow As Integer) As Integer 

Sub hide() 
    While ShowCursor(False) >= 0 
    Wend 
End Sub 

Sub show() 
    While ShowCursor(True) < 0 
    Wend 
End Sub