PC Review


Reply
Thread Tools Rate Thread

Copy data from one sheet to another

 
 
Slim Slender
Guest
Posts: n/a
 
      25th Feb 2010
I started out using the following code which I got from this site:

Public Sub CopyDatatoDatabase()
' by Dave Peterson
' The code depends on the last used cell in column A for both
ranges.

Dim rngToCopy As Range
Dim DestCell As Range
Dim wbk As Workbook

On Error Resume Next
Set wbk = Workbooks("somefile.xls")
On Error GoTo 0

If wbk Is Nothing Then
MsgBox "Opening the book now"
Set wbk = Workbooks.Open("somepath\somefile.xls")
End If

With ThisWorkbook.Worksheets("Data")
Set rngToCopy = .Range("A2:I" & .Cells(.Rows.Count,
"A").End(xlUp).Row)
End With

With Workbooks("somefile.xls").Worksheets("Database")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

rngToCopy.Copy Destination:=DestCell

And it worked fairly well except for a couple of things. The Source
wks has validation. During the pasting process I was prompted to
decide if I wanted to use this or that validation. The Destination
sheet ended up with validation. So, I tried to figure out how to paste
special values only. Also, I want to remove the data from the Source
sheet (effectively move it to the destination) but using rngToCopy.Cut
removed the validation and all formatting from the copied cells on the
source, so I tried .Copy and going back to Delete or Clear the source
sheet but every thing results in destroying the formatting.


Public Sub CopyDatatoDatabase()

Dim rngToCopy As Range
Dim DestCell As Range
Dim wbk1 As Workbook
Dim wbk2 As Workbook

Set wbk1 = ActiveWorkbook

On Error Resume Next
Set wbk2 = Workbooks("somefile.xls")
On Error GoTo 0

If wbk2 Is Nothing Then
Set wbk2 = Workbooks.Open(ActiveWorkbook.Path & "\somefile.xls")
End If

'Subscript out of range
With wbk1.Sheets("Data")
Set rngToCopy = .Range("A2:I" & .Cells(.Rows.Count,
"A").End(xlUp).Row)
End With

With wbk2.Sheets("Database")
If .FilterMode Then .ShowAllData
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
'rngToCopy.Cut Destination:=DestCell

rngToCopy.Copy

'Application Defined or Object Defined Error
DestCell.PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

Application.CutCopyMode = False

rngToCopy.Clear

End Sub

Sometimes this works perfectly and other times I get the errors
indicated in the comments. Can anyone help me smooth this out to work
consistently moving the values only and leaving the formatting and
validation behind.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy rows from one Data sheet to another sheet based on cell conte John McKeon Microsoft Excel Misc 2 15th May 2010 06:49 AM
Auto copy cell data from source sheet to another wrkbook sheet IVLUTA Microsoft Excel Programming 2 2nd Jun 2009 05:07 PM
copy data of two cells from Sheet 2 into one cell in Sheet 1 cahabbinga Microsoft Excel Worksheet Functions 6 30th Jan 2008 01:00 PM
How can i copy data from a tabbed working sheet to a summary sheet =?Utf-8?B?U3RlcGhlbkY=?= Microsoft Excel Misc 1 15th Mar 2007 03:40 PM
how to copy a cell with formula from sheet 1 (data is all vertical) into sheet 2 parag Microsoft Excel Worksheet Functions 3 15th Jun 2006 10:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:07 AM.