Get two VBA textboxes to scroll together - excel

I have a macro that produces two multi-line TextBoxes of related data (sometimes hundreds of rows long). The boxes always have the same number of lines of text in them, with each line corresponding to the adjacent line in the other TextBox. I looked into using a two-column ListBox, but decided to use TextBoxes so that the data can be copied out/highlighted/selected as desired by the user.
I want to make it so that if a user scrolls down, both TextBoxes scroll together (i.e., the lines stay synced up).

After much digging and experimenting, I figured it out! By adding a ScrollBar, I was able to use the ScrollBar_Change() event to adjust the text boxes. On my form, I now have two TextBoxes and a ScrollBar object. Then I have a few necessary subs in my Userform code:
'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
This seems to work quite well for my purposes. It allows me to keep the text-selecting/copying benefits of using TextBoxes while keeping my data synced together.
Some issues I've yet to solve:
Scrolling works fine, but if you try to click the arrows (particularly to go in the opposite direction that you just scrolled), you have to click until your cursor gets to the top of the TextBoxes. For me, this is 21 clicks. A bit annoying, but I'm sure there's a workaround.
Scrolling is not live like with a normal scrollbar. This means you can drag the scrollbar, but it won't update the TextBoxes until you let go.
If a user clicks into a TextBox and starts to navigate with their arrow keys, the two boxes will become out of sync. They'll resync the next time the user clicks the ScrollBar. This is very problematic if the user tries to select more lines than are visible in the window: one TextBox will scroll as they drag their selection but the other TextBox stays in place

Related

ActiveX Textbox flickers white whenever changing position

I have an ActiveX Textbox on an Excel worksheet. I need to move this textbox around and change its visibility using VBA frequently as you interact with the program. The issue is whenever I change the textbox visibility or position, it briefly flashes white before reacting to the code. Here is a video of what I'm talking about. In this video, the code is designed to move the textbox (which has a grey background), to position itself directly over the active cell whenever the selection changes. You can see when it moves over a cell with a yellow background. Then when I change the selection to leave the yellow cell, you can see the textbox flicker white before moving to the new location and becoming grey again.
https://vimeo.com/709930517
Also heres a screenshot of the instant I click another cell after the textbox was placed over the yellow cell.
Before this image, the single textbox on the Worksheet was grey and placed over the yellow cell. You can see in the image after clicking above the yellow cell, the textbox has flickered white over the yellow cell. This is the white flicker. Also in this image, the textbox appears it has already moved to the new location (where I clicked), but its still visible in the old location as well! There's only 1 textbox on the worksheet!
This is super annoying because I have a lot of background colors and when the textbox flickers white it looks horrible.
This is my code:
WORKSHEET CODE
Private Sub RulesTextbox_KeyDown(ByVal keyCode As MSForms.ReturnInteger, ByVal shift As Integer)
Call MODTextbox_KeyDown(keyCode, shift, TextboxSheets.rules)
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call MODWorksheet_SelectionChange(Target)
End Sub
MODULE CODE
Option Base 0
Option Explicit
Global activeCell As Range
Public Sub MODTextbox_KeyDown(ByVal keyCode As MSForms.ReturnInteger, ByVal shift As Integer)
End Sub
Public Sub MODWorksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Set activeCell = Target
Dim textbox As OLEObject
Set textbox = Worksheets(1).OLEObjects("MyTextbox")
Call MODResizeTextboxToMatchRange(textbox, Target)
With textbox
.Visible = False
.Visible = True
.Object.BackColor = RGB(220, 220, 220) 'light grey
End With
textbox.Object.Value = activeCell.Value2
textbox.Activate
Application.ScreenUpdating = True
End Sub
Public Sub MODResizeTextboxToMatchRange(ByRef textbox As OLEObject, ByRef selectedRange As Range)
If selectedRange Is Nothing Then
Exit Sub
End If
Dim totalWidth As Double
Dim totalHeight As Double
Dim top As Double: top = selectedRange.top
Dim left As Double: left = selectedRange.left
If selectedRange.MergeCells And selectedRange.Cells.Count = 1 Then
totalWidth = selectedRange.MergeArea.Width
totalHeight = selectedRange.MergeArea.Height
Else
totalWidth = selectedRange.Width
totalHeight = selectedRange.Height
End If
With textbox
.top = top + 1
.left = left + 1
.Width = totalWidth - 2
.Height = totalHeight - 2
End With
End Sub
I've tried various work-arounds to try and get this to work:
It doesnt matter if you reposition the textbox or make it invisible, it always fickers white first.
Application.ScreenUpdating = False has no effect. Neither does calling DoEvents immediately after changing the position.
Changing the transparency of the textbox to try and make it dissappear doesnt help either, you get the same white flicker
It's like causing any change to the control makes it flicker. I would love some kind of ScreenUpdating = False equivalent so I could just hide the textbox and set back to true when its repositioned. I don't know much about the internal workings of ActiveX. Is it an issue with my computer and not Excel?
Also using a regular form input will not work, I need ActiveX to be able to style the textbox, particularly increasing the font size for users.
Any help is appreciated thanks
Well I figured it out. I need to set the textbox to .Visible = False BEFORE it get's moved, then make it visible after the move.
And you also need to set the backstyle of the textbox to transparent, in the VBA properties before you run the program. When the program is running and the textbox is focused it WONT be transparent, but as soon as it loses focuses it becomes transparent which will eliminate the flicker.
And finally, the TEXT of the textbox may still flicker even if the background doesnt. You can overcome this by setting the font size to 0 before you change the position, then when the textbox arrives at the new location set the font size back to 12 via VBA. You will still see a very tiny version of the text in the flicker, so to take it a step further in addition to setting the font size to 0 you can change the color of the font in the textbox via VBA to match the background color of the cell behind it, again switching back to regular font color once the textbox has been moved.
Wow I had no idea it was so simple!

