Excel: If I open two windows on the same workbook, why do macros stop working in the first window? - vba

I am trying to set up a button in Excel to show a dual view of two separate worksheets at the same time. This is the code I've written so far (see below). So far the code seems to work. The problem is that the top window has some activex controls on the worksheet, and they seem to stop working until the bottom window is closed again. Why is this happening, and what can I do to fix it? Thanks.
Private Sub DualViewButton_Click()
Dim windowToPutOnTimeline As Window
If Windows.Count = 1 Then
ThisWorkbook.NewWindow
Windows.Arrange xlArrangeStyleHorizontal, True, False, False
Set windowToPutOnTimeline = Windows(1)
If Windows(1).Top < Windows(2).Top Then
Set windowToPutOnTimeline = Windows(2)
End If
With windowToPutOnTimeline
.Activate
HorizontalTimelineSheet.Activate
.DisplayGridlines = False
.DisplayRuler = False
.DisplayHeadings = False
.DisplayWorkbookTabs = False
'.EnableResize = False
End With
Windows(2).Activate 'go back to the right focus the user expects.
Else
If Windows(1).Top = Windows(2).Top Then
Windows.Arrange xlArrangeStyleHorizontal, True, False, False
Else
Windows.Arrange xlArrangeStyleVertical, True, False, False
End If
End If
End Sub
EDIT: if I switch the window that's being assigned to windowToPutOnTimeline then the problem goes away. So I've essentially worked around the problem without knowing why it works the other way. (see code snippet below)
With ThisWorkbook
Set windowToPutOnTimeline = .Windows(1)
Set windowToPutOnDataSheet = .Windows(2)
tmp = .Windows(1).Top
.Windows(1).Top = .Windows(2).Top
.Windows(2).Top = tmp
End With

This behavior is a bug in ActiveX control.
As a work around, use a button from the Forms Controls, rather than an ActiveX button
Using the Forms button you will need to add a Module, declare a Sub with your code and assign the Sub as the action macro to your button (as apposed to putting your code in the click event of an ActiveX button)
I tried this on Excel 2007, seem to work OK - the button appears and works on both windows

Related

VBA code for unhiding a bookmarked text is not working

I've created an ActiveX dropdown list and each option is linked to a bookmark for the text. Under the ActiveX controls there are the bookmarks (R1 andR2), hidden.
When I hit the btnselect button, all the other bookmarks, except the selected one, get deleted and the selected one becomes visible.
In the bookmark R2
I have a MacroButton for showing/hiding another text (CollapseMentiuniReclamant). When clicking the button it runs either Expand1 sub or Collapse1 sub, but the bookmark CollapseMentiuniReclamant doesn't show up.
I've simplified the document and codes as much as possible. Link to the document - https://wetransfer.com/downloads/1caea3c5d3b05e226e8b8f6a29760ad220190522071742/15db59.
The vba code is:
Private Sub btnselect_Click()
If ComboBox1.Value = "1" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("R2").Range.Delete
End If
If ComboBox1.Value = "2" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R1").Range.Delete
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End If
End Sub
Sub Expand1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Collapse1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
Sub Collapse1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Expand1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End Sub
Update: I've simplified the last part of code and the problem still persists:
Sub Expand1()
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
I've even removed the button entirely and ran the macro from View Macros Tab and it's not working.
Why doesn't CollapseMentiuniReclamant show up?
It's not showing up because what you're trying to hide/unhide isn't inside the bookmarked range. In any event, you should be inserting/deleting the content, not simply toggling it's hidden property. Making something hidden is no guarantee it won't be seen or printed (even if not seen), as those settings depend on how the end user has Word configured.

Click event on one control sometimes does not fire

I have two buttons on a sheet. On occasion, clicking on one of the buttons does not "fire" the event.
Floating the cursor over the button changes the cursor from a cross to an arrow
The button does not appear to "depress" as it would if the event fired.
Breakpoint or Stop statement in the code do not get reached.
The other button never demonstrates this problem.
If the code is started manually, the code will run as designed.
In Designer Mode, the code appears to be attached to the button.
Closing and re-opening the worksheet, and then triggering the button that normally works will restore functionality to this button.
Trouble shooting advice would be appreciated.
The button code is below as is the properties window, but I think the problem lies elsewhere; just don't know where to look.
Again, another very similar button, which calls different code, seems to work OK all the time.
Thanks for any guidance.
Option Explicit
Private Sub cbDeleteViewed_Click()
With cbDeleteViewed
.Width = .Width
End With
Application.ScreenUpdating = False
Select Case Environ("COMPUTERNAME")
Case "RON-DODIER"
sDrive = "F:\"
sBasePath = "Videos"
ProtectDisable Sheet1
UnProtectEnable Sheet3
Sheet3.Select
Case "RONBP"
sDrive = "Z:\"
sBasePath = ""
ProtectDisable Sheet3
UnProtectEnable Sheet1
Sheet1.Select
Case Else
MsgBox "Cannot Run on This Computer"
Exit Sub
End Select
SetUpZ
DeleteViewedShows
Application.ScreenUpdating = True
End Sub

