Combo box that shows only certain sheet? - excel

create combo box that only shows certain sheet instead of all available sheets,plus the ability to click on the sheet even when hidden?
am using forms controls comboBox, In Payment Code
Private Sub cbSheet_Change()
If cbSheet.Value <> "Select a Sheet" Then
Worksheets(cbSheet.Value).Select
End If
cbSheet.Value = "Select a Sheet"
End Sub
Private Sub Worksheet_Activate()
Dim Sh As Worksheet
Me.cbSheet.Clear
For Each Sh In ThisWorkbook.Worksheets
Me.cbSheet.AddItem Sh.Name
Next Sh
End Sub
In ThisWorkBook code
Private Sub Workbook_Open()
If ActiveSheet.Name = "Master Data" Then
Worksheets("Report").Select
Worksheets("Master Data").Select
End If
End Sub

When looping through the sheets, you can make sure not to add the sheet you don't want in the combobox
Private Sub ComboBox1_Change()
Dim sh As Worksheet, s As String
s = Me.ComboBox1
If s = "" Then Exit Sub
Set sh = Sheets(s)
With sh
If .Visible = False Then
.Visible = True
End If
.Select
End With
End Sub
Private Sub Worksheet_Activate()
Dim sh As Worksheet
Application.EnableEvents = False
Me.ComboBox1.Clear
For Each sh In Sheets
If sh.Name <> "Sheet1" Then
Me.ComboBox1.AddItem sh.Name
End If
Next sh
Application.EnableEvents = True
End Sub
The code should add the hidden sheet names
Adding specific sheets the the combbox
Private Sub Worksheet_Activate()
Application.EnableEvents = False
Me.ComboBox1.Clear
With Me.ComboBox1
.AddItem "Sheet2"
.AddItem "Sheet4"
.AddItem "Sheet5"
.AddItem "Sheet6"
End With
Application.EnableEvents = True
End Sub

Related

How to initialize a listbox to a desired starting point in the list?

I created a macro for Excel which opens a list of all visible sheets in a workbook and goes to the desired sheet as you scroll through the list. The idea is to avoid using the mouse as much as possible.
I am forced to scroll down starting from the first item in the list.
I would like to instead "start" from the initial sheet (wherever it may be) so I can scroll up/down depending on what sheet I would like to open.
In other words,
I would like the listbox to populate with all visible sheets
I would like the starting point for the user to be the active sheet so they can scroll up/down from their starting point
Code for the listbox:
Private Sub CommandButton1_Click()
Unload ListBox
End Sub
Private Sub UserForm_Initialize()
Dim WS As Worksheet
For Each WS In Worksheets
ListBox1.AddItem WS.Name
Next WS
End Sub
Private Sub ListBox1_Click()
Sheets(ListBox1.Value).Activate
End Sub
Code which opens the listbox:
Public Sub ShowUserForm()
Load ListBox
ListBox.Show
Debug.Print "===="
Debug.Print
End Sub
another one...
Private Sub UserForm_Initialize()
Dim ws As Worksheet, idx As Long
With Me.ListBox1
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
.AddItem ws.Name
If ws Is ActiveSheet Then
idx = .ListCount - 1 ' item indexes start at zero
End If
End If
Next
.ListIndex = idx '
End With
End Sub
Private Sub ListBox1_Change()
Worksheets(ListBox1.Value).Activate
End Sub
You mentioned "all . . . sheets", if you want to include Chart sheets loop Each objSheet in Sheets and in the change event replace Worksheets with Sheets
what's about that:
Private Sub UserForm_Initialize()
Dim wksTab As Worksheet
For Each wksTab In ThisWorkbook.Worksheets
If wksTab.Visible = xlSheetVisible Then
If wksTab.Name <> ActiveSheet.Name Then
Me.ListBox1.AddItem wksTab.Name
End If
End If
Next wksTab
Me.ListBox1.AddItem ActiveSheet.Name
Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1
End Sub
Best regards
Bernd

Show sheet into which the value was entered when closing form

I have three buttons in userform S1,S2,S3(sheet1,sheet2..) which assign a value specified in the textbox to the first cell on a sheet. How to do that after closing user form show sheet
into which the value was entered/last used sheet
Private Sub Zamknij_Click()
UserForm1.Hide
Worksheets("SheetName").Activate
End Sub
Private Sub Sheet1_Click()
Sheets(1).Cells(1, 1).Value = TextBox1.Value
End Sub
Private Sub Sheet2_Click()
Sheets(2).Cells(1, 1).Value = TextBox1.Value
End Sub
Private Sub Sheet3_Click()
Sheets(3).Cells(1, 1).Value = TextBox1.Value
End Sub
Putting my comments into code: Here is one possible way to implement what I think it is you're trying to do.
Private Sub Zamknij_Click()
Unload Me
End Sub
Private Sub Sheet1_Click()
ApplyToSheet Worksheets(1)
End Sub
Private Sub Sheet2_Click()
ApplyToSheet Worksheets(2)
End Sub
Private Sub Sheet3_Click()
ApplyToSheet Worksheets(3)
End Sub
Sub ApplyToSheet(wsSheet As Worksheet)
With wsSheet
.Activate
.Cells(1, 1) = TextBox1.Value
End With
End Sub
The above will show the target and then write TextBox1.
If you need to keep another or the current sheet visible until Zamknij is clicked, here is another approach:
Option Explicit
'At the top of the userform (before any subs): Declare a module-level variable
Private mwsTarget As Worksheet
Private Sub Zamknij_Click()
mwsTarget.Activate
Unload Me
End Sub
Private Sub Sheet1_Click()
ApplyToSheet Worksheets(1)
End Sub
Private Sub Sheet2_Click()
ApplyToSheet Worksheets(2)
End Sub
Private Sub Sheet3_Click()
ApplyToSheet Worksheets(3)
End Sub
Sub ApplyToSheet(wsSheet As Worksheet)
Set mwsTarget = wsSheet
wsSheet.Cells(1, 1) = TextBox1.Value
End Sub

