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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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