Changing the colour of tabs of Multipage in VBA

I'm using the Forms in VBA and I was wondering if it is possible to make the tabs in my Multipages change colour when active, so that there is a better contrast with the other tabs. Here is a screenshot
Is there a way to contrast the active tab from the inactive tabs so that the user can know which tab he is using?
Or do you have any idea so that the active tab can appear better against the inactive tabs?
Approach by marking page captions by a checkmark
"Or do you have any idea so that the active tab can appear better against the inactive tabs?" -
A possible & helpful approach would be to
mark each clicked page caption by a checkmark (e.g. ChrW(&H2611)) and to
automatically de-mark a prior page caption: the code "remembers" the current/old page index via the multipage's .Tag property set at any multipage ..._Change() event (.Tag gets initialized via UserForm_Initialize() firstly).
As Captions may include normal blanks, I chose to add a protected blank (ChrW(&HA0)) to the checkmark character to allow a simple Replace() maintaining the blanks in the original page caption.
Example code in Userform
Private Sub MultiPage1_Change()
'Purpose: mark current page caption by a checkmark
With Me.MultiPage1
Dim pg As MSForms.Page
'a) de-mark old caption
Set pg = oldPage(Me.MultiPage1)
pg.Caption = Replace(pg.Caption, ChkMark, vbNullString)
'b) mark new caption & remember latest multipage value
Set pg = .Pages(.Value)
pg.Caption = ChkMark & pg.Caption
.Tag = .Value ' << remember latest page index
End With
End Sub
Help functions & UserForm_Initialize() routine
Function oldPage(mp As MSForms.MultiPage) As MSForms.Page
'Purpose: return currently marked page in given multipage
With mp
Set oldPage = .Pages(Val(.Tag))
End With
End Function
Function ChkMark() As String
'Purpose: return ballot box with check + blank space
ChkMark = ChrW(&H2611) & ChrW(&HA0) ' ballot box with check + blank
End Function
Private Sub UserForm_Initialize()
'Purpose: mark start page & remember page index
Const startIndx As Long = 0
With Me.MultiPage1
.Pages(startIndx).Caption = ChkMark & .Pages(startIndx).Caption
.Tag = startIndx
End With
End Sub
One method I've used is to rename the tab when activated, and include more space before and after the page name, so that the tab widens when selected for visibility, or surround with <> etc. chars: <<< TAB_NAME >>>.
But lately I've considered using separate buttons to activate a page, and change the color. This saves some screenspace as the multipage itself is shorter, buttons can still look like multipage buttons being on the top left of the multipage, but then all the space to the right is free for other controls.
Actually I use labels setup as command buttonds, generally smaller/less tall.