Disable checkboxes when sheet opened in vba

I have 3 checkboxes. When a user opened my sheet, he/she must not check checkboxes. I want them to be disabled. How can I do that?
Not sure if you meant ActiveX or FormControl, so here you go
Code
Private Sub Worksheet_Activate()
Dim myActiveX As Object
Set myActiveX = Sheet1.OLEObjects("CheckBox1")
myActiveX.Object.Value = True
myActiveX.Object.Locked = False ' Make it False if you wish to enable it
myActiveX.Object.Enabled = False ' Another option to disable
Dim myFormControl As CheckBox
Set myFormControl = ActiveSheet.Shapes("Check Box 1").OLEFormat.Object
myFormControl.Value = True
myFormControl.Enabled = False ' Make it True if you wish to enable it
End Sub
Live GIF demo
You have to write some VBA code in order to do that.
Let's suppose that you have 3 CheckBoxes in your first sheet.
By holding the "Alt" key on your keyboard and the pressing one time the "F11" key, opens Microsoft Visual Basic. ( Alt + F11 )
At your left hand you can see the "VBAProject" tree.
Double click on the "ThisWorkbook" file and copy the following code in the window it will appear:
Private Sub Workbook_Open()
void = uncheckAllCheckboxes()
End Sub
Function uncheckAllCheckboxes()
ThisWorkbook.Worksheets(1).CheckBox1.Value = False
ThisWorkbook.Worksheets(1).CheckBox2.Value = False
ThisWorkbook.Worksheets(1).CheckBox3.Value = False
End Function
Save the excel file as "Excel 97-2003 Workbook" type (.xls)
Close your excel.
Open the file you have previously saved and everything will work fine.
;)
P.S.: It is important to enable macros from your excel settings

Compile error with If statement on radiobutton.value

I'm trying to perform an action 'if a radioboxed have been checked", but I'm getting an error:
Compile error: Method or data member not found.
I've created a userform with four radiobuttons (using Controls toolbox), and a commandbutton. The userform loads in an excelsheet (with the click of a separate button), and it is possible to check the radiobutton. if the radiobutton is checked and I click the command button I want some action to happen, but it wont compile my code.
Private Sub cmdCSV_Click()
Dim JurBen As Integer
With Thisworkbook
If .lblRKinst.Value = True Then
JurBen = 1
MsgBox "hurray"
ElseIf .lblRKkon.Value = True Then
JurBen = 2
ElseIf lblForinst = True Then
JurBen = 3
ElseIf lblForkon = True Then
JurBen = 4
Else: Exit Sub
MsgBox ("Choose an option")
End If
It seems to dislike the "value" statement, which works fine with checkboxes? I've tried with "enabled" and without anything. I seem to be the only person on the internet with this problem...
As I've used loads of time on this tiny issue, and seem to be stuck, any help would be greatly appreciated!
If the Radioboxes are on UserForm then if you want to check their value then 1. the UserForm must be loaded at that time and 2. you need to refer to the UserForm.
Example:
if UserForm1.OptionButton1.Value = true then
The radiobox (OptionButton1 in my example) is member of UserForm and not of ThisWorkbook.
As written by Matteo NNZ I was simply referencing the label and not the radiobutton next to it.
No problem what so ever, as the code above works fine.

Right click on sheet-tabs disabled in Excel

I used this vba code in the ThisWorkbook module to disable the right click menu in an Excel workbook.
Private Sub Workbook_Activate()
With Application.CommandBars.FindControl(ID:=847)
.Visible = False
End With
End Sub
Private Sub Workbook_Deactivate()
With Application.CommandBars.FindControl(ID:=847)
.Visible = True
End With
End Sub
Works like a charm.
Problem is, I can't access the right click menu on tabs in ANY workbook now.
The second part of the code is supposed to turn it back on, I assumed? Yet it doesn't.
Even when I remove the code entirely, no workbook, not even a new one, has a menu when I click right on one of the tabs.
Is there a general vba codesnippet that "resets" excel maybe? Or a general "enable all menus" thing?
REVISION:
This code posted here doesn't disable the rightclick menu, it removes the "delete" option from that specific menu.
omg
Application.CommandBars("Ply").Enabled = True
-.-
Started googling different keywords after the last edit and BAM.
Late again as usual, but tackled with the same problem today. Here's the solution to get your right-click functionality back:
Option Explicit
'
Sub tester()
'
Dim cBar As CommandBar
'
For Each cBar In CommandBars
Debug.Print cBar.Name
If (cBar.Type = msoBarTypePopup) Then cBar.Enabled = True
Next
End Sub
Also note that the below also exist. Some macro from work had them all disabled in my Excel.
Application.CommandBars("Cell").Enabled = True
Application.CommandBars("Row").Enabled = True
Application.CommandBars("Column").Enabled = True