I have an excel sheet with multiple listboxes. All of them multi-select. As excel always messes up the dimensions of the listboxes i had a piece of code to repair them when opening the workbook:
Private Sub Workbook_Open()
Dim Ctrl As OLEObject
For Each Ctrl In Sheets("SomeSheet").OLEObjects
If Ctrl.progID = "Forms.ListBox.1" Then
With Ctrl
.Width = 95.4
.Height = 70.2
End With
End If
Next Ctrl
End Sub
However after a while i noticed that the last entry is at best partially visible. A search on the interweb provided me with a solution : link.
Unfortunately that solution does not work for me. I adapted the above code to:
Private Sub Workbook_Open()
Dim Ctrl As OLEObject
Application.ScreenUpdating = False
For Each Ctrl In Sheets("SomeSheet").OLEObjects
If Ctrl.progID = "Forms.ListBox.1" Then
With Ctrl
.Object.IntegralHeight = False
.Width = 95.4
.Height = 70.2
.Object.IntegralHeight = True
.Object.MultiSelect = fmMultiSelectSingle
.Object.MultiSelect = fmMultiSelectExtended
End With
End If
Next Ctrl
End Sub
The result is that the width and height i have set are again screwed up by excel, as soon as the three statements following the .height are executed. With each statement resulting in a further shrinking of the listbox in both dimensions. To make matters worse they also move away from their position.
I'm looking for any clues how to fix this, so to have listboxes of the desired dimension, with all entries visible.
Edit 20140905
As per request a screenshot is added. Even though the scrollbar suggest i can scroll down further, that is not possible. As you can see by the blue color, 'Orange' is there, and is selected, and is in the listbox.
set IntegralHeigth property of ListBox to false.
Related
I have created a userform and included a combobox populated with numbers 1 to x in userform.initialize and then added .setfocus and .dropdown commands. For some strange reason the combobox (with dropdown) appears on my lefthand screen outside the userform. The combobox also appears on the userform, but without the dropdown displayed. The screen combobox is active and if I click a number in the dropdown list the code accepts this input.
I have tried deleting the combobox and inserting another one with a different name, but the behaviour persists. If I remove the .dropdown command, the combobox appears correctly on the userform without the screen copy and I can click the userform box and display and use the dropdown list.
On reading an unrelated post, I tried adding .visible=false, followed by .visible=true before the .dropdown command, but that didn't stop the behaviour.
I have tried exporting, deleting and re-importing the userform, but the behaviour persists.
The code I am using (in userform_initialize sub) is:
With cbxGroup 'combobox
.Clear
For i = 1 To PlayGroups
.AddItem i
Next
.ListIndex = -1
.Visible = False
.Visible = True
.SetFocus
.DropDown
End With
Has anyone come across such behaviour before and can explain what has happened, and maybe how to fix it. I can just recreate the userform, but it seems a lot of unnecessary work. I am using Office 365
I can confirm the behaviour.
I strongly assume that this is related to the fact that you are using the Initialize-Trigger. When this trigger is executed, the form is still in the creation-process and some internal properties (eg the exact screen position) are likely not calculated. As a consequence, when calling DropDown, the Combobox is painted on the screen but not at the right position.
You can keep the code to fill the combobox in the Initialize-Trigger, but you should move the SetFocus and DropDown-commands to the Activate-Trigger
Private Sub UserForm_Initialize()
fillCombo
End Sub
Private Sub UserForm_Activate()
showCombo
End Sub
Sub fillCombo()
With Me.cbxGroup
.Clear
Dim i
For i = 1 To PlayGroups
.AddItem i
Next
.ListIndex = -1
End With
End Sub
Sub showCombo()
With Me.cbxGroup
.SetFocus
.DropDown
End With
End Sub
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!
First time posting, hope this all makes sense. I have created a graphic in excel that shows 88 lines 1. In another worksheet there are rows of data documented what is on each line 2 When a line is clicked on the graphic I want the corresponding data from the other worksheet to pop up 3
I have managed it achieve this by making a transparent button/shape over each line to hide and show a linked image 4. However, I've had to create a separate button and macro for each line with 100+ lines this seems every inefficient. this is the code I used :
Sub LINE1A1()
With ActiveSheet.Shapes("Rectangle 9").TextFrame2.TextRange.Characters
If .Text = "Hide" Then
.Text = "Show"
ActiveSheet.Shapes("Picture 3").Visible = False
Else
.Text = "Hide"
With ActiveSheet.Shapes("Rectangle 9")
ActiveSheet.Shapes("Picture 3").Left = .Left + .Width
ActiveSheet.Shapes("Picture 3").Top = .Top + .Height
ActiveSheet.Shapes("Picture 3").Visible = True
End With
End If
End With
End Sub
What would be a better way to achieve this? side not I used linked image as the data and range of data is subject to changes as each line could have more than 1 row of data.
You can create those buttons or shapes with a macro (as you said for example 100 buttons) and move them with a macro so they are in the right place. They are going to use the same macro. It will just do a different thing depending where the button is or maybe better depending on its name.
There is a nice tutorial.
https://www.youtube.com/watch?v=bF28JhlC7yc&index=18&list=PLpOAvcoMay5SE3XTp2YN2v6NcJuXKM9KX
Here's a simple example where a common Sub uses Application.Caller to figure out which shape triggered it.
Sub Tester()
Dim s As Shape, c, s2 As Shape
c = Application.Caller '<< name of your clicked shape
Debug.Print c
Set s = ActiveSheet.Shapes(c) '<< the clicked shape
With s.TextFrame2.TextRange.Characters
'here you need some way to transform the name of the clicked
' shape to get the name of the other shape to be shown/hidden/moved
Set s2 = ActiveSheet.Shapes(c & "_linked")
s2.Visible = (.Text = "Show")
.Text = IIf(.Text = "Show", "Hide", "Show") 'toggle text
End With
End Sub
On a EXCEL Userform, I have several Textboxes. Their number can vary as they are created dynamically.
I want to achieve the following:
When the user clicks on any of the Textboxes, I want to display a Msgbox, but only on this particular userform and only once for the first click.
Could you give me a pointer for documentation that would help me achieve this?
after googling on this, my code looks like this:
Userform: create a variable number of textboxes
Option Explicit
Dim oKlasseExcel() As Klasse1
Sub userform_initialize()
Dim i As Long
Dim k As Long
k = InputBox("insert number")
i = 0
Do
ReDim oKlasseExcel(0 To i)
Set oKlasseExcel(i) = New Klasse1
Set oKlasseExcel(i).objTextbox = Userform1.Controls.Add("Forms.Textbox.1", "Textbox" & CStr(i))
With oKlasseExcel(i).objTextbox
.Left = 30
.Top = 75 + 25 * i
.Width = 300
.Height = 25
.MultiLine = True
End With
i = i + 1
Loop Until i = k
End Sub
class module:
Option Explicit
Public WithEvents objTextbox As MSForms.TextBox
Sub objTextbox_click()
MsgBox objTextbox.Name & ": Changeereignis ausgelöst!"
End Sub
I think I have to create a class module probably, but I am totally new to this and I think I need a well-written example with some explanation comments, please. My code above does nothing when I click on a textbox.
If you don't want to create any additional data structures to capture clicks, maybe you can try changing some TextBox properties, for example:
create a TextBox with WordWrap = False and after it was clicked change its value to True - then you could distinguish which were clicked or not.
I have a workbook "A" with a macro and set with a shortcut "Ctrl+Q", when I press "Ctrl+Q" the a form pops up, and when I press "ESC" the form is closed since I added a button "CommandButton1" whose property "Cancel" set to TRUE.
In the code I close the form like this:
Private Sub CommandButton1_Click()
Unload Me
End Sub
It works well, but the problem is I can't switch to other opened workbooks until I close the workbook "A", does anyone knows what the problem is?
thanks very much!
the code is like this:
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} Get_Photo
Caption = "abc"
ClientHeight = 3120
ClientLeft = 45
ClientTop = 435
ClientWidth = 4710
OleObjectBlob = "Get_Photo.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "Get_Photo"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub CommandButton1_Click()
Unload Me
End Sub
Sub UserForm_Initialize()
With Get_Photo
.Caption = caption_Name
.Width = 430
.Height = 270
End With
With WebBrowser1
.Width = 540
.Height = 300
.Navigate ("www.google.com/images/logos/images_logo_lg.gif")
End With
End Sub
The above form contains a webbrowser and a cancel button.
I added a button in a worksheet with the macro code Get_Photo.Show
when click the button, the form will show with a picture in it.
but when I closed the form after pressing "ESC", I can't then switch to other xlsm workbooks, but xlsx workbooks are fine..
#SiddharthRout, I added more comments to reproduce the problem – aaron 30 mins ago
Finally, I have been able to reproduce it :) Let me check on it and get back to you. I was not able to switch to other xlsm/xlsx workbook. It kept on showing me the original workbook. The Alt+Tab also refused to work. It's doesn't make any exception if I press the commandbutton. – Siddharth Rout 1 min ago edit
I cannot explain the behavior. Probably it is a bug (However, I haven't seen any documentation on it yet).
This will solve your problem. (TRIED AND TESTED)
Put this 2 extra lines of code in the button code of the worksheet from where you are calling the useeform.
Private Sub CommandButton1_Click()
UserForm1.Show
Application.ShowWindowsInTaskbar = False
Application.ShowWindowsInTaskbar = True
End Sub
HTH
Sid