Select multiple defined sheets - excel

This is a very basic question but I just can't get it right.
I have a workbook with multiple sheets, but for this code I only need two specific ones, which the user selects. So the code starts like this:
Dim SheetA as Worksheet
Dim SheetB as Worksheet
Set SheetA = ThisWorkbook.Sheets("The sheet the user wants")
Set SheetB = ThisWorkbook.Sheets("The sheet the user wants")
Then there is some code. Later I need to select these two sheets and I want the first sheet to be activated.
This is the code I've tried so far, but it does not work (at least the first line).
ThisWorkbook.Sheets(Array(SheetA, SheetB)).Select
SheetA.Activate
Thank you for any helpful advise.

You are close. However, the Sheets-collection needs numbers or strings as parameter, but in your code you are passing Worksheet-objects. The fix is easy: Just pass the names of the worksheets:
ThisWorkbook.Sheets(Array(SheetA.name, SheetB.name).Select

Related

Need to paste Input on multiple sheets Excel, vba

Problem with referencing back to sheet2I have a workbook with 37 sheets. Each sheet requires the user's name.
I have a inputbox for them to put in their name, however it only posts on the active sheet. I have tried referencing back to the cell it fills from the macro but it wont reference.
Here is the code
Sub Name()
Set wsDst = ThisWorkbook.ActiveSheet
Sheets(2).Range("B16").Value = inputbox("Please type your first name and last intial.", "Name")
End Sub
Now if I go to Sheet3 and try ='Sheet2!B16
that is exactly what shows in cell B16.
The main issue is that the user's name will not always be in B16
Sheet4 is B18.
I know this is probably an easy fix for most, however I am still learning all the syntax's and honestly have used codes others have posted and been able to edit them to my needs.

Get Named Range Values

I have two workbooks that have a lot of the same named ranges and I need to move values from the second workbook to the first. I can do this by specifying which sheet the range is on and the name of range, but there are about 200 named ranges spread out over 20-something sheets and I only need the named ranges that are on 11 of those sheets. I could take the time to figure it out and hard code moving each one, but would really like it to be a mostly automated process. That way if a new range is added it can just be done automatically and I don't have to update the code.
The code I have so far is:
For Each Name In sheetList
Set sheet2vals = sheet2.Sheets(Name).Range(rangename)
Set sheet1vals = sheet1.Sheets(Name).Range(rangename)
sheet2vals.Copy sheet1vals
Next Name
sheetList is an array of sheet names where all the ranges are stored. That part works exactly as I need it, I just need to find a way to pull the ranges from each of the sheets and move them. However, the only way I can seem to find to get the list of named ranges is by pulling at the Workbook level, which doesn't tell me what sheet it is on.
Each Name has a RefersToRange property that gives you a Range object, and every Range has a Parent property that gives you the Worksheet it belongs to.
So if you have a Name object, you can always know what sheet it's referring to.

Invalid use of property Excel VBA

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.

declare activesheet to array for printing 2 sheets of workbook

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

Referencing sheet in excel

I am new to excel vba and I have some questions regarding referencing a worksheet
I noticed that when I used
Worksheets(3)
The worksheet would be obtained according to the sequence of the worksheet in the workbook
When I used
Worksheets("Name")
It would be retrieved according to the name of the worksheet
However, I found that both approach is troublesome because for method 1, I need to fix the sequence of the worksheet. Once I dragged the worksheet around, the reference would become incorrect.
Method 2 would need me to fix the work sheet name , which is not that flexible.
I noticed that at the left panel of VBA editor, under the Microsoft Excel Objects, whenever the worksheet is created, a new sheet like
Sheet1 (Name) would be created.
Is there any way that I could reference the worksheet based the the Sheet1 variable, which I could fixed it so that I could freely drag the sheet around or change the worksheet name?
Thanks.
The name you refer to is called the CodeName. You can refer to a sheet by this name.
Eg, for your example Sheet1 (Name) can be referenced as
Worksheets("name")
or
Sheet1
Eg Worksheets("name").Activate or Sheet1.Active would both work
Note that you can change this name to something meaningful in the Properties window of the VBA IDE, but you can't change it at run time

Resources