Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:08:51+00:00 2026-06-12T05:08:51+00:00

Set mainWB = Workbooks(Copy Paste.xlsb) Set mainWS = mainWB.Sheets(Sheet1) Set testWS = mainWB.Sheets(Sheet3) mainWS.Select

  • 0
Set mainWB = Workbooks("Copy Paste.xlsb")
Set mainWS = mainWB.Sheets("Sheet1")
Set testWS = mainWB.Sheets("Sheet3")

mainWS.Select

I keep getting an error on the last line in Excel VBA:

“Method Select of Object ‘_Worksheet’ failed”

Any idea why or how to fix this? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:08:52+00:00Added an answer on June 12, 2026 at 5:08 am

    As discussed in comments, cannot select Sheets in VBA that are not active (or Range objects on them).

    For example the following code

    Sheets(1).Activate
    Sheets(2).Range("A1").Select
    

    will cause the error you are receiving.

    In your case, it seems you are attempting to Select a sheet which is not active – your mainWS object is presumably not the ActiveSheet at the point you are calling this code. An easy way to test if this is happening is if you add the following to the end of your code:

    if (ActiveSheet.Name <> mainWS.Name) then
        msgbox ("Going to crash after clicking ok!")
    end if
    mainWS.Select
    

    Note that you can refer to the activated worksheet with the command ActiveSheet to either get properties or whatever other operations you are interested in doing.

    This error can also happen if you have loop working thru all of the worksheets in the workbook and there are hidden sheets. Lookout for that.


    Last, and unrelated to your specific error message, I assume you are declaring those variables somewhere and simply did not copy them here – if not I would consider using Option Explicit as you can often run into all sorts of issues without having Option Explicit at the top of your VBA code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

set dateformat dmy select isdate('31/1/2012') I thought by going into Change the format of
SET @v1 := SELECT COUNT(*) FROM user_rating; SELECT @v1 When I execute this query
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER VIEW [dbo].[viewProductExportUpdated2] AS SELECT dbo.ProductCategories.ProductCategoryID
SET @Password = ( SELECT UserPassword,IsLocked FROM [Authentication].[tblLogin] WHERE UserName=@UserName) i m trying to
Set OutApp = CreateObject(Outlook.Application) MsgBox (OutApp Is Nothing) On Error GoTo errorHandler Set OutMail
Set oXMLHttp=Server.CreateObject(MSXML2.XMLHTTP) On Error Resume Next oXMLHttp.open GET, http://xxxxxx.com,False oXMLHttp.setRequestHeader Content-Type, application/x-www-form-urlencoded oXMLHttp.send() x
set application [::tcom::ref createobject Excel.Application] $application Visible 1 set workbooks [$application Workbooks] set workbook
I set up custom error pages in nginx with fastCGI for PHP like this
set Q1= 'SELECT ID, EnquiryID, EnquiryDate, CompanyName, ContactPerson FROM EnquiryHeader WHERE '; set Q1
SET @sql = 'Declare ChildTableMigrator CURSOR FOR select ['+@FieldName+'] , ['+@FieldName+'Alias] from [' +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.