+ Reply to Thread
Results 1 to 5 of 5

VBA Add Worksheet: "That command cannot be used on multiple selections"

  1. #1
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    VBA Add Worksheet: "That command cannot be used on multiple selections"

    I am using the following VBA to add a new worksheet but occasionally receive the '1004' run-time error "That command cannot be used in multiple selections"

    PHP Code: 
    Worksheets.Add(Before:=Worksheets("SheetA")).Name "SheetB" 
    Although I thought I broadly understood the nature of this error message, I am struggling to find where the multiple selections might be. Is there some command I can put before "Worksheet.Add" to guarantee that multiple selections are not occurring and therefore prevent the run-time error?

    I've tried

    PHP Code: 
    Worksheets("SheetA").Activate 
    and

    PHP Code: 
    Worksheets(("SheetA").Visible xlSheetVisible
    Sheets
    (("SheetA").Select 
    But these do not seem to handle it.

    Thanks in advance.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Add Worksheet: "That command cannot be used on multiple selections"

    Can we see the rest of the code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VBA Add Worksheet: "That command cannot be used on multiple selections"

    Full procedure:

    PHP Code: 
    Public Function CreateWorksheet(NewWSName$, BeforeWSName$, Optional TabColour As VariantOptional AfterWS As Variant)

        
    Dim wsSheet As Worksheet
        
        On Error Resume Next
        Set wsSheet 
    Sheets(NewWSName)
        
    On Error GoTo 0
        
        
    If Not wsSheet Is Nothing Then
            ClearWSContents 
    (NewWSName)
        Else
            
    ' new & blank
            If IsMissing(AfterWS) Then
                Worksheets.Add(Before:=Worksheets(BeforeWSName)).Name = NewWSName
            Else
                Worksheets.Add(After:=Worksheets(BeforeWSName)).Name = NewWSName
            End If
        End If
        
        If Not IsMissing(TabColour) Then
            SetTabColour ActiveSheet.Name, TabColour
        End If

    End Function 

  4. #4
    Registered User
    Join Date
    04-10-2014
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2010
    Posts
    98

    Re: VBA Add Worksheet: "That command cannot be used on multiple selections"

    I am getting a similar error on trying to Add a new Sheet to a workbook.

    I have verified that there are no multiple selections ANYWHERE: I went to every tab, selected Cell A1, made sure no tabs were Hidden or VeryHidden, and made sure that only one sheet was selected at the time the following line was run, and it STILL throws an error, "That command cannot be used on multiple selections."

    Line of code is really simple, too:
    Please Login or Register  to view this content.
    Straightforward, nothing fancy, just add a new worksheet to the active workbook and name it "1. Table of Contents"... should be simple, but apparently Excel cannot handle this any more. It throws out the "That command cannot be used on multiple selections."

    I have found other references to this error here.


    EDIT

    Something else odd is happening. One part of the code says to move a sheet to a specific location, and it is moving it to one sheet prior to where I am telling it. I ran code to unhide all sheets, including VeryHidden sheets, and it registers no sheets hidden or veryhidden in the workbook, but it isn't being numbered correctly.
    Last edited by Phixer; 08-26-2015 at 02:32 PM.

  5. #5
    Registered User
    Join Date
    04-10-2014
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2010
    Posts
    98

    Re: VBA Add Worksheet: "That command cannot be used on multiple selections"

    The problem was in the Pivot Caches elsewhere in the workbook. Somehow they had become corrupted (despite being less than 10 minutes old) and they were preventing any new tabs from being added to the workbook.

    So, I wrote a subroutine that 'wipes' all the pivot caches in the workbook by setting to false the SaveData variable, then saving the file. Elsewhere in the code it closes and reloads the file, which then runs the second part of this subroutine to switch the settings from False back to True (which usually requires a refresh first) and then re-saves.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2013, 12:45 PM
  2. Create a Command Button to "Save As" and "Close" an Excel Workbook
    By thedunna in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-26-2013, 05:38 PM
  3. How to change a Command Button caption from "Enable" to "Disable"?
    By Infinity in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2007, 12:14 AM
  4. Replies: 3
    Last Post: 04-24-2006, 01:35 PM
  5. How do I add a command button on a worksheet to "paste" from the .
    By Jalifid@hotmail.com in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-13-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1