简体   繁体   中英

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.

Example:

User Selects Cell A4, I would like a message to read in a floating text when that cell is selected. Then if a user selects Cell B6 a different message appears in that box.

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. 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:

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.
  • I strongly advise you to set the background colour of the control to &H80000004&, the predefined Windows scheme colour for menu and form backgrounds; likewise, the foreground to &H80000008&, the menu text colour. 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. 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.

  • Copy-and-paste might be affected by my use of the Worksheet SelectionChange event.

  • 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. Alternately, do this in the label's Click() or MouseMove event:

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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