VBA giving inexplicable error

exceluser90

New Member
Joined
Jun 1, 2011
Messages
15
I'm not entirely sure why, but all of a sudden this line of code:

Code:
Worksheets("Analysis").Activate

Where "Analysis" is the name of a worksheet is giving me Application-defined or object-defined error.

It's such a simple line of code and the code worked for the longest time until now, which is why I am so confused.

Any help would be greatly appreciated as my entire program will not work without this.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
Private Sub Analysis_Click()
    Worksheets("Analysis").Activate
    Worksheets("Analysis").Cells.ClearContents
    Sheets("Main").Activate
    Dim r As Long, c As Long
    r = Range("B" & Rows.Count).End(xlUp).row
    c = Cells(18, Columns.Count).End(xlToLeft).Column
    If Not r = 1 And Not c = 1 Then
        Call save
        Sheets("Main").Range(Cells(17, 1), Cells(r, c)).Copy
        Worksheets("Analysis").Activate
        Worksheets("Analysis").Range("A18").Select
        Worksheets("Analysis").Paste
        With Application
            .CutCopyMode = False
        End With
        Sheets("Analysis").Activate
    End If
    
End Sub
 
Upvote 0
Is the worksheet hidden?

In any case, there's no need to select:

Code:
Private Sub Analysis_Click()
    Dim r As Long
    Dim c As Long
 
    Worksheets("Analysis").Cells.ClearContents
 
    With Worksheets("Main")
        r = .Cells(.Rows.Count, "B").End(xlUp).Row
        c = .Cells(18, .Columns.Count).End(xlToLeft).Column
        If r <> 1 And c <> 1 Then
            Call Save
            .Range(.Cells(17, 1), .Cells(r, c)).Copy Worksheets("Analysis").Range("A18")
        End If
    End With
End Sub
 
Upvote 0
Now this gives me the same error:
Code:
    Worksheets("Analysis").Cells.ClearContents

It's so odd because the code worked for the longest time until now. I have no clue what changed.
 
Upvote 0
I closed some other workbooks and now it works. I can only imagine that had some part in it.

Thanks nontheless.
 
Upvote 0
I had wondered if the workbook that contained "Analysis" might not be active, but then you'd have gotten a subscript error. If Analysis is in the workbook the code is in, you can use ThisWorkbook.Worksheets("Analysis").Cells.ClearContents
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top