+ Reply to Thread
Results 1 to 4 of 4

Method 'Range' of objects'_worksheet' failed

Hybrid View

  1. #1
    Registered User
    Join Date
    12-08-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    37

    Method 'Range' of objects'_worksheet' failed

    Hi All

    Was wondering if anyone knows why I would be getting this error (Method 'Range' of objects'_worksheet' failed) when I try and run this code?
    The code works fine on the first sheet (Sheet1) but not on any of the remaining sheets (Sheet7 - Sheet 13)

    Dim ws As Worksheet
    Dim del2 As Long
    del2 = TextBox5.Value
    
    For Each ws In ActiveWorkbook.Worksheets
    If ws.CodeName = "Sheet1" Or ws.CodeName = "Sheet7" Or ws.CodeName = "Sheet8" Or ws.CodeName = "Sheet9" Or ws.CodeName = "Sheet10" Or ws.CodeName = "Sheet11" Or ws.CodeName = "Sheet12" Or ws.CodeName = "Sheet13" Then
    
    Select Case del2
    Case 1 To 13
    x = 16 - (13 - del2)
    
    'Commision Charge Static Test
    ws.Range(Cells(21, x), Cells(23, 15)).ClearContents                'Clear contents of unused cells first row of commision charge static test
    ws.Range("B24", "O41").Clear                                       'Clear unused rows from commision charge static test
    ws.Range("B23", "O23").Borders(xlEdgeBottom).LineStyle = xlDouble  'Adds boarder back to bottom of row 23
    
    'Capacity Static Test
    ws.Range(Cells(174, x), Cells(176, 15)).ClearContents                'Clear contents of unused cells first row of capacity static test
    ws.Range("B177", "O194").Clear                                       'Clear unused rows from capacity static test
    ws.Range("B176", "O176").Borders(xlEdgeBottom).LineStyle = xlDouble  'Adds boarder back to bottom of row 176
    
    'Pre Recharge Static Test
    ws.Range(Cells(338, x), Cells(340, 15)).ClearContents                'Clear contents of unused cells first row of pre recharge static test
    ws.Range("B341", "O358").Clear                                       'Clear unused rows from pre recharge static test
    ws.Range("B340", "O340").Borders(xlEdgeBottom).LineStyle = xlDouble  'Adds boarder back to bottom of row 340
    
    'Pre HDC Static Test
    ws.Range(Cells(499, x), Cells(501, 15)).ClearContents                'Clear contents of unused cells first row of pre HDC static test
    ws.Range("B502", "O519").Clear                                       'Clear unused rows from pre HDC static test
    ws.Range("B501", "O501").Borders(xlEdgeBottom).LineStyle = xlDouble  'Adds boarder back to bottom of row 501
    
    'After HDC Test
    ws.Range(Cells(548, x), Cells(550, 15)).ClearContents                'Clear contents of unused cells first row of after HDC static test
    ws.Range("B551", "O568").Clear                                       'Clear unused rows from after HDC static test
    ws.Range("B550", "O550").Borders(xlEdgeBottom).LineStyle = xlDouble  'Adds boarder back to bottom of row 550
    
    'Final Static Test
    ws.Range(Cells(597, x), Cells(599, 15)).ClearContents                'Clear contents of unused cells first row of final static test
    ws.Range("B600", "O617").Clear                                       'Clear unused rows from Final Static Test
    ws.Range("B599", "O599").Borders(xlEdgeBottom).LineStyle = xlDouble  'Adds boarder back to bottom of row 599
    
    
    End Select
    End If
    Next ws
    Last edited by grantastley; 01-29-2013 at 04:31 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    With code like this you need a worksheet reference for Cells as well as Range.
    ws.Range(Cells(21, x), Cells(23, 15)).ClearContents
    Here's a couple of ways you can do that.
    ws.Range(ws.Cells(21, x), ws.Cells(23, 15)).ClearContents 
    
    ' or
    
    With ws
       .Range(.Cells(21, x), .Cells(23, 15)).ClearContents 
    End With
    You'll need to add a worksheet reference for Cells wherever you have code like this.
    Last edited by Norie; 01-29-2013 at 03:24 AM.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Method 'Range' of objects'_worksheet' failed

    You need to qualify each Cells object.

    i.e
    ws.Range(ws.Cells(21, x), ws.Cells(23, 15)).ClearContents
    Otherwise, it assumes ActiveSheet.Cells....

  4. #4
    Registered User
    Join Date
    12-08-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Method 'Range' of objects'_worksheet' failed

    Thanx guys works like a treat now

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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