Copy Range of cells from active workbook to existing workbook

praveenpatel421983

New Member
Joined
Aug 17, 2017
Messages
41
Hi all,

I am trying to copy a range from a sheet called "Test" of active workbook to existing workbook in which a sheet called "Test" (which already exists) but I am not able to copy. Please help.

Application.ScreenUpdating = False

Dim parentWorkbook As Excel.Workbook
Dim otherWorkbook As Excel.Workbook
Dim workbookName As Variant

Set parentWorkbook = ActiveWorkbook
workbookName = "C:\New Microsoft Excel Worksheet.xlsx"

If Not workbookName = False Then
Set otherWorkbook = Workbooks.Open(workbookName)
otherWorkbook.Sheets("Test").Range("A1:G1000").Value = parentWorkbook.Sheets("Test").Range("A1:G1000").Value
otherWorkbook.Close False
Set otherWorkbook = Nothing
End If

Application.ScreenUpdating = True
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Without testing, your confusion lies with the IF statement. Remove that and it should be ok:

Code:
    Dim parentWorkbook As Excel.Workbook
    Dim otherWorkbook As Excel.Workbook
    Dim workbookName As String [COLOR=#008000]'I Changed this to string as that was what it was set to[/COLOR]
    
    Set parentWorkbook = ActiveWorkbook
    workbookName = "C:\New Microsoft Excel Worksheet.xlsx"
    
   [COLOR=#008000] 'If Not workbookName = False Then 'Confusing. Not needed. WorkbookName is set in previous line and is a string and was declared as a variant[/COLOR]
    Set otherWorkbook = Workbooks.Open(workbookName)
    otherWorkbook.Sheets("Test").Range("A1:G1000").Value = parentWorkbook.Sheets("Test").Range("A1:G1000").Value
    otherWorkbook.Close False
    Set otherWorkbook = Nothing
    [COLOR=#008000]'End If[/COLOR]
    
    Application.ScreenUpdating = True 'This isn't set to false anywhere?
 
Upvote 0
Ha, yes should have spotted that. Always better if you spot it yourself. Glad it's working
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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