我想首先说明我几乎没有编码经验。我在网上发现了一个VBA片段,用于突出显示整个选定范围(仅作为视觉指南):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target
' Highlight the entire column that contain the active cell
.EntireRow.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True
End Sub
我想让光标跳转到“J”列。例如,在按下“确定”后搜索包含“草莓浇头”字样的单元格后,包含该文本的单元格将变为活动状态,并且由于VBA代码,整个行将突出显示。
我需要处理的第一个单元格是“J”列。我是否也可以选择列J以及突出显示的行?
非常感谢您的时间,并感谢您提供的任何帮助。
答案 0 :(得分:2)
我的三分钱
Target.Cells.Count
。使用Target.Cells.CountLarge
否则如果用户尝试通过按 CTRL + A 选择所有单元格,则会出现溢出错误,因为Target.Cells.Count
无法保持Long
值Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Rw As Long, Col As Long
Dim ColName As String
On Error GoTo Whoa
If Target.Cells.CountLarge > 1 Then Exit Sub
Application.ScreenUpdating = False
Application.EnableEvents = False
' Clear the color of all the cells
'Cells.Interior.ColorIndex = 0
With Target
Rw = .Row
Col = .Column
ColName = Split(Cells(, Col).Address, "$")(1)
' Highlight the entire column that contain the active cell
'.EntireRow.Interior.ColorIndex = 8
Range(ColName & ":" & ColName & "," & Rw & ":" & Rw).Select
End With
LetsContinue:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
。 这是你在尝试的吗?
{{1}}