Too much data in a cell to display in a Userform Listbox

核能气质少年 提交于 2020-01-25 20:48:46

问题


I have a listbox in a userform in Excel 2007.

Some cells in my worksheet contain more than 10 rows (data with ALT ENTER).

I'm trying to clean and display the data. I don't want to change the column width to 1000, but I'd like to use a mouseover box to show all the cell data.

Is there another idea that would work?


回答1:


With mouse over would be possible to do it but it is complicated I think. Here I have another simpler idea: on double click in list box a multi line text box with selected list item(s) data will be shown. This text box has the same position and size as the list box. On the user form click text box hides. Here is some sample code, to test it you need form with list box named "ListBox1":

Option Explicit

Public ListItemInfo As Control

Private Sub UserForm_Initialize()
    Set ListItemInfo = Me.Controls.Add("Forms.TextBox.1", "ListItemInfo", False)
    With Me.ListItemInfo
        .Top = Me.ListBox1.Top
        .Left = Me.ListBox1.Left
        .Width = Me.ListBox1.Width
        .Height = Me.ListBox1.Height
        .MultiLine = True
    End With
End Sub

Private Sub ListBox1_Change()
    Me.ListItemInfo.text = GetSelectedItemsText
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    SwitchListItemInfo
End Sub

Private Sub UserForm_Click()
    SwitchListItemInfo
End Sub

Private Function GetSelectedItemsText() As String
    Dim text As String
    Dim i As Integer
    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) Then
            text = text & Me.ListBox1.List(i) & vbNewLine
        End If
    Next i
    GetSelectedItemsText = text
End Function

Private Sub SwitchListItemInfo()
    If Me.ListItemInfo.text = "" Then Exit Sub
    Me.ListItemInfo.Visible = Not Me.ListItemInfo.Visible
    Me.ListBox1.Visible = Not Me.ListBox1.Visible
End Sub



来源:https://stackoverflow.com/questions/15297508/too-much-data-in-a-cell-to-display-in-a-userform-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!