Labels Flickering On UserFor When I DO A MouseOver - excel

I am having an issue with flickering labels on my userform, when I do a mouse over. Some labels are grouped. I have ungrouped them but I am still having the flickering issue. For each button I have used 3 labels, 1 for background, 1 for Icon and 1 for text as nothing would position the icon and text as I wanted. See image
Apart from the flicker they work fine. I am using labels as buttons as the look better than the vba userform buttons and because, when I tried to you an image as a button, the picture would not display correct, the quality would reduce, but it is fine in a label.
The labels start to flicker when I move the mouse over them, I was looking at these as some possible solution on,Mr Excel Forum & My Original Thread on Mr Excel (there is a code at the botom of this thread), but I am not 100% sure how to change it for my labels. In the second thread that I posted there is a link to the original question which was raised by another member, within this thread they talk of label groups "LabelGroup.Parent.Parent.Controls". I have NO idea what this is and how to do it. Please could someone advise of how I can implement one of these codes with my code, useform or guide me to another solution.
My code is a simple IF statement, that works on a mouse over event
Private Sub MainMenuPageButton_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'MainMenuPageButton border change green on mouse over
MainMenuPageButton.BorderColor = 8435998 'change boarder to green
'Grey Borders for all other labels
SearchButton.BorderColor = -2147483627
WebBrowserButton.BorderColor = -2147483627
SaveFileButton.BorderColor = -2147483627
EmailButton.BorderColor = -2147483627
LoadFileButton.BorderColor = -2147483627
Sheet2Button.BorderColor = -2147483627
Sheet3Button.BorderColor = -2147483627
Sheet12Button.BorderColor = -2147483627
End Sub
Thanks in advance

Related

Mouse over event NOT re-triggering once item has been selected

I have been struggling on this one for a few days now and can not seem to work it out. I am using images as buttons, which have a mouseover effect that changes the border to GREEN when the mouse is hovered over them, apart from them flashing on hover, this works fine apart from when the problem kick in, see below for problem.
I then have an onClick event which changes the border to GREEN if that image/button is selected, this indicates that this is active as the click opens a multi page, this bit also seems to work fine as I had to put in a repaint event in as it would not work without it. UserForm1.Repaint.
The Problem I am having
Once a image/button has been selected and then de-selected the mouse over event NO longer works on the image/button that was previously selected.
Example, If I scroll the mouse over the two buttons below, their borders will change to green on hover, If I selected the WebBrowser button, its border will change to green, indicating that it is active. Once de-selected the mouse over will NO longer work on it. The de-selecting is done by selecting another button.
This is my mouse over code
Private Sub WebBrowserNewButt_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
''Set WebBrowserNewButt Border Colour Green
If WebBrowserNewButt.BorderColor <> 8435998 Then
Me.WebBrowserNewButt.BorderColor = 8435998
''Grey Borders
Me.MainMenuButton.BorderColor = -2147483627
Me.SecondMenuButton.BorderColor = -2147483627
Me.EmailButton.BorderColor = -2147483627
Me.SearchButton.BorderColor = -2147483627
Me.SaveNewButton.BorderColor = -2147483627
End If
End Sub
Onclick code
Private Sub WebBrowserNewButt_click()
Me.WebBrowserNewButt.BorderColor = 8435998 'GREEN BORDER
'GREY BORDERS, for all other images(buttons)
Me.MainMenuButton.BorderColor = -2147483627
Me.SecondMenuButton.BorderColor = -2147483627
Me.EmailButton.BorderColor = -2147483627
Me.SearchButton.BorderColor = -2147483627
Me.SaveNewButton.BorderColor = -2147483627
UserForm1.Repaint
'MULTIPAGES
Me.MultiPage1.Pages(0).Visible = True
MultiPage1.Value = 0
Me.MultiPage1.Pages(1).Visible = False
Me.MultiPage1.Pages(2).Visible = False
Me.MultiPage1.Pages(3).Visible = False
Me.MultiPage1.Pages(4).Visible = False
End Sub
What I have tried so far.
I have tried it with and without the Me. (I left it on Me.)
I have tried to google the answer
I have tried it without repaint, did not work without repaint, repaint
does cause the images to flash.
I have tried to place the change boarder colour in the multi-page, so when active that multi page is
visible = true then the image border colour will change, could not get it to work.
I removed the option of turning the borders to Grey on hover, that did not fix the problem.
The multi-pages will not have their tabs set to visible.
Please could someone advise on what to do.
As always thanks in advance.

VBA Transparent ActiveX object becomes opaque on click [duplicate]

