Can not copy Range to other Workbook in VBA office 2010 - excel

I have a strange issue I can not make run following subroutine :
Sub Copy_Workbook()
Workbooks("book1").Sheets("Sheet1").Range("a1").Copy _ Workbooks("book2").Sheets(2).Range("a2")
End Sub
I have both excels xlsm, both are open and are open in the same instance.
I do receive the problem Out of range.
If anyone has any idea why it could not work can you please advice , thanks.

The following sub will work.
Sub Copy_Workbook()
Workbooks("book1").Sheets("Sheet1").Range("a1").Copy Workbooks("book2").Sheets(2).Range("a2")
End Sub
You have to confirm:
Two workbooks are open, Book2 have at least 2 (two) sheets. Check all spelling are correct for sheet name.

If your workbooks haven't been saved then referencing without their file extension will work.
Once you save them then you must include the extension.
You can confirm this by creating a new workbook then going to the Immediate pane in the VBA IDE and typing ?workbooks("Book1").name. It will return "Book1".
Save the workbook in .xlsm format. Go back to the VBA IDE Immediate pane and re-execute ?workbooks("Book1").name. It will throw a "Subscript out of range" error.
Changing your code to:
Sub Copy_Workbook()
Workbooks("book1.xlsm").Sheets("Sheet1").Range("a1").Copy _
Workbooks("book2.xlsm").Sheets(2).Range("a2")
End Sub
will work!

Related

Open Excel worksheet - run time error with other macros

I made a workbook with several sheets for data entry, and I want the index/main sheet to display upon opening every time.
Everything on the internets says to use something like below, but I get a runtime error.
I've made sure the worksheet is named "Attendance Main" and the spelling is correct.
Most troubleshooting issues I've seen are about activating sheets for copying/pasting and other actions. I just want the main sheet to display each time the workbook is opened.
When I create a new workbook, with 2 basic sheets, the macro works. So I am wondering if other macros are interfering with it. However, none of my macros are in "ThisWorkbook", most are in their own modules.
Any help appreciated, and if you need any info from me to narrow down the issue I will try to provide what I can.
Private Sub Workbook_Open()
Worksheets("Attendance Main").Activate
End Sub
Run-time error '1004'
Activate method of Worksheet class failed
For me a simple select code inside the open event of Workbook works fine.
Private Sub Workbook_Open()
Sheets("ShowThisFirst").Select
End Sub

Excel AddIn using Name Manager

at work we have some add-ins. Someone got the idea to rename a Sheet, whereupon the Script no longer work. To prevent this from happening in the future, I put the following formula in the worksheet and gave the cell a name using the Name Manager
=TEIL(ZELLE("dateiname";A1);SUCHEN("]";ZELLE("dateiname";A1))+1;31)
In VBA I use the following script:
fileName = "Filename.xlam"
Test = Workbooks (fileName) .Application.Names ("Test")
If I set temporarily the isAddIn value to False, everything works without any problems. As an add-in, however, I get error 1004. Can someone help me?
It should be great, if the possibility to get the file name of the xlam add-in via a function, in case someone else comes up with the brilliant idea of renaming this file.
Many thanks for your help
Sam
I use this method to lock my sheet names: Add this to the worksheet code that you want to lock and then save the sheet. If a user tries to edit the name it will simply revert back to its original name.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If ActiveSheet.Name <> "test" Then
ActiveSheet.Name = "test"
End If
End Sub
source

