简体   繁体   English

浮动消息/注释框

[英]Floating Message/Comment Box

So pretty much what I am trying to do is create a floating text box that would be on the right of the spreadsheet. 因此,我几乎要做的就是在电子表格的右侧创建一个浮动文本框。 When a user selects a row/cell it will then place a comment or message with details about that cell in there rather than a small little comment box. 当用户选择行/单元格时,它将在其中放置带有有关该单元格详细信息的注释或消息,而不是一个小的小注释框。

I have tried to use the UserForm Box but its not really what I'm looking for. 我试图使用UserForm Box,但它并不是我真正想要的。

Example: 例:

User Selects Cell A4, I would like a message to read in a floating text when that cell is selected. 用户选择单元格A4,当选择该单元格时,我希望显示一条以浮动文本形式显示的消息。 Then if a user selects Cell B6 a different message appears in that box. 然后,如果用户选择单元格B6,则该框中将显示另一条消息。

Does that makes sense? 这有意义吗?

Update: 更新:

The Following Code Shows a UserForm box when a certain cell is selected: 当选定某个单元格时,以下代码显示一个“用户窗体”框:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Definition As String

If Intersect(Target, Range("C6:D6")) Is Nothing Then Exit Sub
Select Case Target.Row
  Case 22
    Definition = "Text Here"
  Case 23
    Definition = "Text Here Again"
End Select

UserForm1.Label1.Caption = Definition
UserForm1.Show

End Sub

I don't want to use a UserForm box as its not stationary on the Worksheet itself. 我不想使用UserForm框,因为它在工作表本身上不是固定的。 I want it so a Text Box that always appears on the right hand side of the worksheet to display a set message or context when the cell is selected. 我希望它是一个始终显示在工作表右侧的文本框,以在选择单元格时显示设置的消息或上下文。 It will be different then what is stored in the actual cell. 这将与实际单元格中存储的内容不同。

Use a Data Validation message. 使用数据验证消息。 This type of message "pops-up" whenever you click on a cell: 每当您单击一个单元格时,这种消息就会“弹出”:

在此处输入图片说明

You can place a Label or TextBox control directly in the worksheet - make sure it's an ActiveX control, not a Forms control - and position it dynamically using the worksheet's SelectionChange event: 您可以将Label或TextBox控件直接放置在工作表中-确保它是ActiveX控件,而不是Forms控件-并使用工作表的SelectionChange事件将其动态放置:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With Me.lblText

    .Visible = False

    Select Case Target.Row     Case 22         .Caption = "Text Here Again"     Case 23         .Caption = "Text Here Again"     Case Else         .Visible = False         Exit Sub     End Select

    ' Place the label to the right of the target cell     .Left = Target.Left + Target.Width

    ' Or place the label in the far left of the window     '.Left; =; Application.ActiveWindow.VisibleRange.Width; -; .Width

    .Top = Target.Top - 0.75 ' cell borders

    .Visible = True

End With

End Sub

Some hints: 一些提示:

  • Make sure the 'Placement' property of a label is 2 (XlPlacement.xlMove), as other values give you Free-Floating or Move-and-Size. 确保标签的“ Placement”属性为2(XlPlacement.xlMove),因为其他值使您可以自由浮动或移动并调整大小。
  • I strongly advise you to set the background colour of the control to &H80000004&, the predefined Windows scheme colour for menu and form backgrounds; 我强烈建议您将控件的背景色设置为&H80000004&,这是菜单和表单背景的预定义Windows方案颜色; likewise, the foreground to &H80000008&, the menu text colour. 同样,菜单文本颜色&H80000008&的前景。 This ensures visibility for users who have their own colour settings, and explicitly supports users who specify an accessible or assistive colour scheme to ameliorate a visual impairment. 这样可以确保具有自己的颜色设置的用户的可见度,并明确支持指定可访问或辅助的配色方案以改善视觉障碍的用户。

  • My code relies on your sheet supporting event procedures in VBA, and on ActiveX controls. 我的代码取决于您的工作表支持VBA中的事件过程以及ActiveX控件。 It won't work in .xlsx sheets, and it may be blocked (or accompanied by warning dialogues) if your operating environment has a heavy-handed security policy. 它无法在.xlsx工作表中使用,并且如果您的操作环境具有严格的安全策略,则可能会被阻止(或伴随警告对话框)。

  • Copy-and-paste might be affected by my use of the Worksheet SelectionChange event. 复制和粘贴可能会受到我对工作表SelectionChange事件的使用的影响。

  • Right-Click the control in design view for the 'Format Control' menu and uncheck 'Print Control' - if the users print the sheet, they'll want to see the cell contents, not the label. 在设计视图中的“格式控件”菜单上右键单击该控件,然后取消选中“打印控件”,如果用户要打印工作表,则需要查看单元格内容,而不是标签。
  • Also: the label's in the way of selecting and editing the control it's sitting over. 另外:标签是选择和编辑它所位于的控件的方式。 Maybe you want it 4-5mm to the right, so the users can get the mouse into that cell. 也许您希望它在右侧4-5mm,以便用户将鼠标插入该单元格中。 Alternately, do this in the label's Click() or MouseMove event: 或者,在标签的Click()或MouseMove事件中执行此操作:

    Me.lblText.Visible = False

You might draw an obvious conclusion from all this: the native comment or data validation label is a better bet than using forms and ActiveX controls. 您可能会从所有这些中得出一个显而易见的结论:本机注释或数据验证标签比使用表单和ActiveX控件更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM