How to make all sheets in workbook visible using VBA - excel

My Excel workbook has 4 sheets but the VBA editor shows 5 sheets in same workbook.
How do I make all sheets visible?

Edited Answer. See comments from Jeeped and Gserg(at original question)
Maybe the missing worksheet is very hidden.
Try this code to unhide it.
Sub test()
Dim ws As Worksheet
For Each ws In Sheets
ws.Visible = True
Next
End Sub
Changing the visible property in the properties-window also works.

Related

Copy/Paste worksheet to new workbook without that worksheet's VBA code

What I want to do is to create a normal Excel workbook (.xlsx) where I summarize the inputs/outputs of some simulations by copying some sheets from a Macro-enabled workbook (.xlsm) and pasting in a normal Excel workbook. The original sheets have macros, shapes, named ranges, dropdown lists, and some formatting. I want some properties of the sheets to come over to the new workbook (named ranges, row and column formatting, cell formatting) but not others (shapes, dropdown lists, macros). I also have to distribute the .xlsm workbook to other users, so I want a solution that doesn't require the user to grant permissions.
If I copy/paste like below, then I get all the properties of the worksheet to come over to the new workbook. I have figured out how to delete shapes and remove dropdown list formatting from the sheet in the new workbook, but I can't remove the worksheet's VBA code without modifying the VBA references.
If I paste special, then I can avoid bringing over the worksheet's VBA code, but I can't bring over the named ranges.
CopySheetToWB(sht as string, wb_New as workbook)
Dim sht_Name as string, rng As Range, shp as shape
Dim ws As Worksheet, wb As Workbook, ws_New As Worksheet
' set sheet in CURRENT wb
Set wb = ThisWorkbook
Set ws = wb.Worksheets(sht)
' copy/paste sheet to NEW wb
ws.Copy after:=wb_New.Sheets(wb_New.Sheets.Count)
' delete shapes from NEW ws
Set ws_New = wb_New.Worksheets(sht)
For Each shp In ws_New.Shapes
shp.Delete
Next shp
' remove dropdown lists from copied sheet
ws_New.Cells.Validation.Delete
End Sub
The answer to my problem turned out to be quite simple. My old code that wasn't working and my new code that is working. If you save the file type correctly, then the macros in the worksheets are not a problem as they are removed.
wb_New.SaveAs FileName:=str_fName, FileFormat:=xlWorkbookNormal 'old code
wb_New.SaveAs FileName:=str_fName, FileFormat:=51 'new code
Also make sure to include the file extension in str_fName. VBA doesn't automatically append the correct file extension based on the FileFormat you choose.

EXCEL can't unhide hidden sheet

Trying to create a workbook that gets data from ACCESS. I can open the workbook and was adding some code to auto open in excel. But when i tried to edit it keeps telling me that I must unhide hidden work sheet. And the unhide command is grayed out. When I first set it up I selected Personal Workbook, which I think applies to any book I open. Along the way I kept deleting workbooks in order start over, so I think there is nothing really to delete. I wanted to add this code to to auto start but I can't get to the code. The follwing code is supposed to unhide all hidden workboks/sheets:
Sub Viewit()
Dim Ws As Worksheet
Application.ScreenUpdating = False
For Each Ws In Worksheets
Ws.Visible = True
Next Ws
Application.ScreenUpdating = True
End Sub
Looking for a better solution and what I am doing that is wrong.
Thanks
There is a very hidden level :
ActiveWorkbook.Sheets("sheet name").Visible = xlSheetVeryHidden
Or xlSheetVeryVisible for the opposite effect
Note this level is only controllable through vba, but functions can work with cells on very hidden sheets.

How to disable the display of pagebreaks for the workbook in Excel VBA?

I am able to disable the display of page breaks for the single worksheet in Excel,but i want to disable for the whole workbook at a time .Please someone help me to disable the display of pagebreaks for the workbook by using VBA.
This has already been answered, but here's what you need:
Sub DisablePagebreak()
Dim ws As Worksheet
For Each ws In Worksheets
ws.DisplayPageBreaks = False
Next
End Sub
Worksheets is the collection of all Worksheets from the current Workbook. As the name's implying .DisplayPageBreaks hides/shows the page breaks.

Prevent user duplicating or adding new sheets to Excel manually

I am new in using Excel VBA. I have an Excel VBA code that will duplicate a certain sheet for n times. I want to prevent user from changing the workbook structure manually (such as adding new sheet, duplicating sheet, or moving sheet), but I still want the VBA to be able to run.
How should I do it?
I was hoping you could use UserInterfaceOnly but that appears to be only worksheet level and not workbook level - it locks the worksheet for users, but allows VBA code to make updates.
The only other way I can think of is to lock and unlock the workbook as needed:
Sub Test()
Dim wrkSht As Worksheet
UnlockWorkbook
Set wrkSht = ThisWorkbook.Worksheets.Add
LockWorkbook
End Sub
Sub LockWorkbook()
ThisWorkbook.Protect Password:="aaa", Structure:=True
End Sub
Sub UnlockWorkbook()
ThisWorkbook.Unprotect Password:="aaa"
End Sub

Hide sheets in Excel VBA

I am trying to create an Tracker. Need help with these ideas:
When the Excel book is opened, it must show only the "Tracker" Worksheet. All other sheets need to be hidden. Now am using the code {sheet.visible = xlveryhidden} But the code is too long, I have to include each sheet name. I need help with a code to show only the Tracker sheet but to hide all other sheets in the workbook.
I've included two comboboxes as year and month. Also included a Command button OK & Cancel. When clicked on OK, it has to verify the IF condition and show a specific sheet. I have no issues with that. it is showing a specific sheet. But I need a single line code to hide all other sheets
I have also included a "New Tracker" Command button. When Clicked on it, it must include a New sheet. Also too let the user to rename it. This worksheet must also be hidden once saved.
I aware the process is simple, but not sure how to complete it.
To hide all but the Tracker worksheet
Sub hidesheets()
Dim ws As Worksheet, wb As Workbook 'Create the variables we'll use for our worksheet and workbook objects.
Set wb = Excel.ActiveWorkbook 'Set the workbook variable to equal the active workbook. This can also be set to equal a named workbook if you want.
For Each ws In wb.Worksheets 'Loop through each worksheet in the workbook.
If Not ws.Name = "Tracker" Then 'If the worksheet's name isn't "Tracker" then...
ws.Visible = xlSheetVeryHidden 'Set it to very hidden.
Else 'If it is the "Tracker" worksheet, then ...
ws.Visible = xlSheetVisible 'Set it to visible.
End If 'End the IF/THEN statement.
Next ws 'Repeat for the next worksheet in the workbook.
End Sub
You'll have to modify this if you want it to work with a command button or do something with another worksheet. But it should get you started. I'd do it but I don't quite have the time right now.

Resources