Show textbox scrolled to the top without highlighted text on UserForm.Show

I know how to show a textbox scrolled to the top on a UserForm.Show event, however that involves the method SetFocus and if I set the focus, it automatically highlights the text which I do not want. I'm using a userform to show a message because my messages are too long for a MsgBox. Is there a way to prevent it from auto-selecting the text while still having the box scrolled to the top upon .Show? This scrolls to the top but still selects the text despite 0 values:
Messages.MessageBox.SetFocus
Messages.MessageBox.SelStart = 0
Messages.MessageBox.SelLength = 0
Messages.MessageBox.TabStop = False is so close to working, but the fact that you can scroll isn't obvious (scroll bars only appear when you click in the textbox) and when you click in the textbox it auto scrolls to the bottom again.
If there is no way around this, is there a better solution that a UserForm for what I am trying to do (show long messages)? I cannot use a label instead of a textbox either, the messages exceed the character limit for the .Caption property.
I fixed it but I'm not going to accept this as the true correct answer because I don't think it would solve everyone's problem. I needed to allow my users to still edit the spreadsheet while the userform was open, so when I set Messages.Show vbModeless it fixed everything. None of the text was selected, the scroll bars were visible, and it was scrolled to the top. I have a feeling it's because the focus technically isn't specifically on the userform anymore, so it didn't highlight the text. If you need to make sure nobody messes with the spreadsheet while the userform is open though, this will not solve the problem for you.
Full code for example:
Sub ValidateData()
'Do all your stuff
Call Messages_Initialize(msgDict)
Messages.Show vbModeless
End Sub
Sub Messages_Initialize(msgs As Dictionary)
Dim msgString As String
If Not msgs.Exists("success") Then
msgString = "The cells highlighted in red contain invalid values. Please fix before attempting to validate them again."
msgItems = msgs.Items
For i = 0 To msgs.Count - 1
msgString = msgString + vbNewLine + vbNewLine + msgItems(i)
Next i
Else
msgString = msgs("success")
End If
Messages.MessageBox.Text = msgString
Messages.MessageBox.MultiLine = True
Messages.MessageBox.Locked = True
Messages.MessageBox.WordWrap = True
Messages.MessageBox.SetFocus
Messages.MessageBox.CurLine = 0
End Sub

Excel VBA textbox to populate combobox

