Copying a worksheet to another workbook using VBA - vba

I am trying to copy a worksheet from the workbook that the VBA is in (ThisWorkbook) and past it into another workbook that the user has open (ActiveWorkbook). I have written a function but I cannot get it to work. I have done something similar before and I have searched the internet but I cannot find a solution or why it is failing me. What am I doing wrong?
Function workBooks() As String
aWbkName = ActiveWorkbook.Name
tWbkName = ThisWorkbook.Name
Dim wbk1 As Workbook
Dim wbk2 As Workbook
Set wbk1 = tWbkName
Set wbk2 = aWbkName
wbk1.Sheet2.Copy After:=wbk2.Sheets(7)
End Function

Try this and see if it works. It will copy Sheet2 in ThisWorkbook and paste it after Sheet1 in the ActiveWorkbook
Option Explicit
Public Sub copy_sheet()
Dim source_worksheet As Worksheet
Set source_worksheet = ThisWorkbook.Worksheets("Sheet2")
Dim target_worksheet As Worksheet
Set target_worksheet = ActiveWorkbook.Worksheets("Sheet1")
source_worksheet.Copy After:=target_worksheet
End Sub

you don't need all that variable dimming and assigning:
Sub workBooks()
ThisWorkbook.Sheet2.Copy After:=ActiveWorkbook.Sheets(7)
End Sub

Related

Excel - Copy between two workbooks in VBA

I have two workbooks in excel. I am trying to copy a worksheet from one workbook to another.
And after that I want to close the workbook where I had copied from.
What I have done so far:
Sub copy()
Workbooks.Open filename:= _
"C:\2016.xlsm"
ActiveWorkbook.Sheets("Grafic").Select
Selection.Copy Destination:=Workbooks("C:\Grafic.xlsx").Sheets("Sheet1").Range("A1")
End Sub
Thanks.
Maybe this helps
Option Explicit
Sub CopyIt()
Dim wb As Workbook
Dim copyWb As Workbook
Dim wks As Worksheet
Dim fileName As String, sheetName As String
fileName = "... complete filename ..."
sheetName = "... sheet name ..."
Set wb = Workbooks.Open(fileName:=fileName)
Set wks = wb.Sheets(sheetName)
Set copyWb = ThisWorkbook ' the workbook you would like to copy to
wks.copy before:=copyWb.Sheets(1)
wb.Close False
End Sub
Use
Application.Workbooks("2016.xlsm").Close
Close method has some parameters to set if you want to save changes or not.
More info:
Workbook.Close

How to move a worksheet from one workbook to another in VBA

I have tried going through what other people have asked but I cannot figure out for the life of me why my code is not working. This is what I have:
Sub Move_Sheets()
Dim PTrend As Worksheet
Dim Strend As Worksheet
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks("Workbook1.xlsb")
Set wb2 = Workbooks("Workbook2.xlsb")
Set PTrend = wb2.Worksheets("Sheet1")
Set Strend = wb2.Worksheets("Sheet2")
With wb2
.Sheets(Array(PTrend, Strend)).Copy Before:=wb1.Sheets(7)
End With
End Sub
I am trying to move sheets from workbook 2 to workbook 1. My error occurs with my "with statement". Why is my code not working?
As always, thanks for all your guys help.
G
.Sheets is expecting sheet names while you are passing sheet object.
Try it like this...
With wb2
.Sheets(Array(PTrend.Name, Strend.Name)).Copy Before:=wb1.Sheets(7)
End With

How to refer to a Excel Worksheet by its VBA Object Name in another Workbook?

