Hide only the sheet related to showed user form - excel

I use this code to hide the Excel sheet while I use the user form
Private Sub Workbook_Open()
Application.Visible = False
Calculator.Show
End Sub
But it hides all the Excel sheets I'm working on. Is there another code that enables me to hide only the user form sheet?

Your Application.Visible = False is referring to the entire Excel application, rather than a particular sheet or user form.
It's not entirely clear if you are trying to hide a sheet or a user form.
I think you are trying to hide a sheet and then show a user form.
If so, you can try this:
ThisWorkbook.Sheets("YourSheetName").Visible = xlSheetHidden
Calculator.Show
Note: you must hide the sheet before showing the userform (otherwise, the sheet will stay visible until the userform is closed).

Related

Excel graphical glitches when moving between sheets with private subs?

I encountered an odd graphical glitch when running private subs on a sheet then using a macro to jump to another sheet with a private sub on it. Basically excel is getting slowed down (the drop down menu's in the ribbons get messed up as well).
IE:
Sheet 1 has
Private Sub Worksheet_Deactivate()
Sheets("Sheet1").Visible = False
End Sub
Sheet 2 has the above code as well except Sheet2 would be the one made hidden when deactivating the worksheet.
With a button placed on sheet1 which trigger the following macro
Sub Sheet1_Button1_Click()
Sheets("Sheet2").Visible = True
Sheets("Sheet2").Select
End Sub
For testing purposes I was just using another macro assigned button on sheet2 that jumped back to sheet one and found that caused the issue. Does anyone know what's going on here and how to prevent it? Maybe this is more of a Microsoft issue?
In my original workbook I had a private sub on a "Cost Estimations" sheet that would run some some code to un-hide used lines and re-hide unused lines in a table that was referencing another sheet. Then I had a macro assigned button on that same sheet that would open a normally hidden sheet with some more info on it. The "hidden" sheet had a private sub on it that automatically hide it when the user clicks off of the sheet just like the "Sheet1" in my example. Additionally in the original workbook it was causing all the information from "cost estimations" to display on the "hidden" sheet, but only if calculations were set to automatic. I was however unable to replicate that in my test worksheet.

Allow users to print protected worksheet

I have a worksheet that is populated from userform information, and from a few other spreadsheets in the same file. I have used the following code:
Application.Dialogs(xlDialogPrint).Show
I ensure the sheet is activated before printing, and I have gotten the function working correctly.
This sheet and others in the workbook must be protected to prevent changes by the user outside of a userform. When I protect the sheet and add appropriate code to unlock that sheet a few things happen:
1. The userform is filled as designed.
2. The print dialog box shows up as designed.
3. When the print button is pressed on the dialog box, it will not print. Not a printer issue: It will not print to PDF either.
How can I get it to actually print the worksheet like I need?
You can unprotect the sheet by using
ActiveSheet.Unprotect
https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.unprotect
Then protect it after working with it using
ActiveSheet.Protect
https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.protect
banal code below must do the work without need to unprotect and protect
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:=True

Userform needed only once

I'm a new VBA user. I have a workbook with multiple worksheets. Each one has it own userform for data entry. The userform shows when I click on the sheet. After I'm done entering data, the worksheet is populated and the userform closes (unload). All this works well. However, after the initial data entry is complete, the goal is to use the data on the worksheets for other applications and the userform is no longer needed. What is the code or the terminology to say the userform should not reappear again when the worksheet is clicked on? Currently, I red X out of the userform. If I click the command button to close, it repopulates and I lose all my data.
Thanks!
As A.S.H commented; you could store the information in a number of ways. An easy example is declaring a variable outside of the Macro:
Public FormOpened as boolean
And then set FormOpened as true once the form has been shown. Then you could add a check to the start of the mouse-click macro:
If FormOpened = True then Exit sub

How to Show/Hide all comments on an Excel sheet (but not impact other sheets)

I want to have a button Show/hide all comments on the active Excel sheet only. I implemented and am fairly happy with this solution (using Application.DisplayCommentIndicator):
Simple VBA Show/Hide Excel Comments problems
The only issue is that this solution shows/hides comments on every sheet open in the application. I need it to only operate on the active sheet (IE. the one with the button) This is especially inconvenient because the other sheets (in other workbooks) don't have the Show/Hide button.
I have a semi-fix for your issue. Let's say if the user pressed your button to hide the comments, which is located in Sheet1, but then decides to switch to another worksheet, all comments would be activated again.
Problem: if the user switches back to Sheet1, the comments will be hidden and the user would have to press unhide regardless of their previous selection if they would like to see the comments:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet1" Then
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
Else
Application.DisplayCommentIndicator = xlCommentAndIndicator
End If
End Sub

How to prevent user from deleting sheet, but leave all else open

I have developed an Excel Application in which useres can enter custom categories. I let the user call up and hide this data entry worksheet with a button in the custom ribbon.
Now I've realized that the user can accidentally delete these worksheets. How do I disable the delete command for this worksheet while leaving all else open?
I've been searching the web but have come up empty on this.
This is for Excel 2007
Thanks
Protect them.
Tools > Protection > Protect Worksheet.
Add the password and choose what actions your users should do in the sheets.
You can do the same using VBA too. Check the following link
Updated with a code for sheet level protect
You may put the following code in the sheet that you need to manage any mischief ;)
Private Sub Worksheet_Activate()
ThisWorkbook.Protect Password:="Password", Structure:=True
End Sub
Private Sub Worksheet_Deactivate()
ThisWorkbook.Unprotect Password:="Password"
End Sub
But you see, when you have a book with 100 sheets and if you want 50 sheets to be protected.
Then you gotta either save all the sheet indices into a very hidden sheet. Usee that list in a module level VBA code to trigger the protect. Because not everytime you will have sheets in asceding order. If sheet indices in an order you can simply iterate them.
Let me know if you like to have workbook level code as well.

Resources