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.
Related
I'm working on a routine that will populate a worksheet from data on a second worksheet in the same active workbook. The location on the destination worksheet is relative to a given cell which is the active cell on the relevant worksheet. In order to avoid continually swapping between active sheets, I was hoping that I could reference the destination cell using the 'offset' method, however I can't get it to work. My code line would be something like this:
Worksheets("DestinationSheet").activecell.offset(Rowoffset:=x, ColumnOffset:=y).Value=DataValue
Where x, y, and Datavalue are variables.
How about
Worksheets("DestinationSheet").range(activecell.address).offset(Rowoffset:=x, ColumnOffset:=y).Value=DataValue
?
The activecell is only a single cell on the active sheet so cannot be located on another sheet (and that sheet must be active when the macro is run). Btw it's not a good idea to base code on the activecell if you can avoid it.
That said, I'm not sure I understand what you are doing.
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
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.
In order to navigate through complex spreadsheets that I'm asked to analyse I need a list of all current regions in a worksheet. Excel help does not give me many clues. My solution so far is to loop over areas using the special cells function, but it is rather slow.
Function list_all_current_regions(work_sheet)
Dim current_region_dic As New Dictionary
Set r = work_sheet.Cells(1, 1)
For Each x In Array(xlCellTypeConstants, xlCellTypeFormulas)
Set c = work_sheet.Cells(1, 1).SpecialCells(x, 23)
For Each a In c.Areas
If Not current_region_dic.Exists(a.CurrentRegion.Address) Then
current_region_dic.Add a.CurrentRegion.Address, ""
End If
Next
Next
Set list_all_current_regions = current_region_dic
End Function
Is there a smarter way to list all the current regions in a worksheet?
Over the years I've avoided having multiple ranges on Worksheets for the very reason you are asking about, and I move disconnected content onto its own Worksheet when a client gives me a model designed as a single "ubersheet".
The only way around this I've found is to use Named Ranges. They are accessed using the Names collection attached to the Workbook and Worksheet objects.
One other tip I'll give is to just used the Named Range on the top left cell only then you can use the CurrentRegion property to grab the entire range. This helps when you have expanding regions that you don't want to go in to set and reset the entire range.
https://msdn.microsoft.com/en-us/library/office/ff196678.aspx?f=255&MSPPError=-2147217396
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