I have two Excel Workbooks:
Source.xlsx
Tool.xlsm
Source.xlsx contains a Worksheet with the VBA Object Name shtTests:
Let's assume that in Tool.xlsm I have a variable that contains a reference to the Workbook stored in Source.xlsx:
Dim wkbSource as Workbook
Set wkbSource = GetSourceWorkbook() ' Some function that gives a reference to the workbook
Core Question: How can I reference shtTests within Tool.xlsm by using shtTests' VBA Name?
Or to formulate the question as code... assume you have this code snippet:
Dim wkbSourceShtTests as Worksheet
Set wkbSourceShtTests = GetShtTestsFromWkbSources(wkbSources)
Question: What does GetShtTestsFromWkbSources have to look like?
Note: I do not want to reference it by its Excel Name like you would do using wkbSources.Worksheets("Test Cloning") because people might change its Excel Name some day.
Is this what you are trying?
Sub Sample()
Dim wbThis As Workbook, wbThat As Workbook
Dim wsThat As Worksheet
Dim wsCodeName As String
Set wbThis = ThisWorkbook
Set wbThat = Workbooks("Book4") '<~~ Change this to relevant workbook
wsCodeName = "ShtSheets"
Set wsThat = wbThat.Worksheets(CStr(wbThat.VBProject.VBComponents(wsCodeName).Properties(7)))
Debug.Print wsThat.Name
End Sub
Note: For this to work, you need to enable access to Visual Basic Projects
On the File menu Excel, Click Options|Trust Center|Trust Center Settings|Macro Settings, Check the box "Trust Access to the VBA Project Object Model"
If you wanted a function instead of setting up the trusted access then this would probably work:
Sub example()
Dim wkbSource As Workbook
Set wkbSource = GetSourceWorkbook()
Dim wkbSourceShtTests As Worksheet
Set wkbSourceShtTests = GetShtTestsFromWkbSources(wkbSource, "shtTests")
End Sub
Function GetShtTestsFromWkbSources(wkbk As Workbook, codename As String) As Worksheet
For Each sht In wkbk.Sheets
If sht.codename = codename Then Set GetShtTestsFromWkbSources = sht
Next
End Function

Copy worksheet over to another workbook

I would like to export a worksheet from Workbook A to another Workbook B which I need Excel to prompt me to choose. I am getting an error "Type Mismatch". Alternatively, I can export to an entirely new Workbook as well.
Sub savefile()
Worksheets("Test").Activate
Dim wb As Workbook
Dim filter As String
Dim linkf As Variant
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
caption = "Please Select an output file "
linkf = Application.GetOpenFilename(filter, , caption)
If linkf = False Then Exit Sub
Set wb = Workbooks.Open(linkf)
targetWorkbook.Sheets("Test").Copy After:=Workbooks(wb).Sheets("Sample")
End Sub
Replace the following line
targetWorkbook.Sheets("Test").Copy After:=Workbooks(wb).Sheets("Sample")
to
targetWorkbook.Sheets("Test").Copy After:=wb.Sheets("Sample")
Replace your last line with this:
targetWorkbook.Sheets("Test").Copy After:=wb.Sheets("Sample")
Make sure that you have a worksheet called "Sample" in the second workbook

Copy sheet from one workbook to another workbook

I need to take (copy) sheet from one workbook and append it to the end of existing excel document. I wrote this code but it doesnt work and gives an error. Please help =)
Public wb As Workbook 'workbook with the source sheet
Sub test()
dim wbNew as workbook 'destination workbook
Set wbNew = Workbooks.Open(Me.fileDestPathTextBox.Value)
wbNew.Sheets.Add After:=wbNew.Sheets(wbNew.Sheets.Count).Name = Me.sheetNameTextBox.Value
wb.Sheets("Souce Sheet").Copy wbNew.Sheets(Me.sheetNameTextBox.Value)
End Sub
You don't add a sheet and then try and copy your sheet to that sheet - you just copy the original sheet to the target workbook:
Sub test()
dim wbNew as workbook 'destination workbook
Set wbNew = Workbooks.Open(Me.fileDestPathTextBox.Value)
wb.Sheets("Souce Sheet").Copy After:=wbNew.Sheets(wbNew.Sheets.Count)
End Sub