+ Reply to Thread
Results 1 to 5 of 5

Avoiding Run Time Error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-20-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    255

    Avoiding Run Time Error

    Hello,

    I currently have a spread sheet where the user enters 'y' in the G column. If a y is entered, the user can then press the 'Update' button, and the rows containing a y in the g column will be moved to a different sheet, 'History'.

    It works fine if there are y's entered into the column, but I want to be able to avoid the user getting: "Run-Time Error '1004': no cells were found" when no y's are entered into the g column.

    The current code is this:
    Private Sub UpdateHistory2_Click()
        Application.ScreenUpdating = False
        
        Dim ws As Worksheet, ms As Worksheet
        Set ws = Sheets("Current Contractors")
        Set ms = Sheets("History Contractors")
    
        ws.ListObjects("Table1").Range.AutoFilter
    
        With ms
            'heading:
            .Range(.Cells(1, 1), .Cells(1, 8)).Value = Array("Today's Date", _
                                                             "Purchase Order Date", _
                                                             "Contractor", _
                                                             "Equipment", _
                                                             "Description", _
                                                             "Expected Time of Completion", _
                                                             "Completed? y or n", _
                                                             "Time of Completion")
        
        End With
        With ws.Range("A1:H" & ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row)
                
                .AutoFilter 7, "y"
                .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy
                ms.Range("A" & ms.Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
                .Resize(.Rows.Count - 1).Offset(1).EntireRow.Delete Shift:=xlUp
                .AutoFilter
            
        End With
        
        Application.ScreenUpdating = True
        
    End Sub
    I have attached my work book. The following code is for Current Contractors and History Contractors, although almost the exact code is being used for Maintenance Sheets.

    Please Help
    Attached Files Attached Files

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Avoiding Run Time Error

    perhaps change
                
                .AutoFilter 7, "y"
                .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy
                ms.Range("A" & ms.Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
                .Resize(.Rows.Count - 1).Offset(1).EntireRow.Delete Shift:=xlUp
                .AutoFilter
    to
                
                .AutoFilter 7, "y"
                if .columns(1).SpecialCells(xlCellTypeVisible).count > 1 then
                   .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy
                   ms.Range("A" & ms.Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
                   .Resize(.Rows.Count - 1).Offset(1).EntireRow.Delete Shift:=xlUp
                end if
                .AutoFilter
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Avoiding Run Time Error

    Private Sub UpdateHistory()
        Application.ScreenUpdating = False
        
        Dim ws As Worksheet, ms As Worksheet
        Set ws = Sheets("Current Contractors")
        Set ms = Sheets("History Contractors")
    
        ws.ListObjects("Table1").Range.AutoFilter
    
        With ms
            'heading:
            .Range(.Cells(1, 1), .Cells(1, 8)).Value = Array("Today's Date", _
                                                             "Purchase Order Date", _
                                                             "Contractor", _
                                                             "Equipment", _
                                                             "Description", _
                                                             "Expected Time of Completion", _
                                                             "Completed? y or n", _
                                                             "Time of Completion")
        
        End With
        With ws.Range("A1:H" & ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row)
                
                .AutoFilter 7, "y"
                On Error GoTo no_cells
                .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy
                On Error GoTo 0
                ms.Range("A" & ms.Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
                .Resize(.Rows.Count - 1).Offset(1).EntireRow.Delete Shift:=xlUp
                .AutoFilter
              
            
        End With
        
        Application.ScreenUpdating = True
        End Sub
    no_cells:
        Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Avoiding Run Time Error

    Put these lines at the top (before the ScreenUpdating line):

    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    Dim r As Range, y As String
    Set r = Range("G:G")
    y = "y"
    If wf.CountIf(r, y) = 0 Then Exit Sub
    Gary's Student

  5. #5
    Forum Contributor
    Join Date
    05-20-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    255

    Re: Avoiding Run Time Error

    Thank you All!

    JosephP, I used your suggestion because it required the least amount of change!

+ 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