Use the 'offset' method to reference a cell in another worksheet - excel

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.

Related

Read One Cell and Copy to Another Only Upon Opening Spreadsheet

I need to copy the data in cell c3 to cell i11 when I open my spreadsheet. I do not want i11 to change after this action until I reopen the spreadsheet.
You can set the code in the ThisWorkbook part of your projects. Select Workbook and Open or use the following code.
The working formula is the value you see me do below, but this can be achieved a number of way such as using Cells(3,3).Value. Additionally the way the sheet is referenced can vary. I put both the Activesheet reference and a explicit sheet reference, depending how your sheet is structured you may have to use one over the other, but choose one below and see how your sheet handles it and if adjustments are needed.
Private Sub Workbook_Open()
'Use one of these depending on how your sheet opens
ActiveSheet.Range("I11").Value = ActiveSheet.Range("C3").Value
Worksheet("YourWorksheetHere").Range("I11").Value = Worksheet("YourWorksheetHere").Range("C3").Value
End Sub

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.

Force Excel Formula to Reference Current Workbook

I have a simple formula reference that I use in my workbook, however it gets complicated when I use another function that instantly opens my default worksheet and copies it over to my active workbook.
The problem is that the cells in this workbook reference another sheet in my default workbook. The sheet in that and all the other workbooks I am working on has the same name. It's "Form"
When I use my code to copy the sheet over, the cell automatically changes it's reference to include the previous workbook.
I want the formula to ALWAYS USE THE CURRENT WORKBOOK.
Here is what I use
=Form!B6
Here is what I end up getting when i drop the sheet
="filepath"Form!B6
Here is a way to copy a formula from one workbook to another with no changes:
Sub ytrewq()
Dim s As String
s = Workbooks("book2.xlsm").Sheets("Sheet1").Range("G8").Formula
Workbooks("temp.xlsm").Sheets("Sheet1").Range("H1").Formula = s
End Sub
The trick is to use INDIRECT(). For example:
=INDIRECT("Form!B6")

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

Microsoft Excel: Programmatically create a hidden named range

I am told that the Excel object model permits a Range that is not a part of any sheet, yet contains a set of cells and is denoted by a name in the workbook.
Can anyone explain to me how these fit into the Excel object model and how one would go about creating such a thing programatically (either in VBA or .NET source code).
Thanks.
Your question is a little vague, but I'll give it a shot.
Well, as Dave describes, you can give a specific range of cells on a sheet a "Range Name" which you can then refer to programatically, but that doesn't sound like what you are asking.
It sounds like you are asking "is there an abstract RANGE of cells available to be used by VBA code that doesn't literally exist on any worksheet?" The answer to this is no, even named ranges are simply a convenient reference to a real set of cells on a real worksheet.
You can, however, programatically hide a worksheet so that the user doesn't see it, and still work with cells and ranges on that sheet. Just do:
Sheets("Sheet1").Visible = xlSheetHidden
Sheets("Sheet2").Visible = xlSheetVeryHidden
Sheets("Sheet3").Visible = xlSheetVisible
What's "VeryHidden", you ask?
It means that the user can't go to Format, Sheet, Unhide and make the sheet visible.
So if I'm correctly understanding what you want, just programatically hide one of the sheets, then use Dave's technique to create a named reference to a range on this hidden (or VeryHidden) sheet.
That would be a named range. You can reference a selection of cells, and just type a name where it says 'A1' next to the formula bar. That creates a named range that doesn't change.
Alternatively you can create a named range that is based on a formula, and therefore potentially changes as data in the spreadsheet changes. You do this from the 'Define Name' option (which is in the Formulas Ribbon in Office 2010).
Named ranges can be accessed from VBA, and (I'm pretty sure) from .net.
So you'd access the named range from vba like so:
Range["MyNamedRange"]
Yes, there is a NamedRange control in the Microsoft.Office.Tools.Excel namespace in VSTO. This is a host control which is different from the native Range control in the Microsoft.Office.Interop.Excel.Range namespace.

Resources