Macro to copy specific cells from one workbook to another

D

dwight.yorke

Hey community,

I really need help on this one, Iam a newbie to excel macros and got
stuck with this task. Basically we need a macro that can perfom the
following function;

1)Copy specific cells from workbook1 (e.g A3, D5, C4)
2)Open and existing workbook (workbook2) on the computer
3)Paste cells from workbook1 into the next avialble row in workbook2 in
a certain order. (i.e A3 first, D5 second, and C4last)

I hope the instructions were clear, and appreciate any help i could
get.

Thanks,
Dwight
 
D

dwight.yorke

Hey Ron thanks for the help but I am stilling running into problems. A
the moment I am able open a new document from a saved location on the
compter, which is great. I am able to copy one of the cells from work
book1 to workbook2, which is a good start. The problem is, the cell
from workbook one is not being pasted into the right cell, that I want
in workbook2. Also i cant seem to get the macro to do mutiple
copy/paste with out running into errors. Below is the code I have so
far.


Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function bIsBookOpen(ByRef szBookName As String) As Boolean
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


Function Lastcol(sh As Worksheet)
On Error Resume Next
Lastcol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function


Sub copy_to_another_workbook()
Dim sourceRange As Range
Dim destrange As Range
Dim destWB As Workbook
Dim Lr As Long


Application.ScreenUpdating = False
If bIsBookOpen("test.xls") Then
Set destWB = Workbooks("test.xls")
Else
Set destWB = Workbooks.Open("h:\test.xls")
End If
Lr = LastRow(destWB.Worksheets("Sheet1")) + 1
Set sourceRange = ThisWorkbook.Worksheets("Sheet1").Range("A1:A6")
Set destrange = destWB.Worksheets("Sheet1").Range("A" & Lr)
sourceRange.Copy
destrange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
destWB.Close True
Application.ScreenUpdating = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top