Copy & Paste to closed workbook in another folder

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
I have a little issue if you could help I copy and paste from one workbook to another with the code below which works fine as long as both workbooks are in the same folder and both open, what I would like to do is have the PriceSheet workbook in another folder and be able paste its data in to the closed DataBase workbook which is password protected so I would need it to unprotect then protect again.

Sub Clear_Existing_Data_Before_Paste()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Set wsCopy = Workbooks("PriceSheet.xls").Worksheets("ALL")
Set wsDest = Workbooks("DataBase.xls").Worksheets("ALL")

'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row

'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

'3. Clear contents of existing data range
wsDest.Range("A2:S" & lDestLastRow).ClearContents
'4. Copy & Paste Data
wsCopy.Range("A2:S" & lCopyLastRow).Copy _
wsDest.Range("A2")
End Sub

Regards
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,
untested but try this update to your code & see if does what you want

Rich (BB code):
Sub Clear_Existing_Data_Before_Paste()
    Dim wsCopy As Worksheet
    Dim wbDest As Workbook
    Dim lCopyLastRow As Long, lDestLastRow As Long
    Dim FileName As String, OpenPassword As String, wsPassword As String

'******************************************************************************************************
'********************************************SETTINGS**************************************************
'REQUIRED - specify the PATH & File Name
    FileName = "C:\MyDocuments\MyDatabase\Database.xlsx"
    
'OPTIONAL - enter password to OPEN workbook
    OpenPassword = ""
'OPTIONAL - enter password to UNPROTECT database worksheet
    wsPassword = ""
    
'*****************************************************************************************************
    
    On Error GoTo myerror
    Set wsCopy = ThisWorkbook.Worksheets("ALL")
'1. Find last used row in the copy range based on data in column A
    lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
    
    Application.ScreenUpdating = False
    Set wbDest = Workbooks.Open(FileName, UpdateLinks:=False, ReadOnly:=False, Password:=OpenPassword)
    
    With wbDest.Worksheets("ALL")
        .Unprotect wsPassword
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
        lDestLastRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Clear contents of existing data range
        .Range("A2:S" & lDestLastRow).ClearContents
'4. Copy & Paste Data
        wsCopy.Range("A2:S" & lCopyLastRow).Copy .Range("A2")
'protect sheet (optional)
       If Len(wsPassword) > 0 Then .Protect wsPassword
    End With
    
'5. Close & Save changes
    wbDest.Close True
    Set wbDest = Nothing
    
    MsgBox "Database Updated", 64, "Updated"
    
'if things go wrong, inform user
myerror:
'ensure workbook is closed
    If Not wbDest Is Nothing Then wbDest.Close False
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Ensure that you change the values in settings section as required

Dave
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top