+ Reply to Thread
Results 1 to 6 of 6

Error when selecting last row

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Thumbs down Error when selecting last row

    Hey there,

    I am trying to delete all the data on "Sheet1" of my spreadsheet except row 1 but using the code below, I keep getting an error when the macro is trying to calculate the end row and column, can any tell me where I am going wrong...and to stop me from going bonkers?!

    Sub clear_sheet_content()
    
    Dim oSheet As Excel.Worksheet
    Dim col As Integer
    Dim row As Integer
    
    Set oSheet = Worksheets("Sheet1")
    
    col = ActiveSheet.UsedRange.Columns.count
    row = ActiveSheet.UsedRange.Rows.count + ActiveSheet.UsedRange.row - 1
    
        On Error GoTo Errhandler
        
        oSheet.Range(cells(2, 1), cells(row, col)).Select
        Selection.ClearContents
        Selection.NumberFormat = "General"
        MsgBox "The sheet has been cleared", vbInformation, "Add Non Conformity"
        
    Errhandler:
    
        MsgBox "There is no data on sheet1", vbInformation, "Add Non Conformity"
        
        
    End Sub
    Cheers,

    Jag
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Error when selecting last row

    When you run the macro, which sheet is active and which sheet do you want cleared...?

  3. #3
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: Error when selecting last row

    Hey Steve, I'm using that button on the NC Sheet Info to activate the macro. The macro should then clear all the data on "Sheet1" except of course the top row which is my header row.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Error when selecting last row

    I was gently trying to point out that the macro calculates the extent of the data based on the active sheet, which is not the sheet containing the data when you press the button. Change references to ActiveSheet to oSheet.

    EDIT: try this
    Sub clear_sheet_content()
    
    Dim oSheet As Excel.Worksheet
    Dim col As Long
    Dim row As Long
    
    Set oSheet = Worksheets("Sheet1")
    
    With oSheet
        col = .UsedRange.Columns.Count
        row = .UsedRange.Rows.Count + .UsedRange.row - 1
        
        On Error GoTo Errhandler
        With .Range(.Cells(2, 1), .Cells(row, col))
            .ClearContents
            .NumberFormat = "General"
        End With
    End With
        MsgBox "The sheet has been cleared", vbInformation, "Add Non Conformity"
        Exit Sub
    Errhandler:
        MsgBox "There is no data on sheet1", vbInformation, "Add Non Conformity"
        
    End Sub
    Last edited by StephenR; 10-07-2009 at 07:58 AM.

  5. #5
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: Error when selecting last row

    Oh I see...yeah I've just amended that again actaully and it works! I really need to get a grip of using with and end with statements so I'll look into that now...thanks for your help
    Last edited by therealjag; 10-07-2009 at 08:01 AM. Reason: Update

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Error when selecting last row

    It worked for me on your attachment.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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