Move Excel Sheet from one Workbook to another - excel

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))

Related

Create kind of temporary workbook from active workbook and then save temp. wb without changes in act. wb.. Excel VBA

Hello I have a workbook from which I need 2 sheets. The sheet 1 is needed to be copy pasted as values (to eliminate formulas and preserving formatting). MY logic is following :1) create a kind of temporary workbook of the initial file 2) then do copy paste as values 3)select only sheet 1 and 2 4) save this temporary workbook (with two sheets) to the folder. This is done in order to avoid any changes in the active (original ) workbook. I am using the following code and it seems to work. However my initial active workbook is also changed (in the first sheet only values (no formulas)). What should I change in my code , so that my schema starts working.
Set tb = ActiveWorkbook
tb.Sheets(1).Cells.Copy
tb.Sheets(1).Range("A1").PasteSpecial Paste:=xlValues
tb.Sheets(Array("Sheet1", "Sheet2")).Select
tb.Sheets(Array("Sheet1", "Sheet2")).Copy
tb.SaveAs Filename:="C:path"

VBA code to update links in another workbook

Can someone help me on this? I have an excel file where I want to write a code that picks a value from a specific cell from the same workbook and paste it into another workbook. I am not sure how can I connect two workbooks and update the value.
You can also use queries for this.
If you specifically want to do this with VBA, you can open another workbook using:
Dim Tr As Workbook
Set Tr = Workbooks.Open(TFileName)
where TFileName stands for the full path of your second workbook. If you set your second workbook you can copy paste anything you want.
You can also use this:
Tr.Windows(1).Visible = False
so the workbook stays in the background

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

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

Resources