Post from close book to an open

jmoney30

New Member
Joined
Dec 19, 2020
Messages
31
Office Version
  1. 2016
I'm trying to use this code but it will not paste the information. It opens the close book and selects but doesn't paste.

Sub GetDataClosedBook3()





Dim sourceworkbook As Workbook

'location of the file and data to copy

Dim currentworkbook As Workbook



Set currentworkbook = ThisWorkbook

Set sourceworkbook = Workbooks.Open("C:\Users\June.xlsx")

sourceworkbook.Sheets("Sheet2").Range("A2:C11").Copy



currentworkbook.Activate

currentworkbook.Worksheets("Sheet1").Activate

currentworkbook.Worksheets("Sheet1").Cells(4, 1).Select

ActiveSheet.Paste



sourceworkbook.Close

Set sourceworkbook = Nothing

Set currentworkbook = Nothing



ThisWorkbook.Activate

Worksheets("Sheet1").Activate

Worksheets("Sheet1").Range("A1").Select





End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,
welcome to forum

Try this update to your code & see if does what you want

VBA Code:
Sub GetDataClosedBook3()
    Dim SourceWorkbook As Workbook
    Dim PasteRange  As Range
    Dim FileName    As String
    
    FileName = "C:\Users\June.xlsx"
    
    If Dir(FileName, vbDirectory) = vbNullString Then
        MsgBox FileName & Chr(10) & "File Not Found", 48, "Not Found"
    Else
        Application.ScreenUpdating = False
        Set PasteRange = ThisWorkbook.Worksheets("Sheet1").Cells(4, 1)
        
        Set SourceWorkbook = Workbooks.Open(FileName, False, True)
        
        SourceWorkbook.Sheets("Sheet2").Range("A2:C11").Copy PasteRange
        
        SourceWorkbook.Close False
        Application.ScreenUpdating = True
    End If
    
End Sub

Dave
 
Upvote 0
I tried and it still doesn't work

Code works fine for me

does not work it what way?
Did you make any changes to published code and are you getting any errors?

Dave
 
Upvote 0
I made some updates and still not working. I don't believe its copying the data. when I open the sheet the data is selected not copied
 
Upvote 0
I made some updates and still not working. I don't believe its copying the data. when I open the sheet the data is selected not copied

code I posted does not select the data - post code you have updated

Dave
 
Upvote 0
It opens and works put doesn't paste. I have it going to an existing workbook with other sheets in it. I keep the workbook open when running and nothing paste. Its is 2016.
 
Upvote 0
I thought it should copy data from a closed workbook and paste to a workbook that's open that I'm working on.
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,329
Members
449,155
Latest member
ravioli44

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