我的 VBA 技能非常原始,所以如果有人能提供帮助,你能告诉我在哪里插入 VBA 代码以及如何让它“工作”!
我通常不回答缺乏努力的问题,但这个问题远远超出了正常问题,所以我会尝试回答它。
hovering 可以在单元格上显示内容。当我说hovering 时,我的意思是hovering 而不是Selecting 一个单元格。
示例文件的链接发布在本文末尾。
1. 在您的文件中,转到 VBA 编辑器并插入一个用户表单。接下来放置一个标签控件并调整其大小以填充用户表单,如下图所示
2.将此代码粘贴到用户表单中
代码
Option Explicit
Const GWL_STYLE = -16
Const WS_CAPTION = &HC00000
Private Declare Function GetWindowLong _
Lib "user32" Alias "GetWindowLongA" ( _
ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar _
Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindowA _
Lib "user32" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Sub HideTitleBar(frm As Object)
Dim lngWindow As Long
Dim lFrmHdl As Long
lFrmHdl = FindWindowA(vbNullString, frm.Caption)
lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
lngWindow = lngWindow And (Not WS_CAPTION)
Call SetWindowLong(lFrmHdl, GWL_STYLE, lngWindow)
Call DrawMenuBar(lFrmHdl)
End Sub
'~~> Hide Title bar and border using API
Private Sub UserForm_Initialize()
HideTitleBar UserForm1
End Sub
'~~> Stop the execution of the code
Private Sub Label1_Click()
StopLoop = True
Unload Me
End Sub
这样做的目的是删除表单的标题栏和边框。
3.接下来插入一个模块并将这段代码粘贴到那里
Public Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End Type
Public StopLoop As Boolean
Sub StartShowingCellContents()
Dim lngCurPos As POINTAPI
Dim rng As Range
StopLoop = False
Do
'~~> Get the cursor position
GetCursorPos lngCurPos
'~~> This will give the cell address "under" the cursor
Set rng = ActiveWindow.RangeFromPoint(lngCurPos.x, lngCurPos.y)
If Not rng Is Nothing Then
If Not rng.Cells.CountLarge > 1 Then
With UserForm1
'~~> Display cell value in the label
.Label1.Caption = rng.Value
'~~> Show the form modeless
.Show vbModeless
DoEvents
End With
End If
End If
DoEvents
'~~> Stop the loop (invoked by clicking on the userform's label
If StopLoop = True Then Exit Sub
Loop
End Sub
4. 然后你就完成了。首先,运行过程Sub StartShowingCellContents()。要停止,只需单击用户表单
5. 在行动。我用手机点击了图片,这样您就可以看到该单元格顶部的光标。
重要提示:
- 在代码运行之前,您将无法执行任何操作,例如复制、粘贴、删除等。停止代码,做你想做的,然后再次运行代码。
- 随意根据自己的喜好自定义代码。
- 示例文件可以从HERE下载