+ Reply to Thread
Results 1 to 3 of 3

Method 'Name" of object'_Worksheet' failed

  1. #1
    Registered User
    Join Date
    09-01-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    3

    Method 'Name" of object'_Worksheet' failed

    Hoping I can get help fixing my code for this error. Thank you!

    -----

    Sub ML_ExtractKLToSheetsLastName()
    Dim ws As Worksheet
    Dim wsNew As Worksheet
    Dim rData As Range
    Dim rfl As Range
    Dim KL_LastName As String
    Set ws = ActiveSheet

    With ws
    Set rData = .Range(.Cells(1, 1), .Cells(.Rows.Count, 86).End(xlUp))
    .Columns(.Columns.Count).Clear
    .Range(.Cells(2, 21), .Cells(.Rows.Count, 21).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Cells(1, .Columns.Count), Unique:=True

    For Each rfl In .Range(.Cells(1, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp))
    KL_LastName = rfl.Value

    If WksExists(KL_LastName) Then
    Sheets(KL_LastName).Cells.Clear
    Else

    Set wsNew = Sheets.Add
    wsNew.Move After:=Worksheets(Worksheets.Count)
    wsNew.Name = KL_LastName 'this is where the code fails'

    End If

    rData.AutoFilter Field:=21, Criteria1:=KL_LastName
    rData.Copy Destination:=Worksheets(KL_LastName).Cells(1, 1)

    ActiveSheet.Cells.Columns.AutoFit

    'ActiveSheet.Range("G2").Select
    'ActiveWindow.FreezePanes = True

    'ActiveSheet.Columns("S:U").Select
    'Selection.Columns.Group
    'Range("G2").Select

    'ActiveSheet.Outline.ShowLevels ColumnLevels:=1 ' to collapse the columns

    Next rfl
    End With
    ws.Columns(Columns.Count).ClearContents
    rData.AutoFilter
    End Sub

    Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
    End Function

  2. #2
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Method 'Name" of object'_Worksheet' failed

    what is the value KL_LastName at the time of the assignment name?
    To do it for me and help me it is 2 different things!
    Sorry for my english, blame Google translator

  3. #3
    Registered User
    Join Date
    09-01-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Method 'Name" of object'_Worksheet' failed

    I just realized the issue is that I have an existing worksheet named a value in the defined range KL_LastName. I renamed the existing worksheet so there is no longer a conflict and the code works.

+ 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. [SOLVED] Runtime Error: 1004 Method 'Visible' of Object "_Worksheet' failed
    By Sorjas in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-15-2015, 02:29 AM
  2. [SOLVED] "Method 'Range' of object '_worksheet' failed" when a different sheet is active
    By CO# in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2014, 11:48 AM
  3. XL03 Sort error: "Method 'Range' of object '_Worksheet' failed"
    By AdLoki in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2011, 08:23 AM
  4. "Method 'Range' of object '_Worksheet' failed" error in Windows Script Host
    By jikenj in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-16-2010, 02:18 PM
  5. Error in Macro: "Method 'Paste' of object '_Worksheet' failed"
    By blork in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-05-2006, 01:50 PM
  6. What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 PM
  7. "method 'Copy' of object '_Worksheet' failed"
    By Terry Holland in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2005, 12:05 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