You are here:

Excel/Copying data from one excel worksheet to another

Advertisement


Question
Hi,

I have an excel workbook with several worksheets that I use to fill in as forms from my customers. Currently everyday I just delete the previous days work, but I want to instead of deleting it I want to copy it to another worksheet in the same workbook to keep the data for future referencing.
my worksheets are set up so I enter data in one column such as:
name
Address
phone number etc.

In the worksheet I want it to be copied to I would like the data to be put in one row so instead of C5:C10 it would be C4:J4.
I would like it to be done upon the click of a button which I know how to create and assign a macro to. I have other macros that run on the original work sheets but I do not believe that they will affect each other.

Regards and Thank you in advance.

M

Answer
Monique,

If I assume both workbooks are open and the code is in the workbook where you enter the data.  For the destination worksheet I assume the name
the sheet is named MyData
In the workbook where you write the data, I assume the worksheet has the name Form1
You can change these in the code.


Sub abc()
Dim shDest As Worksheet, shSource As Worksheet
Dim lastrow As Long, r As Range
Set shDest = ThisWorkbook.Worksheets("MyData")
Set shSource = ThisWorkbook.Worksheets("Form1")
Set r = shSource.Range("C5:C12")
r.Copy
lastrow = shDest.Cells(shDest.Rows.Count, "C").End(xlUp).Row + 1
shDest.Cells(lastrow, "C").PasteSpecial Transpose:=True
Application.CutCopyMode = False
End Sub



I used C5:C12  because you would need that many cells to write to column C:J on the destination sheet.  You can adjust the range back to C5:C10 if you wish.

If you want to clear the data that you just copied, then you would use (adds a line to clear the data)

Sub abc1()
Dim shDest As Worksheet, shSource As Worksheet
Dim lastrow As Long, r As Range
Set shDest = ThisWorkbook.Worksheets("MyData")
Set shSource = ThisWorkbook.Worksheets("Form1")
Set r = shSource.Range("C5:C12")
r.Copy
lastrow = shDest.Cells(shDest.Rows.Count, "C").End(xlUp).Row + 1
shDest.Cells(lastrow, "C").PasteSpecial Transpose:=True
Application.CutCopyMode = False
r.ClearContents
End Sub

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.