I'm after some help that has had me stumped for a while. Excuse the long explanation.
I have a combobox that populates from a range when the userform initializes. When I type into the combobox the preemptive text appears as it is supposed to. I then have a Change event for a textbox which populates based on what gets typed into the combobox. That part all works fine (I got that code from another site).
I have two ways to enter the data into the combobox, one is by typing, and the other is when the text in another text box changes, it also populates the combobox. I do this by "combobox1 = textbox1.value". Now here is the part when I am stumped. When using the combobox1 = textbox1 method, it doesn't work properly (or more so, how I want it to work). It enters the text that is contained in textbox1, but it doesn't show the full line of preemptive text like how it does when typing in the combobox, nor does it then populate the textbox that changes when the combobox changes. If I then click in the combo box and hit the space bar the change event fires and the rest of the preemptive text from the range appears. I tried putting a space " " command at the end combobox1 = textbox1 & " " in the hope it would think there is more text to come but that didn't work. Is there any way to get VBA do do this, or am I asking too much of it?
Hopefully this makes sense.
cheers
paul
seems like ComboBox AutoCompletion feature is triggered by UI input only
you can work around it as follows:
Private Sub TextBox1_Change()
Dim iList As Long
With Me.ComboBox1
For iList = 0 To .ListCount - 1
If Left(.List(iList), Len(Me.TextBox1.Value)) = Me.TextBox1.Value Then
.ListIndex = iList ' if any combobox1 value matches textbox1 value then select it
Exit Sub
End If
Next
.ListIndex = -1 ' if no combobox1 value matches textbox1 value then "deselect" combobox1
End With
End Sub

Bugs in Excel's ActiveX combo boxes?

