Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 获取两个VBA文本框以一起滚动_Excel_Vba_Textbox_Scrollbar_Userform - Fatal编程技术网

Excel 获取两个VBA文本框以一起滚动

Excel 获取两个VBA文本框以一起滚动,excel,vba,textbox,scrollbar,userform,Excel,Vba,Textbox,Scrollbar,Userform,我有一个宏,它可以生成两个包含相关数据的多行文本框(有时长达数百行)。框中的文本行数始终相同,每一行对应于另一个文本框中的相邻行。我考虑使用两列列表框,但决定使用文本框,以便用户可以根据需要复制/突出显示/选择数据 我希望这样,如果用户向下滚动,两个文本框一起滚动(即,行保持同步) 经过大量的挖掘和实验,我终于找到了答案!通过添加滚动条,我可以使用ScrollBar\u Change()事件来调整文本框。在我的表单上,我现在有两个文本框和一个滚动条对象。然后,我的Userform代码中有一些必要

我有一个宏,它可以生成两个包含相关数据的多行文本框(有时长达数百行)。框中的文本行数始终相同,每一行对应于另一个文本框中的相邻行。我考虑使用两列列表框,但决定使用文本框,以便用户可以根据需要复制/突出显示/选择数据


我希望这样,如果用户向下滚动,两个文本框一起滚动(即,行保持同步)

经过大量的挖掘和实验,我终于找到了答案!通过添加滚动条,我可以使用
ScrollBar\u Change()
事件来调整文本框。在我的表单上,我现在有两个文本框和一个滚动条对象。然后,我的Userform代码中有一些必要的sub:

'This constant affects whether the ScrollBar appears or _
   not, as well as some of the movement graphics of the _
   ScrollBar.
'This MUST be reset if the TextBoxes are resized
'I made it a UserForm-level Const because I use it _
   elsewhere, but it could also live in SetUpScrollBar
Private Const TEXTBOX_MAX_LINES_IN_VIEW as Long = 21

Private Sub SetUpScrollBar()
'I call this whenever I show my Userform (happens after a _
   separate macro determines what to put in the TextBoxes). _
   It determines whether the ScrollBar should be shown, and _
   if so, sets the .Max property so it scrolls in accordance _
   to the number of lines in the TextBoxes.

    Dim linesInTextBox as Long

    With Me.TextBox1
        .SetFocus
        linesInTextBox = .LineCount - 1 
        'Need to subtract 1 or you'll get an error _
           when dragging the scroll bar all the way down.
    End With

    'If there are fewer lines than the max viewing area, hide the scroll bar.
    Select Case linesInTextBox > TEXTBOX_MAX_LINES_IN_VIEW
    Case is = True
        ShowScrollBar True
        With Me.ScrollBox
            .Min = 0 'I believe this is the default, but I set it just in case
            .Max = maxLinesInTextBox
            .Value = 0
        End With
    Case is = False
        ShowScrollBar False
    End Select
End Sub

Private Sub ShowScrollBar(show As Boolean)
'A simple way of showing or hiding the scrollbar
    With Me.ScrollBar1
        .Enabled = show
        .Visible = show
    End With
End Sub

Private Sub ScrollBar1_Change()
'When the scrollbar position changes (either by dragging _
   the slider or by clicking it), set the CurLine property _
   of each TextBox to the current ScrollBar value.

    With Me.TextBox1
        'Need to set focus to the box to get or adjust the CurLine property
        .SetFocus
        .CurLine = Me.ScrollBar1.value
    End With

    With Me.TextBox2
        'Need to set focus to the box to get or adjust the CurLine property
        .SetFocus
        .CurLine = Me.ScrollBar1.value
    End With 
End Sub



这对我来说似乎很有效。它允许我在保持数据同步的同时,保持使用文本框的文本选择/复制优势

一些我尚未解决的问题:

  • 滚动可以很好地工作,但是如果您尝试单击箭头(特别是指向刚刚滚动的相反方向),则必须单击箭头,直到光标到达文本框的顶部。对我来说,这是21次点击。有点烦人,但我肯定有解决办法
  • 滚动与普通滚动条不同。这意味着您可以拖动滚动条,但它不会更新文本框,直到您放手
  • 如果用户点击文本框并开始使用箭头键导航,这两个框将不同步。下次用户单击滚动条时,它们将重新同步。如果用户尝试选择的行数超过窗口中可见的行数,则会出现问题:一个文本框将在拖动其选择时滚动,而另一个文本框保持不变

最初在此处发布(然后自动删除)-哦,很有趣。谢谢你的提醒!我重新发布,因为旧帖子中的自动删除消息建议这样做-是的,我没有注意到它是roomba'd(因为-1分和30多天的不活动),你做了正确的事情:)