im trying to open another workbook, select a column from that workbook and paste it into my active workbook. However i am running into this error
Runtime 1004: Select method of worksheet class failed
Have tried different ways from watching youtube but cant seem to solve the issue, help pls! thanks in advance!
my code
First off, you should really follow the suggestions to avoid the Select/Selection/Activate/Activexxx pattern in favour of using fully qualified Range references
While sticking to your code "style", you have to change:
ThisWorkbook.Worksheets("Working").Select
to:
ThisWorkbook.Activate
ThisWorkbook.Worksheets("Working").Select
because you cannot select a Worksheet in a Workbook which is not the "active" one, since the previous Workbooks.Open statement has made the newly opened workbook the "active" one
Related
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
This is my first post and I new to programming in VBA. I have a large excel file where I am trying to create a macro to delete multiple tabs. My code is as followed it works as I am prompted with a do I wish to permanently delete:
Option Explicit
Sub delSheet()
Worksheets("sheet2").Delete
End Sub
However when I transfer in a tab (tab name is "sheet92') from another file I get this error using the same code
Option Explicit
Sub delSheet()
Worksheets("sheet92").Delete
End Sub
What is causing the code to delete the sheet2 which is the tab that I created within opening a .xlsb file and creating a new sheet vs. the error message on copying into the file from an existing file? Thank you advance for your help.
Your question is not very clear, but you probably have to reference the correct workbook, like this:
Workbooks("file.xlsx").Worksheets("sheet92").Delete
If the workbook is not already active, giving only Worksheets("sheet92") won't work.
I solved my first progroamming bug! I was using the wrong name, I was using the field (name) vs. the actual name of the tab, I took a screen capture here !http://imgur.com/a/bPNkg! once I used name Sheet1 I was able to delete. Thank you for your help #cub
I'm trying to copy a worksheet from workbook into another using Worksheets.Copy function. However, this seem to be working in only some of the files and for others I get "Unable to get the Copy property of the Worksheet class" error message. I've tried to copy/move worksheets manually from other workbooks. Also, tried unprotecting/protecting workbook and worksheets but none seem to be working. I'm using Microsoft Office 2010. Is there an Excel feature that enables/disables copying worksheets form other workbooks?
In case someone stumbles upon this in the future, my issue was that the worksheet I was trying to copy was set to 'xlSheetVeryHidden'. To overcome this issue, I temporarily made the sheet visible and made it hidden again straight after the Copy call.
templateWorksheet.Visible = xlSheetVisible
Call templateWorksheet.Copy
templateWorksheet.Visible = xlSheetVeryHidden
In my case the issue was that I had not been working with the worksheet but the workbook. Make sure you are using the worksheet and not the book.
In my case, the Excel file was not enabled for editing. Once I did that, it worked.
I have a workbook (we'll call it "AAA") that is based on a template and refers to code modules in a second workbook. The second workbook (we'll call it "CodeStorage") is a repository for code modules so that any saved versions of the template will be able to access modified/updates code without a change to the saved workbook.
My problem arises when I have multiple windows open in "AAA" and try to get the activesheet when a module is running in "CodeStorage". In code, I create an object ("oWorkbook") that is a reference to the workbook "AAA" When "AAA" has focus, oWorkbook.Activesheet returns the sheet for the active window. When "CodeStorage" is running a module and thus has focus, oWorkbook.Activesheet returns the sheet that is selected in window #1 regardless of what window (#2, 3, etc) was active when the code module in "CodeStorage" was called.
Has anyone run into this and have you found a work around?
ActiveSheet is confusing the way you are using it.
You need to explicitly activate a sheet for it to be considered the ActiveSheet. Running code in another workbook does not activate it.
Selecting cells in a worksheet will activate it. Or specifically calling Activate.
You could do something like:
oWorkbook.Activate
oWorkbook.Activesheet
Alternatively, and preferably, you could do something like the following:
oWorkbook.Worksheets("Sheet1")
oWorkbook.Worksheets(1)
Both these are better. If your user selects a different workbook during runtime execution or you select something in a different sheet, ActiveSheet will return something different.
It's better to fully qualify you workbook paths when using multiple workbooks. This will save you a ton of headache in the future for dealing with "what is activated?" or "what is selected?" This answer is worth reading, too.
To rephrase my question, I was looking for a way to reference the last active worksheet in a workbook that had multiple windows open. If window 1 was selected, I wanted the sheet from window 1, same for window 2, 3, or ???. I also needed the reference when a code module in a different workbook was running and the code module had a variable that was an object reference to the calling workbook.
The solution is Workbook.Windows.Item(1).ActiveSheetView.Sheet. When called from the running code module, even in a different workbook, it returns the same as workbook.activesheet when that is called from the workbook itself. My tests show that Workbook.Windows.Item(1) is the last active window when a workbook loses focus.
Thank you enderland. You got me pointed in the right direction.
I have a problem with adding an event code to a newly created sheet.
The problem seems to only occur right after I open the Excel workbook.
I use
Dim codemod As Object
codemod = ActiveWorkbook.VBProject.VBComponents(Worksheets("Sheet4").CodeName).CodeModule
to add the code to the created sheet module but when I try to run this code right after opening the Excel workbook it gives me an error: run-time error '9' Subscript out of range. The debug points to the codemod line.
The weird part is that this error does not come up again when I change the code just a tiny bit and then change it back to the original state. After I do this the code runs as it should i.e. inserts code to the newly created sheet.
Anyone got any idea what may be the problem?
There is not much else to the code except inserting the lines but that does not seem to be the problem.
Thanks in advance
(This is my first action on S.O. so please don't shout at me when I'm doing things wrong.)
Did you concider preparing a workbook + sheet with the desired event code (Test1.xls-Sheet1). Then, in the target workbook (Test2.xls), copy that prepared sheet. The code in the target workbook would look like
Sub Demo1()
Workbooks.Open "Test1.xls"
Sheets("Sheet1").Copy After:=Workbooks("Test2.xls"). _
Sheets(Workbooks("Test2.xls").Worksheets.Count)
Workbooks("Test2.xls").Activate
End Sub
This obviously is a workaround but it works instantly.
A second option could be to prepare the 'workbook-with-one-sheet' and save it as a (sheet) template in D:\Documents and Settings\User\Application Data\Microsoft\Excel\XLSTART. In that case the code can be
Sub Demo2()
Sheets.Add Type:="Test1"
End Sub