Trouble copying worksheet between workbooks

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all,

I have made a macro in excel to copy a worksheet from another workbook without opening that second workbook, but this code only worked the first time.

VBA Code:
Sub CopySheetFromClosedWB()
Application.ScreenUpdating = False

    Set closedBook = Workbooks.Open("C:\Users\RubenDias\Desktop\Mapas Ajudas de Custo\Lista Obras.xlsx")
    closedBook.Sheets("Folha1").Copy After:=Sheets("Folha1")
    closedBook.Close SaveChanges:=False

Application.ScreenUpdating = True
End Sub


The plan is to copy "Folha1" from "Lista Obras" workbook and copy it as last worksheet in the currently opened workbook, without opening "Lista Obras".
This code worked the first time I tried it.

I can copy the worksheet manually.

Can anybody help me with this?

Thank you all
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Did you put any variables that can change in the code, such as the folder path?
Perhaps excel does not recognize the folder path.
 
Upvote 0
Please post to this forum in English only.
 
Upvote 0
So please repost your reply in English.
 
Upvote 0
Did you put any variables that can change in the code, such as the folder path?
Perhaps excel does not recognize the folder path.
The folder path is the same as it was before.
If excel wasn't recognizing the path it would throw me an error, and that does not happen.
It simply looks like it refreshes my opened workbook (and closes my "Lista Obras", if I have it open already) and nothing else happens.

Can I be having problems because de worksheets have the same name?
Already tried to delete the worksheet copied first and then tried to run this macro again, but it didn't worked.
Also tried with ScreenUpdating = True, but it was the same outcome
 
Upvote 0
Found my solution.
I must have deleted part of the code without realizing, because I just had to add "ThisWorkbook" on the copy line.

Now I will just update this code to delete the last worksheet (previously copied by this code) to copy a new one with the same name.


VBA Code:
Sub CopySheetFromClosedWB()
Application.ScreenUpdating = False

    Set closedBook = Workbooks.Open("C:\Users\RubenDias\Desktop\Mapas Ajudas de Custo\Lista Obras.xlsx")
    closedBook.Sheets(1).Copy After:=ThisWorkbook.Sheets(Sheets.Count)
    closedBook.Close SaveChanges:=False

Application.ScreenUpdating = True
End Sub


Thanks you all for the help
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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