I have a VBA/Excel that user clicks on labels (Active X - Text Label) to perform some actions. The label property is BackStyle Transparent, but when the user click, the label keep opaque, like white or whatever the BackColor property is set.
How can I keep transparent when user click on the label?
Don't use an ActiveX control for this. Any Shape can be assigned to a macro, so instead of having Click event handlers for ActiveX labels like so:
Private Sub Label2_Click()
'do stuff
End Sub
Make the handlers public, give them a meaningful name:
Public Sub BuscaPorPalavraChave()
'do stuff
End Sub
Replace the labels with TextBox shapes - make the shape fill and border transparent, right-click the shape, and select "assign macro" - then pick BuscaPorPalavraChavre. Done!
Rinse & Repeat for every label. I know, painful - but worth it!
That navigation UI looks very nice BTW =)
I came up with a different solution in case you have faced a related problem, like the transparency/opaque problem when executing a macro hovering over a transparent label but that becomes opaque when clicking on it.
The workaround consists basically in changing the visibility status when hovering in and out. It might be counterintuitive at first (Because you are making disappear the same label you are using to execute the macro) but it works really well. Let's assume we have two Active X labels overlap. Label1 is the bigger one and label2 is the smaller one, completely contained in Label1. The code I used is like:
Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.ScreenUpdating = False
Label2.Visible = False
Label1.Visible = True
### Put your code in here
Application.ScreenUpdating = True
End Sub
Then as you hover out label2, and assuming the labels are correctly overlap, you will hover over Label 1, now visible because you "activated it", and the following code is executed
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.ScreenUpdating = False
Label2.Visible = True
Label1.Visible = False
### Put your code in here
Application.ScreenUpdating = True
End Sub
Please bear in mind that you need to have a figure or button in between the two Active x labels, otherwise you will get a strange result because in the exact moment you hover over Label 2, it will disappear, leaving Label 1 beneath it which will also disappear as soon as you move, makin Label 2 appear again and ... you get the idea. This will not break the excel file but will make it make strange things like blinking or delaying the mouse movement. To prevent thus. I recommend to have that shape as a safe zone.
Hope this helps.

Trying to hide labels on button if color is red in visual studio

This will only work for the labels sender but, I need to use this also for the button sender so it can hide if the label is red only when the button is clicked.
I have 48 Seats
Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Seat9.Click, Seat8.Click, Seat7.Click, Seat6.Click, Seat5.Click, Seat48.Click, Seat47.Click, Seat46.Click, Seat45.Click, Seat44.Click, Seat43.Click, Seat42.Click, Seat41.Click, Seat40.Click, Seat4.Click, Seat39.Click, Seat38.Click, Seat37.Click, Seat36.Click, Seat35.Click, Seat34.Click, Seat33.Click, Seat32.Click, Seat31.Click, Seat30.Click, Seat3.Click, Seat29.Click, Seat28.Click, Seat27.Click, Seat26.Click, Seat25.Click, Seat24.Click, Seat23.Click, Seat22.Click, Seat21.Click, Seat20.Click, Seat2.Click, Seat19.Click, Seat18.Click, Seat17.Click, Seat16.Click, Seat15.Click, Seat14.Click, Seat13.Click, Seat12.Click, Seat11.Click, Seat10.Click, Seat1.Click
Dim Seats As Label = CType(sender, Label)
If Seats.BackColor = Color.White Then
Seats.BackColor = Color.Red
Else
Seats.BackColor = Color.White
End If
Okay, I think I understand now.
You have multiple labels which represent seats and when the user clicks on the labels they change from white to red.
You are wanting to create a button that once clicked, it removes all red labels from the screen/form.
You would place the following in the click event of your button (assuming that your form is called frmMain)
For Each c As Control in frmMain.Controls
Dim l As Label = DirectCast(c, Label) ' Cast from control to label
If Not l Is Nothing ' If this control is indeed a label then process
If l.BackColor = Color.Red Then ' If color is red then hide label
l.Visible = False
Else
l.Visible = True
End If
l.Visible = False
End If
Next
I don't have VS pulled up, so syntax may be a little incorrect but I think you get the point.

VBA legend and plot area resizing

