I am trying to make another worksheet active when command button is clicked, but I'm staying within the same active workbook:
Sub Submit_Warranty()
'Set warranty data worksheet as active page
Sheets("Sheet2").Activate
'Show the submit warranty user form
Warranty_Input.Show
End Sub
I keep getting "subscript out of range" error. Any ideas?
If the code you posted is everything, then that error pretty much has to be from an invalid reference. So my guess would be that the actual displayed name is something like "Warranty_Data", while "Sheet2" is likely the VBA object name (maybe you're using them in reverse).
There are a lot of ways to select a worksheet, with various advantages and disadvantages. When it comes to selecting by name, the major gotcha to watch out for is that sheets actually have two names assigned, and you're employing both methods of selection in the code you posted. The one name is what's displayed in the sheet's workbook tab, the other name is internal to VBA. Here's a screenshot to demonstrate how to use both types of names.
Related
I have a workbook setup for users that have a few cells index/matched to a serperate workbook. The issue I am running into is these user books are sometimes opened on Mac, and sometimes PC, causing issues with the Index/Match formula. My first attempted solution was this:
ThisWorkbook.UpdateLink Name:=ThisWorkbook.LinkSources
However when this was assigned to a macro button, it causes a dialogue box to open for the user to select the source.
I instead have tried to set up two seperate macros; one for Mac and one for PC (Not unusual on this sheet as i've had to duplicate a fair bit for Mac/PC compatibility), and these macros paste the required formula directly to the cell. However, this still causes the choose source dialogue to open.
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Tracker")
ws.Range("J3").Formula = "=INDEX('[Workflow 19-20 LIVE.xlsm]PLAN'!$F:$F,MATCH(I3,'[Workflow 19-20 LIVE.xlsm]PLAN'!$A:$A,0))"
Is there any way I can avoid the dialogue box situation? I really don't want to give users any options here.
I want that whenever the workbook is closed a Sub from a sheet(sheet13) should be called, but as shown in the screenshot Im getting the error Invalid or unqualified reference.
By now I have tried
B2_Click
!Sheets("sheet13").B2_click
!Sheets("Employee Names").B2_click
Thisworkbook.Sheets("Employee Names").B2_click
And I am getting an error every time.
Your last example should work, so I would direct my troubleshooting towards the method you are calling. Try this in a new workbook:
In ThisWorkbook module
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Sheet13").Test
End Sub
And in Sheet13 module
Sub Test()
MsgBox "Closing"
End Sub
If that works, you have a starting point.
First off, the easy solution as already mentioned by others:
ThisWorkbook.Sheets("January").Something (string is case sensitive!)
There are multiple ways to reference a Sheet, and there are different types of names for worksheets. You have to differentiate between the "CodeName" and the "Name".
You will easily find more information on the difference between those two.
The main differences are, that the Name can be changed by the user, it is the name visible on the tab at the bottom of the Excel Interface.
The CodeName can only be changed through the VBA Interface.
The CodeName and the Name are usually identical when creating a worksheet (Sheet1, etc..).
To Reference a Sheet by Name you can use this code:
ThisWorkbook.Sheets("SomeName").SomeFunction()
The CodeName can be used directly like this: Sheet2.SomeFunction(), but I'd recommend changing the CodeName to something meaningful/expressive first!
(Name) is the CodeName, Name is the visible Name
More information on the Bang operator ! can be found here:
https://rubberduckvba.wordpress.com/2018/03/15/vba-trap-default-members/
Extensive Answer on referencing Sheets:
https://stackoverflow.com/a/41481428/10223558
Sub test()
Worksheets("Sheet1").Range("A1").Value = 20
End Sub
This simple code is giving error when I compile it.
activesheet. works fine.
I want to know whats resulting in an error and how to fix it...
looks like it's not identifying the sheets, workbook etc.
The answer depends on which error you get. There can be 2 issues:
1. Workbook not specified
You have more than one workbook and Excel is looking in the wrong workbook for your sheet named "Sheet1", then you need to specify the workbook.
Workbooks("my-workbook").Worksheets("Sheet1").Range("A1").Value = 20
or if it is in the workbook where the code is running at it is better to use
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = 20
Note that you should avoid ActiveWorkbook which is not very reliable.
2. Wrong worksheet name
There is no worksheet named Sheet1. Check your worksheet names. Note that there are different ways to specify a worksheet.
Specify by number
Worksheets(1).Range("A1")
This uses the position of the worksheet in the tab bar below the worksheets. Note that is not very reliable because position can easily be changed by moving the tabs around.
Specify by tab name
Worksheets("Sheet1").Range("A1")
This is probably the most common method. The worksheet is specified by its tab name. This is more reliable than by number.
Specify by VBA name
Sheet1.Range("A1")
Here the VBA name of the sheet is used. This name can only be changed in the VB editor and is not visible to the user, and has nothing to do with the tab name. Using this ensures that the VBA code still works on the desired worksheet even if a user changes the tab name of the worksheet.
So if the tab name is Sheet1 its VBA name can be Sheet5 and it can be on position 3 in the tab bar.
Using this example …
Worksheets("Sheet1").Range("A1")
Sheet5.Range("A1")
Worksheets(3).Range("A1")
… are all 3 accessing the exact same worksheet just by different names. So better to use meaningful names (and no numbers) here to not confuse.
I am trying to print the active worksheet and another worksheet with general information (in the same workbook) at the same time. (Recto verso, which my printer does automatically, no code needed for that)
In my workbook I have multiple sheets that use the same code for printing. Now I would like to add the sheet with general information called "Huurvoorwaarden" to an array so it is printed automaticaly and at the back side of the active sheet.
I have tried multiple sollutions like dim / set activesheet.name, and codes which I have found on the web. nothing works.
I know that when I would change "activesheet" to Sheet1, that would work, but only for sheet 1.
Could you please help me?
Here is what I have got: (all my older attempts are deleted)
'Print Active Sheet and sheet Huurvoorwaarden
Worksheets(Array("activesheet.name", "Huurvoorwaarden")).PrintOut
The name of the active sheet is not the string literal "ActiveSheet.Name", it is the property ActiveSheet.Name.
So you need to use
Worksheets(Array(Activesheet.Name, "Huurvoorwaarden")).PrintOut
I have a workbook (we'll call it "AAA") that is based on a template and refers to code modules in a second workbook. The second workbook (we'll call it "CodeStorage") is a repository for code modules so that any saved versions of the template will be able to access modified/updates code without a change to the saved workbook.
My problem arises when I have multiple windows open in "AAA" and try to get the activesheet when a module is running in "CodeStorage". In code, I create an object ("oWorkbook") that is a reference to the workbook "AAA" When "AAA" has focus, oWorkbook.Activesheet returns the sheet for the active window. When "CodeStorage" is running a module and thus has focus, oWorkbook.Activesheet returns the sheet that is selected in window #1 regardless of what window (#2, 3, etc) was active when the code module in "CodeStorage" was called.
Has anyone run into this and have you found a work around?
ActiveSheet is confusing the way you are using it.
You need to explicitly activate a sheet for it to be considered the ActiveSheet. Running code in another workbook does not activate it.
Selecting cells in a worksheet will activate it. Or specifically calling Activate.
You could do something like:
oWorkbook.Activate
oWorkbook.Activesheet
Alternatively, and preferably, you could do something like the following:
oWorkbook.Worksheets("Sheet1")
oWorkbook.Worksheets(1)
Both these are better. If your user selects a different workbook during runtime execution or you select something in a different sheet, ActiveSheet will return something different.
It's better to fully qualify you workbook paths when using multiple workbooks. This will save you a ton of headache in the future for dealing with "what is activated?" or "what is selected?" This answer is worth reading, too.
To rephrase my question, I was looking for a way to reference the last active worksheet in a workbook that had multiple windows open. If window 1 was selected, I wanted the sheet from window 1, same for window 2, 3, or ???. I also needed the reference when a code module in a different workbook was running and the code module had a variable that was an object reference to the calling workbook.
The solution is Workbook.Windows.Item(1).ActiveSheetView.Sheet. When called from the running code module, even in a different workbook, it returns the same as workbook.activesheet when that is called from the workbook itself. My tests show that Workbook.Windows.Item(1) is the last active window when a workbook loses focus.
Thank you enderland. You got me pointed in the right direction.