Hi, I found the below code online. My goal is to copy data from from one workbook to another without specifying the specific filenames. In this code, it seems I have to have my cursor on the target sheet when running the macro, otherwise it copies the data from the target onto itself. Is there a better way to set up the source and target method?
I also need to change it to "paste values" but I don't know how to modify.
Any help is appreciated. Thanks
I also need to change it to "paste values" but I don't know how to modify.
Any help is appreciated. Thanks
VBA Code:
Sub copybook2()
Dim sourceBook As Workbook
Dim targetBook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
' Must have exactly 2 workbooks open
If Workbooks.Count <> 2 Then
MsgBox "There must be exactly 2 workbooks open to run the macro", vbCritical + vbOKOnly, "Copy Columns From Source To Target"
Exit Sub
End If
' Set the source and target workbooks
Set targetBook = ActiveWorkbook
If Workbooks(1).Name = targetBook.Name Then
Set sourceBook = Workbooks(2)
Else
Set sourceBook = Workbooks(1)
End If
' Set up the sheets
Set sourceSheet = sourceBook.ActiveSheet
Set targetSheet = targetBook.ActiveSheet
' Copy the columns
sourceSheet.Range("C24:I28").Copy Destination:=targetSheet.Range("C24")
End Sub