I have an Excel chart which changes on selections made in slicers.
I noticed that the plot area and the legend area change depending on the made selection.
I tried to fix the position and size for the plot area using vba, but this simply does not work unfortunately.
The plot area and the legend keep on resizing, causing the legend overlapping the plot area. Which I obviously do not want.
I have this code, placed in the worksheet page of the vba editor:
Option Explicit
Private Sub Chart_Calculate()
ChartObjects("grafiek 4").Activate
ActiveChart.PlotArea.Width = 637.783
ActiveChart.Legend.Left = 716.514
ActiveChart.Legend.Width = 176.735
ActiveChart.Legend.Height = 295.334
End Sub
having this code, I assumed the automatic resizing would be gone, but I saw the legend sometimes still overlaps te plot area.
Is there a solution which permanently fixes this problem?
Edit1:
Yesterday, I simply added a few parameters for the plot area. It seemed to work then. But now I tried again, and the legend is overlapping the plot area again.
I changed the code to:
Option Explicit
Private Sub Chart_Calculate()
ChartObjects("grafiek 4").Activate
ActiveChart.PlotArea.Top = 33.102
ActiveChart.PlotArea.Left = 67.1
ActiveChart.PlotArea.Width = 637.783
ActiveChart.Legend.Top = 7
ActiveChart.Legend.Left = 716.514
ActiveChart.Legend.Width = 176.735
ActiveChart.Legend.Height = 329.667
End Sub
So with 2 more paramters for the plot area.
edit2:
I have checked the legend properties in Excel. under 'options for legend' there is a checkbox: show legend without overlapping plot area (I do not know the exact english text).
This box is checked, but it does overlap the plot area.
Why is it impossible to achieve this? Having fixed sizes for the plot area and the legend should not be so hard.
edit 3:
I do have this routine now in my workbook:
Option Explicit
Private Sub Chart_Calculate()
ChartObjects("grafiek 4").Activate
With ActiveChart
With .PlotArea
.Top = 33.102
.Left = 67.1
.Width = 637.783
End With
With .Legend
.IncludeInLayout = True
.Position = xlLegendPositionRight
.AutoScaleFont = False
.Font.Size = 8
.Top = 5
.Left = 706.899
.Width = 179.735
.Height = 336.681
End With
End With
End Sub
Sub kopieergrafiek()
ActiveSheet.ChartObjects("Grafiek 4").Copy
End Sub
(including the suggestion in the comment below my post)
I does not seem to work. Does worksheet_change event perhaps works better?
edit 4:
I still do not have a solution for this issue. It already happens when the name of 1 of the legend items is to long to fit the space. And it also happens when there are to many items in the legend to fit in the space available.
I think there is no solution for this. Unless I could somehow tell excel to maximize the number of items in the legend. or to maximize the length of the series name.
I was having this problem myself with the legend resizing the plot area. I tried what Portland Runner suggested, only setting .Legend.IncludeInLayout to false (thus separating the legend from the plot area as he suggested, perhaps he made a typo?) and my plot area was no longer resized.
I also had this problem and found this answer. I found a fix that works for me. Not sure why exactly it works but I do these steps:
Set the legend position
Undock the legend (legend.includeLayout = false)
Resize the plot area to the size I wanted
Re-dock the Legend (legend.includeLayout = True)
Set the Legend.Left position
and after that the legend is correctly positioned and lined up.
I know this is an old thread, but since I had the same issue and was able to solve it, but did not really find the correct answer on this thread, I thought it would be good to post my solution. It is really important to refresh the pivotlayout, otherwise you will not see a difference in the Chart Legend. This will adjust the Legend so it will not overlap the plot area. It will also increase the size of the chart, so if you do not want that, you will need to use other code.
Sub AdjustChartLegendActiveSheet()
Dim j
For j = 1 To ActiveSheet.Shapes.count
If ActiveSheet.Shapes(j).Type = msoChart Then
ActiveSheet.Shapes(j).chart.Legend.IncludeInLayout = True
ActiveSheet.Shapes(j).chart.PivotLayout.PivotTable.PivotCache.Refresh
End If
Next j
End Sub

In Excel, VBA - How can we lock resizing of a rectangle

I have an excel sheet that contains two rectangles and text in other cells.
I need to allow users to only edit the text in the rectangle. They should not be able to change the size of the object.
Applying lock on the rectangle locks the object as well as the text.
Does anyone know how I can achieve this?
Why not create two objects, one being a rectangle that is locked, and one being a text box that is not locked? This is really simplistic, but a possible answer.
Another idea would be to have the rectangle equal a set cell, and let them enter their text in the cell and it would transfer over even when the rectangle is locked.
As far as I am aware Excel does not accommodate Events for shapes and so there is no simple way of detecting a change in a shape size and then resizing the shape.
It is possible to emulate what you are asking for by using a workaround.
Imagine you have two rectangles on your spreadsheet called 'Rectangle 1 and 'Rectangle 2'. When a user finishes updating the text in any given box they must then click the spreadsheet to move out of 'edit' mode for the shape. You can detect this using the Workbook_SheetSelectionChange event.
The following module allows you to set the size of the rectangles as constants and will resize the rectangles accordingly:
Const Rect1Height As Integer = 50
Const Rect1Width As Integer = 200
Const Rect2Height As Integer = 50
Const Rect2Width As Integer = 200
Sub SetRectangleSize()
Dim Rect1 As Shape
Dim Rect2 As Shape
Set Rect1 = ActiveSheet.Shapes("Rectangle 1")
Set Rect2 = ActiveSheet.Shapes("Rectangle 2")
Rect1.Height = Rect1Height
Rect1.Width = Rect1Width
Rect2.Height = Rect1Height
Rect2.Width = Rect1Width
End Sub
Now all you need to do is to call this sub from a workbook level event:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
SetRectangleSize
End Sub
Each time a user updates the text in one of the rectangles they will click back on the spreadsheet and the event is fired, resulting in the rectangles being sized according to the constant height and width parameters that you have defined.

Resources