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
Related
I do a lot of Visio ShapeSheet editing and it would save me a tremendous amount of time to automatically switch to the current shape's sheet when I select a new shape. Let's assume I only have 1 ShapeSheet open, only select 1 Shape, and have all the windows docked on the Visio app (I don't have RegEdit powers to change this).
So far, I've got the following VBA code in ThisDocument:
Private WithEvents vsoWin as Visio.Window
Private Sub ThisDocument_RunModeEntered(ByRef doc as IVDocument)
'Just assume this is the correct window
Set vsoWin = ActiveWindow
End Sub
Private Sub vsoWin_SelectionChanged(ByRef win as IVWindow)
'If nothing is selected, leave
If vsoWin.Selection.Count < 1 Then Exit Sub
'Look for a ShapeSheet (Window.SubType = 3)
For each oWin in Application.Windows
If oWin.Subtype = 3 Then
Application.ScreenUpdating = False 'Pause screen to prevent jitter
oWin.Close 'Delete old ShapeSheet
vsoWin.Selection(1).OpenSheetWindow 'Make new ShapeSheet
Application.ScreenUpdating = True 'Update visuals
Exit For 'Stop looking for ShapeSheets
End If
Next
Exit Sub
(The above code is written from memory since I don't have access to Visio at the moment. Please forgive any minor errors)
This code works, but I'm hoping for a less jittery result. Application.ScreenUpdating = False doesn't seem to do anything in this case: I still briefly witness the old ShapeSheet closing, the drawing window resizing, then the new ShapeSheet opening. Swapping the order (open new window > close old window) is a little less chaotic, but not great. Using Application.Minimize to hide the swap instead is slightly better on the eyes, but still not a smooth transition.
My question: Is there a smoother way to display the active shape's ShapeSheet?
This code works at my side! I just add variable which related with Visio Application - vsoApp.
Private WithEvents vsoWin As Visio.Window
Private WithEvents vsoApp As Visio.Application
Sub st()
Set vsoWin = ActiveWindow ' initialize Window variable
Set vsoApp = Application ' initialize Application variable
End Sub
Private Sub ThisDocument_RunModeEntered(ByRef doc As IVDocument)
'Just assume this is the correct window
Set vsoWin = ActiveWindow
End Sub
Private Sub vsoApp_SelectionChanged(ByVal Window As IVWindow)
'If nothing is selected, leave
If vsoWin.Selection.Count < 1 Then Exit Sub
'Look for a ShapeSheet (Window.SubType = 3)
For Each oWin In Application.Windows
If oWin.SubType = 3 Then
Application.ScreenUpdating = False 'Pause screen to prevent jitter
oWin.Close 'Delete old ShapeSheet
vsoWin.Selection(1).OpenSheetWindow 'Make new ShapeSheet
Application.ScreenUpdating = True 'Update visuals
Exit For 'Stop looking for ShapeSheets
End If
Next
End Sub
My workaround:
Press Alt+F8 keys and run St sub-routine.
Open ShapeSheet window for selected shape.
Select another shapes and so on...
Update with your code i get error like this.
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.
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
I know, I am asking an unusual question. But, please do help me.
I have a below code on Workbook that will take care of copy/paste data on sheets. It would allow me to paste data into the cells without changing format(past only values).
Basically, the code will use destination formatting. similar to "paste values". It would allow the user to paste data from any other format. So that format is consistent across sheets.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim vNewValues as Variant
NewValues = Target
Application.EnableEvents = False
Application.Undo
Target = NewValues
Application.EnableEvents = True
End Sub
Along with above code, I also have another code on the sheet that will help me to clear the contents and code is linked to a button. So, when the button is pressed it will clear the contents of the sheet.
Private Sub ResetKey_Click()
If MsgBox("Content of the sheet will be deleted and cannot be restored", vbOKCancel + vbInformation) = vbOK Then
Worksheets("User").Range("E19:I3018").ClearContents
Else
Exit Sub
End If
End Sub
Concern: I see a conflict between these codes. Because, when I click on the button I get the error that will point me to Application.Undo in the first code. I tried debugging the code but I was not able to get both to work. Please Suggest.
This will work:
Private Sub ResetKey_Click()
If MsgBox("Content of the sheet will be deleted and cannot be restored", vbOKCancel + vbInformation) = vbOK Then
Application.EnableEvents = False
Worksheets("User").Range("E19:I3018").ClearContents
Application.EnableEvents = True
Else
Exit Sub
End If
End Sub
That is, you have to suppress the Change event in other macros working on that sheet. Not elegant but doable.
To clarify what the first macro does: it saves the cell's content, undoes a user's paste or input, and then only fills in the value which was pasted, leaving the format intact. The problem with this approach is that the event handler does not return information on the action that triggered it - it could be a paste but clearing cells as well.
You can only use .Undo to undo the last action in the worksheet not to undo vba actions and must be the first line in the macro. As explained in the documentation.Application.Undo. Quote below:
This method undoes only the last action taken by the user before
running the macro, and it must be the first line in the macro. It
cannot be used to undo Visual Basic commands.
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