I'm trying to figure out how to copy data from one excel workbook to another. The end goal is to automate creation of a sign in sheet — I have information on students in one workbook, and I want to create a new workbook that will have the relevant columns in a different order. I've tried to figure out how to use various methods to copy from one sheet to another but I keep ending up with error messages. Here is the code I'm using:
Option Explicit
Sub signinsheet()
Dim newbook As Workbook
Dim ldate As Date
Dim center As Variant
Dim title As Variant
Dim original As Variant
Dim pastebook As Workbook
Dim students As Integer
Dim wbTarget As Workbook 'workbook where the data is to be pasted
Dim wbThis As Workbook 'workbook from where the data is to copied
original = ActiveWorkbook.Name
center = InputBox("What is the center name?")
students = InputBox("How many students?")
ldate = Date
Set pastebook = Workbooks.Add
With pastebook
.title = center & ldate
.Subject = "signup sheet"
.SaveAs Filename:=center & ldate
End With
title = center & ldate
Workbooks(original).Activate
Workbooks.Open (title)
Workbooks(title).ActiveSheet.Range("F5", Range("F5").Offset(students, 0)).Value = Workbooks(original).Sheets("Sheet1").Range("B2", Range("B2").Offset(students, 0)).Value
'set to the current active workbook (the source book)
Set wbThis = ActiveWorkbook
Set wbTarget = Workbooks.Open(title)
wbTarget.Sheets("sheet1").Range("F5").Select
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'copy the range from source book
wbThis.Sheets("sheet1").Range("B2", Range("B2").Offset(students, 0)).Copy
'paste the data on the target book
wbTarget.Sheets("sheet1").Range("F5").PasteSpecial
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
wbTarget.Save
'close the workbook
wbTarget.Close
'activate the source book again
wbThis.Activate
'set to the current active workbook (the source book)
Set wbThis = ActiveWorkbook
'open a workbook that has same name as the sheet name
Set wbTarget = Workbooks.Open(title)
wbTarget.Sheets("sheet1").Range("A5").Select
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'copy the range from source book
wbThis.Sheets("sheet1").Range("C2", Range("C2").Offset(students, 0)).Copy
'paste the data on the target book
wbTarget.Sheets("sheet1").Range("F5").PasteSpecial
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
wbTarget.Save
'close the workbook
wbTarget.Close
'activate the source book again
wbThis.Activate
'set to the current active workbook (the source book)
Set wbThis = ActiveWorkbook
'open a workbook that has same name as the sheet name
Set wbTarget = Workbooks.Open(title)
wbTarget.Sheets("sheet1").Range("B5").Select
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'copy the range from source book
wbThis.Sheets("sheet1").Range("D2", Range("D2").Offset(students, 0)).Copy
'paste the data on the target book
wbTarget.Sheets("sheet1").Range("F5").PasteSpecial
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
wbTarget.Save
'close the workbook
wbTarget.Close
'activate the source book again
wbThis.Activate
End Sub
Basically I want to copy B2 to the number of students from the original workbook to F5 on down on the new workbook. I also want C2 on down to go to A5, and I want D2 on down to go to B5. It'll work occasionally on the first copying over, but after that it breaks down and stops working. (I'm using a mac, and right now referring to workbooks as workbook(original) or workbook(title) works for me. Title is the name of the new workbook. I have some other formatting I'm doing on the sign in sheet but I've removed it from the code to maintain confidentiality (and because it's working).