I have tried to macro my workbook select a specific worksheet but unsuccessfully. Please see picture attached for the code.
Any ideas how to fix it or what I have done wrong?
Put your workbook open sub in ThisWokrbook rather than in the sheet.
Add this to ThisWorkbook
Private Sub Workbook_Open()
Run "OpenSheet"
End Sub
and in a module add:
Sub OpenSheet()
'Activate Workbook
Workbooks("urworkbook.xls").Activate
'Activate Worksheet
Workbooks("urworkbook.xls").Sheets("urSheet").Activate
End Sub
just some improvements to the Workbook_Open procedure :
(It will display Full Screen, like a real application)
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Run "OpenSheet"
End Sub
Related
I have this VBA written. Essentially what it does is; I've used 'Define Range' ("Test") on a few cells in a worksheet. The VBA makes sure that whenever the workbook is opened, it automatically zooms in to fit that range. Right now, I think it only triggers when the workbook is opened, and it gives an error message if that worksheet is not selected when the workbook is opened. Now, I would like it to run only if the specific worksheet is selected. I've literally copied it from someone else so don't really know how I would amend this...
Thanks in advance!
Private Sub Workbook_Open()
Range("Test").Select
ActiveWindow.Zoom = True
Cells(1, 1).Select
End Sub
Zoom & Scroll To Cell When Worksheet Is Activated...
... and if the worksheet is active when the workbook is opened.
It is assumed that the range is of Workbook scope.
ThisWorkbook Module
Option Explicit
Private Sub Workbook_Open()
Const WorksheetName As String = "Sheet1"
If ActiveSheet.Name = WorksheetName Then SelectMyRange
End Sub
Sheet Module, e.g. Sheet1...
... where Sheet1 is the code name i.e. the name not in parentheses, e.g. Sheet1(Data), seen in the VBE Project Explorer window.
Option Explicit
Private Sub Worksheet_Activate()
SelectMyRange
End Sub
Standard Module, e.g. Module1...
... created by using Insert Module.
Option Explicit
Sub SelectMyRange()
Const RangeName As String = "Test"
With Range(RangeName)
.Select
ActiveWindow.Zoom = True
Application.Goto .Cells(1), True
End With
End Sub
On opening of a specific workbook I want to hide some excel functions, like scroll bars or ribbon. Problem is that all other woorkbooks are affected...I use "Workbook" & Open with the following:
Private Sub Workbook_Open ()
Application.ExecuteExcel4Macro "show.toolbar(""ribbon"", false)"
Application.DisplayScrollBars=False
End Sub
I also tried the same using With Application and With ThisWorkbook.
I don't know how to limit the macro to work when only a specific Excel workbook is opening, without affecting other already opened woorkbooks, or workbooks which will be opened after the specific one is already open.
The Ribbon lives at Application level; consider hiding it in Workbook_SheetActivate and showing it again in Workbook_SheetDeactivate handlers in ThisWorkbook.
Option Explicit
Private Sub Workbook_Open()
'invoked when ThisWorkbook is opened.
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is activated.
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is deactivated.
End Sub
Now, these events would be firing when activating/deactivating sheets in ThisWorkbook, ...and I would expect this to cause some kind of annoying flickering with the Ribbon whenever a sheet in ThisWorkbook deactivates and then another sheet in ThisWorkbook is activated immediately after.
You can use the Sh argument to determine whether the ActiveSheet is still in ThisWorkbook, and prevent hiding the Ribbon in SheetDeactivate then:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is deactivated.
If Not ActiveSheet.Parent Is ThisWorkbook Then
CommandBars.ExecuteMso "HideRibbon" 'will show the Ribbon if it's hidden
End If
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is activated.
CommandBars.ExecuteMso "HideRibbon" 'will show the Ribbon if it's hidden
Application.DisplayScrollBars=False
End Sub
Consider toggling Application.DisplayFullScreen instead of invoking Excel4 macros or obscure MsoID commands.
I know there are lots of questions on this, which I have read - but none seem to give me the code I need to make this work.
I have a number of buttons that I have placed in the Ribbon of my excel sheet. These are attached to macros that copy sheets onto another sheet, as an example
The macro is ran by pressing the button:
Sub btnSheet1_onAction(control As IRibbonControl)
mFunction.CopySheet1toSheet2
End Sub
The macro is contained in my mFunction module as :
Public Sub CopySheet1toSheet2()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
ws.Cells.Copy Destination:=ThisWorkbook.Sheets(2).Cells
End Sub
Now.... I need to protect items/cells in sheet 1 and 2. When I protect the sheets the macros make excel crash - no runtime errors or anything.
I have inserted the following code into the 'ThisWorkbook'
Private Sub Workbook_Open()
Sheets(1).Protect Password:="secret", UserInterFaceOnly:=True
Sheets(2).Protect Password:="secret", UserInterFaceOnly:=True
End Sub
But it still doesn't work - I have also tried with the following code in the mFunction module
Public Sub CopySheet1toSheet2()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
ws.Unprotect Password = "secret"
ws.Cells.Copy Destination:=ThisWorkbook.Sheets(2).Cells
ws.Protect Password = "secret"
End Sub
But that doesn't seem to work either - I am guessing it might be something to do with the fact that the macro is copying the sheet into another sheet that is locked also?
I should also note that there are other sheets in the workbook that are protected, but that do not have macros attached to them, so they stay protected, could this be causing an issue?
Some help would be greatly appreciated!!
UserInterFaceOnly
When you save a Workbook with sheets that have been protected using UserInterFaceOnly, this property is removed on the file that is saved. So on reopening the file the sheets will remain protected but can not be changed programmatically either.
So, regarding this piece of code, which on first glance appears to do exactly what you need:
Private Sub Workbook_Open()
Sheets(1).Protect Password:="secret", UserInterFaceOnly:=True
Sheets(2).Protect Password:="secret", UserInterFaceOnly:=True
End Sub
.. if you save and reopen your file, when your above Workbook_Open() runs it will fail to set the protection as there is already protection in place.
The workaround is to include lines for each sheet that remove any protection in place first. Then you can set it again correctly - like so:
Private Sub Workbook_Open()
Sheets(1).Unprotect Password:="secret"
Sheets(2).Unprotect Password:="secret"
Sheets(1).Protect Password:="secret", UserInterFaceOnly:=True
Sheets(2).Protect Password:="secret", UserInterFaceOnly:=True
End Sub
This should then allow your copy code to run without issue as I can't see much wrong with that part at all.
Incidentally, if your passwords are the same, you could tidy it up slightly with:
Private Sub Workbook_Open()
Dim sh As Worksheet
For Each sh In Array(Sheets(1), Sheets(2))
sh.Unprotect Password:="secret"
sh.Protect Password:="secret", UserInterFaceOnly:=True
Next
End Sub
Okay - so I have used this as a work around, but if anyone can give a more eloquent solution that would be great:
Dim ws As Worksheet
Set ws1 = ThisWorkbook.Worksheets(1)
Set ws2 = ThisWorkbook.Worksheets(2)
ws1.Unprotect ("2402")
ws2.Unprotect ("2402")
ws1.Cells.Copy Destination:=ws2.Cells
ws1.Protect ("2402")
ws2.Protect ("2402")
I have 2 workbooks. Workbook A & B.
When workbook A opens, workbook B opens automatically. Auto open workbook macro is working nicely. But when I want to close workbook A, the workbook B should close together automatically. But with the auto close macro below, it doesn't seems to work. Please advise where went wrong :
Private Sub Workbook_Close()
If Workbooks.Close("A.xlxm") Then
ThisWorkbook.Close
End If
End Sub
The code above is pasted in workbook B's ThisWorkbook.
In workbook A in the ThisWorkbook code pane.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.Workbooks.Item("B.xlsm").Close True
On Error GoTo 0
End Sub
I am creating a macro for my co-workers. They get a file daily and at the end of the day have to copy certain information to another workbook. The macro is to take care of the copying. I want to have a userform with a combobox popup that contains a list of current open workbooks so it knows which file to copy from. How do I set it up so that the selection made there sets a workbook variable with that selection?
What I'm trying to do is:
Sub CopySub()
Dim wb As Workbook
UserForm1.Show
Set wb = Workbooks(ComboBox1.Value)
....Rest of Copy and Paste Code
Below is the code for the userform:
Private Sub OK_Click()
'Take user selection and continue copy and paste code
UserForm1.Hide
End Sub
Private Sub Cancel_Click()
'Cancel everything, end all code
End
End Sub
Private Sub UserForm_Activate()
'Populate list box with names of open workbooks.
Dim wb As Workbook
For Each wb In Workbooks
ComboBox1.AddItem wb.Name
Next wb
End Sub
Your code isn't working now because CopySub doesn't know what\where ComboBox1 is. Also, if the user clicks the form's X to close it instead of pressing the cancel button or clicks the OK button without selecting a workbook, CopySub will keep running.
There are a couple different ways to get the form information. The simplest with your current code is to properly reference ComboBox1 and add a simple test.
Sub CopySub()
Dim wb As Workbook
UserForm1.Show
If UserForm1.ComboBox1.Value = "" Then
Exit Sub
End If
Set wb = Workbooks(UserForm1.ComboBox1.Value)
' rest of code goes here
End Sub
Something else to think about though is ways to make your macro quicker and easier to run. If the only thing on your form is a Combobox for selecting the workbook and users will be starting the macro from a keyboard-shortcut or from the menu, consider having the macro ask if they want to run the macro on the active workbook. Clicking Yes to a question is a lot faster than having to click a dropdown box, select the workbook, and then click OK.
Sub CopySub()
Dim wb As Workbook
If MsgBox("Do you want to run the macro on '" & ActiveWorkbook.Name & "'?", vbQuestion + vbYesNo) = vbYes Then
Set wb = ActiveWorkbook
Else
UserForm1.Show
If UserForm1.ComboBox1.Value = "" Then
Exit Sub
End If
Set wb = Workbooks(UserForm1.ComboBox1.Value)
End If
' rest of code goes here
End Sub
After further searching I found the answer, and its the same as what mischab points out, I didn't create a global variable so there was no way for my userform to communicate with the subroutine. I solved this by declaring a variable with scope for the whole workbook as such:
Public wb1 As String
Sub CopySub()
Dim wbCAR As Workbook
UserForm1.Show
Set wbCAR = Workbooks(wb1)
....Rest of code
and by setting the userform code to such:
Private Sub OK_Click()
wb1 = ComboBox1.Value
UserForm1.Hide
End Sub
Private Sub Cancel_Click()
Unload Me
End
End Sub
Private Sub UserForm_Activate()
'Populate list box with names of open workbooks.
Dim wb As Workbook
For Each wb In Workbooks
ComboBox1.AddItem wb.Name
Next wb
End Sub