Automatically protecting Sheets after saving

I am trying to automatically lock cells after user has hit save button. The sheets is protected and contains some unlocked cells where user can input data.
Now I want those cells to get locked after user has clicked data. I have multiple sheets in the workbook but I want this to happen just for the first sheet.
Option Explicit
Dim Ws As Worksheet
Private bRangeEdited As Boolean
'Private WithEvents Ws As Worksheet
Private Sub Workbook_Open()
Set Ws = Range("A1:ZZ27").Parent
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sMSG As String
sMSG = "Beim Speichern der Arbeitsmappe werden die Eingabezellen gesperrt. " & vbLf
sMSG = sMSG & "Möchten Sie fortfahren ?"
If Not bRangeEdited Then GoTo Xit
If Not Me.ReadOnly Then
With Range("A1:ZZ27")
If MsgBox(sMSG, vbExclamation + vbYesNo) = vbNo Then
Cancel = True
GoTo Xit
End If
Unpro_SheeTS
' .Parent.Unprotect "1234"
If .SpecialCells(xlCellTypeBlanks).Address <> .Address Then
.SpecialCells(xlCellTypeConstants).Locked = True
bRangeEdited = False
End If
Pro_SheeTS
' .Parent.Protect "1234"
End With
End If
Xit:
End Sub
Private Sub ws_Change(ByVal Target As Range)
Dim Ws As Worksheet
If Not Intersect(Range("A1:ZZ27"), Target) Is Nothing Then
bRangeEdited = True
End
End Sub
Sub Unpro_SheeTS()
For Each Ws In ActiveWorkbook.Worksheets
Ws.Unprotect ("1234")
Next
Exit Sub
End Sub
Sub Pro_SheeTS()
For Each Ws In ActiveWorkbook.Worksheets
Ws.Protect ("1234")
Next
Exit Sub
End Sub
My guess is that you should have a worksheet_change event function which keeps track of what cell is modified in a list/array/collection as you feel.
On the other hand your Workbook_BeforeSave event would go through the sheets you want and for each of them it would unprotect the sheet, lock the cells (worksheet.range(...).locked = true) then protects the sheet.
(You dont need to protect the workbook for your purpose, it is a different matter)
for you to have better results :
indent your code
refer to ranges from a worksheet explicitly
(worksheet.range)
consider migrating to another language ;)

run time error 1004 unable to set the hidden property of the range class

I get the run time error when I open the workbook. The open function works great without the close function, but as soon as I add the close function I get the error. Any suggestions?
Private Sub Workbook_Open()
Application.ScreenUpdating = False
For Each cell In Range("A1:Z1")
If cell.Value = "X" Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Protect "1962"
Next ws
ThisWorkbook.Protect "1962", True
ThisWorkbook.Save
End Sub
The error occurs because you protect the worksheet in the BeforeClose routine. Hence the Workbook_Open doesn't have access to update it the next time it is being opened. Try this:
Private Sub Workbook_Open()
Dim cell As Range
Application.ScreenUpdating = False
ActiveSheet.Unprotect "1962" '<<<<
For Each cell In Range("A1:Z1")
If cell.Value = "X" Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
ActiveSheet.Protect "1962" '<<<<
End Sub

"The item with the specified name wasn't found". error

I made the macro code so that it checks cell "w6" in all worksheets when the excel file opens. When "w6" is empty, it should hide graph "FG" and only show graph "F".
When "w6" is not empty, it should hide graph "F" and only show graph "FG". I set the names of each graph as "F" and "FG". But there is an error message in the
wsht.Shapes.Range(Array("FG")).Visible = msoFalse line in the HideFG macro that "the item with the specified name wasn't found." I am sure that the graph name is "FG", but why is this happening? Is there an excel genius who can solve this?
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG ws
Else
HideF ws
End If
End With
Next
End Sub
Sub HideF(wsht As Worksheet)
'
' HideF Macro
'
'
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG(wsht As Worksheet)
'
' HideFG Macro
'
'
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Instead of calling another macro, you could just simply try the following:
If what you refer to are actually Shapes:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Shapes.Range(Array("F", "FG")).Visible = False
If ws.[W6] = 0 Then
ws.Shapes("F").Visible = True
Else
ws.Shapes("FG").Visible = True
End If
Next ws
End Sub
Or when they actually are ChartObjects then:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.[W6] = 0 Then
ws.ChartObjects("F").Visible = True
ws.ChartObjects("FG").Visible = False
Else
ws.ChartObjects("F").Visible = False
ws.ChartObjects("FG").Visible = True
End If
Next ws
End Sub
As per this question of yours, I feel you might not want to loop all the sheets.
In that case, change:
For Each ws In ThisWorkbook.Sheets
Into:
For Each ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
Let me know if it works for you :)

Resources