I'm trying to write a macro that takes Sheet1 from another, already open, workbook and copy it into a the workbook that will use the data. The macro will be run from the workbook that will be receiving the copied sheet and the file I want the data from will always be named the same.
I've tried the follow code, but it either just copies incorrectly or doesn't work.
Workbooks("FileIWantToCopyFrom.XLSX").Sheets("Sheet1").Copy
ActiveWorkbook.Worksheets("FileIWantToCopyFrom.XLSX").Select
Sheets("Sheet1").Copy After:=Workbooks("Book1").Sheets(1)
Any help would be much appreciated. Thanks!
Related
I have a macro, and I am struggeling with saving. The macro:
opens second workbook from SharePoint
unprotects it
unprotects a sheet in the original workbook
copies line(s)
pastes those line(s) in the second workbook
protects the sheet in the second workbook
saves the second workbook
closes it
protects the sheet in original workbook
I have this macro in 4 files, always 1 original file - 1 second file. Two files are without any problems, but the other 2 files, I can't save the second file. It says that there is already a file with this name, and asks if I want to rewrite it.Why does it ask that, when I am using the function .Save?Or, if this cannot be managed, how do I write in the macro that it should choose - yes, rewrite?
Thank you.
I tried to use the combined function Close SaveChanges:=True, but it didnt help.
This my first post in this forum, and I am newbie to VBA. I thought to ask you whether you might have some ideas how to do the following task I am trying to accomplish in VBA:
I have more than 600 workbooks in a folder containing multiple spreadsheets. Each of spreadsheets contains some sort of data which I need to access their range and copy to new workbooks.
From sheets stored in one workbook for example I only need one, which is predominantly stored at Sheets index position 1, although in more than 100 workbook that is not the case. I already looped through all files in folder with Dir() function and know which workbooks do have sheets that I need on wrong index position. I need also to copy a range of that specific sheet to another workbook, which I am able to do but how to use VBA to find dynamically workbook I need and sheet by its index position. I really want to avoid opening workbooks via Dir function in a loop and do work manually.
How can I dynamically use an array or any other method and find those workbooks and sheets within workbooks that I am interested and either flag them through Boolean If statement and either have msg box pop out saying: “This sheet needs work”, or just copy the range to new workbook and save it along with the remainder of workbooks in a folder.
Any ideas or help would be much appreciated.
Many thanks
I have an Excel workbook with some VBA code in modules and in the worksheets. As I use the workbook, Excel will suddenly create ghost/phantom worksheets that can't be deleted. IN the screen shot below the original works sheets, Sheet17 (Main Menu ) and Sheet 25 (Results) ended up getting duplicated (Sheet17, Sheet171, Sheet1711, Sheet 17111; Sheet25, Sheet251, Sheet2511, Sheet25111, etc). The version less than Sheet17111 are ghost worksheets (notice the different icon) that can not be deleted. The VBA code for those worksheets stayed with the latest version of the worksheet (ie. Sheet17111).
The only way to get rid of the ghost worksheets is to save the workbook as an xlsx, close the workbook open up the xlsx, copy over the worksheet code and modules and save out the xlsm. Of course this only corrects the problem until the next occurrence
I am not calling any special addins.
Any tips on what I can try to correct this problem.
Ian
I am trying to copy a sheet from one Workbook to another using vba. I am using the following code:
ThisWorkbook.Sheets("Invoice").Cells.Copy
When I run it only the cells get copied and logo isn't getting copied. How to I change this to copy the logo also?
Simply copy the entire sheet rather than the Cells
Something like this to copy from the Workbook containing the code to the first sheet of Book1
ThisWorkbook.Sheets("Invoice").Copy Workbooks("Book1").Sheets(1)
Or from the ActiveWorkbook
ActiveWorkbook.Sheets("Invoice").Copy Workbooks("Book1").Sheets(1)
Please, could you tell me if the following is possible in VBA:
I need to write code that copies a sheet from my workbook to a new freshly created xls file. BUT, the sheet I wanna copy has a Macro behind (it's actually a WorkSheet_Change event handler) that I also want to copy to the new file.
Many thanks
Kind regards,
Miloud B
You can Export the code from the original and then Import it into the copy.
Here's an article that shows some code for doing this: Macro to Add a Macro to New Workbooks