I was wondering if there was a way to run a VBA script when I open a sheet in the workbook.
For example, I have a workbook called "Inventory" and I want to run an "InitiateInventoryValues" Function when the "View Inventory" sheet is opened.
Can anybody please help me on this?
Double click the "Workbook" icon in VBE and use this event. It will trigger everytime you activate a different sheet by clicking its tab. If the tab is the one named "View Inventory", your code will run (once) when the sheet is activated:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "View Inventory" Then
'Do your code
End If
End Sub
Related
I created a spreadsheet in the past which contained buttons that would run macros in the workbook. When I open the spreadsheet now and press the button, the macro will run perfectly. However, on the Developer tab, there are no macros listed and no code available. Is there a way to make the macros visible?
I do not understand. Is that your problem?
Option Explicit
Option Base 1
' This Macro is shown.
Public Sub X()
MsgBox ("X")
End Sub
' This Macro is hidden.
Public Sub Y(ByVal N As Long)
MsgBox ("Y")
End Sub
I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub
I have a already existing excel file with some headers and single worksheet, I want that whenever a user click on add new worksheet button headers should be repeated on the it.Is it possible, if yes please tell be the steps to create one excel like this.
You could use a template, although that's a question for the superuser site.
As a macro answer try this:
Create a new worksheet with only the header on it, Rename this sheet Template.
Go into the Developer tab and insert an ActiveX button, double click it to go into the vba editor (you may need to save your document as a macro enabled document first)
Change the name of your button to btnNewWorkSheet and the caption to something like "Click here for a new worksheet"
Go into the sheets page in the VBA editor and copy this code:
Private Sub btnNewWorkSheet_Click()
Sheets("Template").Copy After:=ActiveSheet
End Sub
Now you can click on the button on any page where it appears to create a copy of the Template page
You can paste the following code to "ThisWorkbook" module:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If TypeName(Sh) = "Worksheet" Then
Sheet1.Range("A1:K1").Copy Sh.Range("A1")
End If
End Sub
Sheet1 is the name of the single worksheet object, replace A1:K1 with the range of your headers, replace A1 with the desired destination.
I'm working on an inherited spreadsheet (creation date 1998, or maybe earlier) and need to change the references on a dialog sheet. I can't see the macro in the run macro list, or anything similar, the sheet itself doesn't show up in the VBA project explorer window, which sounds like it could be an Excel 4.0 macro from other questions I've read here. A lot of people suggest rewriting an Excel 4.0 macro from the beginning, but the problem is that I don't actually know what the macro does.
Specifically, the macro is linked to a command button, and when I click on 'Assign macro', the name of the macro is written in the 'Macro name' text box, but it's not actually listed in the drop-down menu. If I try to assign this macro to another command button by typing the same name into the 'Macro name' text box, I get an error that the macro is unavailable.
(Screenshot link: http://imgur.com/HlxvLV2)
I tried using the solution here (Cannot see excel sheet in VBE), and because the sheet I'm interested in is not actually a worksheet, I declared ws as a dialog sheet.
Public Sub TestAccessToXL4MacroSheet()
Dim ws As DialogSheet
Set ws = ThisWorkbook.ActiveSheet ' succeeds
Debug.Print ws.Name ' outputs "Macro1"
Set ws = Worksheets("Macro1") ' fails: "Subscript out of range"
End Sub
The result is that the name of the sheet appears in the immediate window.
(Screenshot link: http://imgur.com/C1L2SQP)
Is this the intended result? I can already see the sheet name as a tab, I want to see the sheet itself in the VBA editor.
XL4 worksheets (your macro1 sheet for example) are not visible in VB IDE.
To view the sheet in Excel right click on one of the visible sheet tabs and from the menu select the command "Unhide...". You can select the macro1 sheet and click "OK".
Alternatively, try
Sub ShowAllSheets()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
sh.Visible = True
Next
End Sub
Then you will need to convert the XL4 code to VBA manually.
I have created a macro and a button but I can't see where to edit the button setting to invoke the macro. I'm using Excel 2003.
If you've used a Form Control. The chance to assign a macro will come up right after you add the button to the sheet. Or, if the button is already on the sheet, you can right-click on it and select Assign Macro.
If you've used an ActiveXControl, right-click on the button and select View Code. This will open up the VBE with
Private Sub myCommandButtonName_Click()
End Sub
From this sub call your macro like so...
Private Sub myCommandButtonName_Click()
Call myMacro
End Sub