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")
Related
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
I have been trying to figure out how to move (or copy) an Excel sheet from one Workbook to another using Microsoft.Office.Interop.Excel in vb.NET. It seems like Sheets.Move(Before, After) is limited to moving a Sheet to another location within the same Workbook, by using the sheet index as the destination location (where Sheet1 = 1, Sheet2 = 2, and so on).
For example, the following will move Sheet1 to before Sheet3:
Sheets(1).Move(3)
Is there a way I can move (or copy) Sheet1 to another Workbook (that already exists)? I can't figure it out.
This looks like a duplicate from…
C# - How to copy a single Excel worksheet from one workbook to another?
I am aware that this is a C# question, however, the answer provided by NickZucco is a VB solution and appears to work as expected.
In my tests, the following code “moves” a worksheet from SourceWorkbook to DestinationWorkbook…
SourceWorkbook.Worksheets(wsheetToMove).Move(After:=DestinationWorkbook.Worksheets(1))
To copy the worksheet…
SourceWorkbook.Worksheets(wsheetToCopy).Copy(After:=DestinationWorkbook.Worksheets(1))
How can I select worksheet in excel using vba even when sheet name and sheet position changed constantly?
I was using sheet index to avoid vba fail to select worksheet after changing the worksheet name, however, if I change relative position of the ws, its index changes.
I noticed that sheet name in vba project browser, there is a item called (name) which is a fixed sheet number (I guess it was named by the sequence that I created the ws.) Is it possible to select ws based on this fixed name?
Thank you
You can use Name in VBE to specify your name for Worksheet. This will allow you to rename your Worksheet in Excel according to your needs.
In VBA you can then use something like:
Option Explicit
Sub Test()
MyWorksheetName.Activate
End Sub
This is where you edit your Name:
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.
I have a named range called "cellabove" in my workbook that always references the cell above the cell it is used in. The RefersToR1C1 property of the name is "=!R[-1]C". This allows it to be applied on any worksheet in the workbook.
When I run a macro that deletes some rows then some cells containing cellabove in their formulas are not recalculating, even though I end the macro with application.calculatefullrebuild. However when I then manually force a full rebuild by pressing CTRL+SHIFT+ALT+F9 the cells do recalculate.
Any idea of how I can force the relative referenced named range to refresh?
Using refersto that start with ! in Defined Name formulas is IMHO not advisable as there are long-standing bugs.
You could use =INDIRECT("R[-1]C",FALSE) instead
Try adding one of these calculation options into your code to see if they have an effect.
Sub Calculate_It()
ActiveSheet.Calculate
''// Equivalent to Shift+F9 Recalculates the active worksheet
Application.Calculate
''// Equivalent to F9 Recalculates all worksheets in all open workbooks
Application.CalculateFull
''// Equivalent to Ctrl+Alt+F9 Recalculates all worksheets in all open workbooks (Full recalculation)
Application.CalculateFullRebuild
''// Equivalent to Shift+Ctrl+Alt+F9 Rebuilds the dependency tree and does a full recalculation
End Sub