Workbook_Open() in Excel 2016 not firing

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all!
The simple event sub
Private Sub Workbook_Open()
MsgBox "Work book is open"
End Sub
does not seem to work. However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.
I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window. Is this a bug in 2016 and is there a workaround?
I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there. But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.
It is a simple single Sub in the ThisWorkbook module. Macros are enabled. In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros. It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function.
Try encapsulating the call with a new instance of Excel. Example below:
Sub OpenInNewExcel()
Dim Background_Excel As Excel.Application
Dim pathName As String
Dim fileName As String
Let pathName = "Enter your path here" 'include "\" at the end
Let fileName = "Enter your file name here"
Background_Excel.Workbooks.Open fileName:=pathName & fileName
Background_Excel.Parent.Quit ' This is how you close the file completely using VBA otherwise the file will close and the Excel Shell will remain.
End Sub
Also make sure that enable macros is turned on in the Options-Trust Center.
You have to add the file/folder location of your workbook as a "Trusted Location".
You can see more info about that in Designate trusted locations for files in Office 2016.
I have same problem then I found solution after google it:
https://www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger
Then I also use "Private Sub Workbook_Open()" and "Public Sub Auto_Open()" open in excel 2016 that work fine:
Private Sub Workbook_Open()
CustomStartUp
End Sub
Public Sub Auto_Open()
CustomStartUp
End Sub
Private Sub CustomStartUp()
MsgBox "Work book is open"
End Sub
I've had this problem (I'm using Microsoft 365), and I found this thread.
It happens for me sometimes when I have another workbook already open, then, on trying to open my macro-enabled workbook, before any sheet is displayed I get the Microsoft warning message about macros. Then, although I click its 'Enable' button, the Workbook opens, macros do get enabled, but Workbook_Open doesn't run.
I've never known the problem to occur if no other workbook is open. (Of course, the user might still get the yellow-backed messages at the top of the workbook, asking them to click the Enable Editing and/or Enable Macros buttons.)
Note that my Workbook_Open just calls another 'workbook-opening' sub in a module to do all the opening processing.
My solution: When my workbook-opening sub is called, it sets a global variable to True to indicate it has run.
I've made it obvious to the user that the problem has occurred, by means of a 'Welcome' sheet with all its cells locked, so the user can do nothing; at this point all other sheets are very hidden. The workbook-opening sub, when it runs, deactivates this sheet and makes it very hidden, so the user never normally sees it, and makes the other sheets visible. But if this screen remains, it instructs the user to select the other workbook, then select this one again. My Workbook_Activate code then runs, and because the global variable isn't True, it calls the workbook-opening sub. If this global variable is True, it does nothing.
To make this work, the Workbook_Close sub makes the other sheets very hidden and the Welcome sheet visible, ready for the next time the Workbook is opened.
Hey presto, the problem is solved.
The Welcome sheet actually has a dual purpose, in that if either of the yellow-backed warning messages are displayed, it will remain and force the user, with suitable instructions, to click Enable Editing and/or Enable macros. If the users aren't au fait with macro-enabled Excel, they will just ignore these and try to carry on regardless.
All this is much easier to implement than to explain. I hope it's clear.
And I hope this might be of help to someone.
I had this issue with one of my files as well. I managed to fix this issue by running Workbook_Open manually in the VBA editor once open and saving the file in another location. The file in the new location should have no issue with auto-running Workbook_Open. If this doesn't work, copy the original file to a new location before manually running & saving.
If the newly saved file does not run Workbook_Open, repair your version of Office.

Copy/Paste data to other existing workbook on timer

I currently have this VBA -
Sub StartTimer()
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
Sub AutoCalc()
Application.CalculateFull
Sheets("Kine").Range("B603:E603").Copy _
Destination:=Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1")
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
The .OnTime command is working perfectly without the Copy/Paste section, which is great.
This gives me a list of values from an SQL query that will auto-update, as well as an average at the bottom of each column of values.
I'm trying to set this up so that the average will automatically be added onto columns in Minitab but I believe that the Macro is stopping the Auto-Update in Minitab.
So my idea is to copy-paste the averages into an Excel Workbook that has no macros of its own and then link that to Minitab.
Have I put in the Copy-Paste code wrong or is there some issue with where the macros need to be stored and how?
Quick Edit - I should add that current code gives "Run-Time Error 9, Subscript out of range" and highlights copy/paste code.
I've found the solution.
My destination workbook was open in a separate window so the source wasn't recognising it as being open. Bit of a nightmare!
It's necessary to have both workbooks open in the same instance of Excel.
Additionally, my original paste code only pasted "#REF". I have changed that to -
Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1").PasteSpecial xlValues
Works much better.
One more thing, in case anybody might find it useful. The source workbook must be active in order to carry out the auto-update.
Adding below line sorted out most issues though its still a work in progress -
ThisWorkbook.Activate

VBA Run-time error '9': Subscript out of range; trying to activate another workbook

I'm trying to create a VBA in Excel 2010 that takes info from another spreadsheet that I'm not allowed to alter and bring it over to the spreadsheet with my macro built in. Here's my code:
Sub BringUpWorkbook()
Workbooks("RITE 1624.xls").Activate
End Sub
I have several VBA books, have visited dozens of sites on the Internet, including those here at stackoverflow.com, and cannot find a reason why I'm receiving the run-time error. The workbook is already open, I've tried adding everything trailing the title, I've tried removing the .xls, I've even did all of the above with in a variable. Any ideas?
Make sure the extension is correct. If it's a excel-2010 file like you indicate, you may need to update your code to reflect the extension of your "RITE 1624" file (e.g. .xlsx for a 2010 Excel workbook, .xlsm for a 2010 Excel macro-enabled workbook, or whatever the extension is.
Sub BringUpWorkbook()
Workbooks("RITE 1624.xlsx").Activate
End Sub
EDIT:
To make sure you have the right name of the workbook, you can print the name of each workbook in an immediate window.
Open up the VBA editor, and then press Ctrl+G to open the Immediate Window (or do View > Immediate window). Then run the following Macro:
Sub OpenWkbkNames()
For Each Workbook In Workbooks
Debug.Print Workbook.Name
Next
End Sub
Note that this will give you the names of all the open workbooks in the same Excel instance as your macro. If your RITE 1624 file is in a separate Excel instance, then the instance that your macro is in will not be able to see that file (and it won't appear in the output of the OpenWkbkNames code above). The simplest way to resolve this is to open all of your necessary files from the Excel instance that contains your macro.
Sub BringUpWorkbook()
Workbooks.Open("RITE 1624.xls").Activate
End Sub

Resources