I have noticed that I get all sorts of annoying errors when:
I have ActiveX comboboxes on a worksheet (not an excel form)
The comboboxes have event code linked to them (eg, onchange events)
I use their listfillrange or linkedcell properties (clearing these properties seems to alleviate a lot of problems)
(Not sure if this is connected) but there is data validation on the targeted linkedcell.
I program a fairly complex excel application that does a ton of event handling and uses a lot of controls. Over the months, I have been trying to deal with a variety of bugs dealing with those combo boxes. I can't recall all the details of each instance now, but these bugs tend to involve pointing the listfillrange and linkedcell properties at named ranges, and often have to do with the combo box events triggering at inappropriate times (such as when application.enableevents = false). These problems seemed to grow bigger in Excel 2007, so that I had to give up on these combo boxes entirely (I now use combo boxes contained in user forms, rather than directly on the sheets).
Has anyone else seen similar problems? If so, was there a graceful solution? I have looked around with Google and so far haven't spotted anyone with similar issues.
Some of the symptoms I end up seeing are:
Excel crashing when I start up (involves combobox_onchange, listfillrange->named range on another different sheet, and workbook_open interactions). (note, I also had some data validation on the the linked cells in case a user edited them directly.)
Excel rendering bugs (usually when the combo box changes, some cells from another sheet get randomly drawn over the top of the current sheet) Sometimes it involves the screen flashing entirely to another sheet for a moment.
Excel losing its mind (or rather, the call stack) (related to the first bullet point). Sometimes when a function modifies a property of the comboboxes, the combobox onchange event fires, but it never returns control to the function that caused the change in the first place. The combobox_onchange events are triggered even when application.enableevents = false.
Events firing when they shouldn't (I posted another question on stack overflow related to this).
At this point, I am fairly convinced that ActiveX comboboxes are evil incarnate and not worth the trouble. I have switched to including these comboboxes inside a userform module instead. I would rather inconvenience users with popup forms than random visual artifacts and crashing (with data loss).
I don't have a definitive answer for you, but I can tell you that I stopped using ListFillRange and LinkedCell for ActiveX controls about 10 years ago. I don't recall what particular problems I encountered. I just remember coming to the conclusion that whatever little time they saved me isn't worth the brain ache of trying to track down the bugs. So now I populate the controls through code and deal with output in the events.
My active-x combo box works fine when my Dell is docked but resizes to a larger font each time it is clicked when the Dell is undocked - very strange. I added resizing code which works when undocked, but both .height and .scaleheight fail when docked and when triggered programmatically (even stranger).
Sheet2.Shapes("cb_SelectSKU").Select
Selection.ShapeRange.Height = 40
Selection.ShapeRange.ScaleHeight 0.8, msoFalse, msoScaleFromTopLeft
I then added my own enableevents-like switch so that the resizing only occurs when a user selects a combobox value, not when anything is affected while a macro is running.
Select Case strHoldEvents
Case Is = "N" 'Combobox resizing fails with error when triggered programatically (from SaveData)
Call ShowLoadShts
Sheet2.Shapes("cb_SelectSKU").Select
Selection.ShapeRange.Height = 40
Selection.ShapeRange.ScaleHeight 0.8, msoFalse, msoScaleFromTopLeft
Case Else
End Select
Finally that seems to work, whether docked or undocked, whether triggered by the user or during a procedure. We'll see if it holds...
I have a partial reply for the Dell users, and for your formatting problem
The formatting and display problem is another known-but-undocumented issue in Excel.
Many flat-panel monitors (including laptop displays) are unable to render fonts correctly in textbox controls on an Excel spreadsheet: you've got a mild version of this problem.
Our company has recently upgraded to new (and much larger!) monitors, and I can at last use textboxes, labels and combo boxes in worksheets. Our old Samsung screens displayed text controls correctly, but any manual or VBA-driven updates resulted in an illegible jumble of overlapping characters.
Listboxes don't have the problem: it's the 'textbox' part of your combo box that has the issue. Try manipulating a listbox in VBA event procedures: it's a kludge but it works.
In-Cell dropdowns from Data Validation lists don't have the problem. If you set up a validation list for a cell, then set the data validation error messages to empty strings, you can enter free-form text in the cell; the drop-down list is advisory, not a mandatory limit-to-list.
The problem is sometimes ameliorated (but never completely fixed) by using the Terminal or System fonts in your Active-X control.
The problem is sometimes ameliorated (but never completely fixed) by using a VBA event to nudge or resize your Active-X control by 0.75 mm.
Check if your laptop manufacturer has released an upgrade to the display drivers.
...And that's everything I know about the font rendering problem. If Mike (with his Dell laptop) is reading this: Good luck with those workarounds - to the best of my knowledge, there's no real 'fix'.
The stability problem was a major headache for me until Excel 2003 came out: using any Active-X control in the sheet was a source of instability. The Jury's still out on Listbox controls embedded in a sheet, even in Excel 2003: I still avoid using them.
So I was facing the same issues. I had a file with drop down lists on which I had superimposed the combobox to fight the issue of illegibility when zooming out too much. This was what my code looked like INITIALLY:
'=========================================
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler
If Target.Count > 1 Then GoTo exitHandler
Set cboTemp = ws.OLEObjects("ComboBox1")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = "Treatment"
.LinkedCell = Target.Address
.Visible = False
.Value = ""
End With
End If
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.ComboBox1.DropDown
End If
exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
errHandler:
Resume exitHandler
End Sub
'====================================
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems, change to KeyUp
'Table with numbers for other keys such as Right Arrow (39)
'https://msdn.microsoft.com/en-us/library/aa243025%28v=vs.60%29.aspx
Private Sub ComboBox1_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
Select Case KeyCode
Case 9 'Tab
ActiveCell.Offset(0, 1).Activate
Case 13 'Enter
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
'====================================
I was facing all sorts of issues but as primarily mentioned on this thread, the LinkedCell issue was the biggest. My selection from the drop down menu would go wherever on the sheet I had clicked last, instead of the cell I had chosen the drop down box from, and in process, also disturbing the code of wherever the selection would go. I used a simple ONE LINE code to make sure my program in ActiveX runs only when its a drop down menu. I used this before the LinkedCell command ran:
If Target.Validation.Type = 3 Then
'... all the normal code here...
End If
So my code now looks like this:
'... Code as before
If Target.Validation.Type = 3 Then
' NEW CODE LINE ABOVE
If Target.Count > 1 Then GoTo exitHandler
Set cboTemp = ws.OLEObjects("ComboBox1")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = "Treatment"
.LinkedCell = Target.Address
.Visible = False
.Value = ""
End With
End If
End If
' End of the new If
Unbelievably, this worked. And now my excel sheet isn't misbehaving anymore. Hope this helps.
For this reason, I use cells with data validation lists when putting combo boxes on a spreadsheet.

Resources