Copy, to another sheet, a range of cells in a row if one cell has certain text.

Instead of a screen shot, can you upload a copy of your file? De-sensitize the data if necessary. This would make it easier to test a possible solution.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Instead of a screen shot, can you upload a copy of your file? De-sensitize the data if necessary. This would make it easier to test a possible solution.

Done, previous link should have the excel file. All info is test info.
 
Upvote 0
The reason you were getting the error is because only Jan. had data starting in row 13. The other months did not and thus the error "No cells were found". This revised macro checks to make sure that each sheet has at least one occurrence of "held" in column F. If it doesn't have at least one occurrence, then that sheet is skipped.
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Held Appts 4 Submission" Then
            LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
            If WorksheetFunction.CountIf(ws.Range("F13:F" & LastRow), "held") > 0 Then
                ws.Range("A11:F" & LastRow).AutoFilter Field:=6, Criteria1:="held"
                ws.Range("A13:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Held Appts 4 Submission").Cells(Sheets("Held Appts 4 Submission").Rows.Count, "A").End(xlUp).Offset(1, 0)
                If ws.AutoFilterMode = True Then ws.AutoFilterMode = False
            End If
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
The reason you were getting the error is because only Jan. had data starting in row 13. The other months did not and thus the error "No cells were found". This revised macro checks to make sure that each sheet has at least one occurrence of "held" in column F. If it doesn't have at least one occurrence, then that sheet is skipped.
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Held Appts 4 Submission" Then
            LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
            If WorksheetFunction.CountIf(ws.Range("F13:F" & LastRow), "held") > 0 Then
                ws.Range("A11:F" & LastRow).AutoFilter Field:=6, Criteria1:="held"
                ws.Range("A13:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Held Appts 4 Submission").Cells(Sheets("Held Appts 4 Submission").Rows.Count, "A").End(xlUp).Offset(1, 0)
                If ws.AutoFilterMode = True Then ws.AutoFilterMode = False
            End If
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

Awesome, perfect! Thank you!
 
Upvote 0
So out of no where this macro has started giving me "Run-time error '1004': AutoFilter method of Range class failed.

I have included the spreadsheet and a screen shot of the highlighted error from Visual Basic here.
 
Upvote 0
Try this macro:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim ws As Worksheet
    Sheets("Held Appts 4 Submission").UsedRange.Offset(1, 0).ClearContents
    For Each ws In Sheets
        LastRow = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
        If WorksheetFunction.CountIf(ws.Range("F13:F" & LastRow), "Held") > 0 Then
            ws.Range("A13:F" & LastRow).AutoFilter Field:=6, Criteria1:="Held"
            ws.Range("A15:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Held Appts 4 Submission").Cells(Sheets("Held Appts 4 Submission").Rows.Count, "A").End(xlUp).Offset(1, 0)
            If ws.AutoFilterMode = True Then ws.AutoFilterMode = False
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
You have 2 macros with the same name. Either re-name or delete the macro in Module1 or delete Module1 entirely.
 
Upvote 0
Still getting same error. Deleted both modules, and then created a new one with the referred to code. I have re-uploaded the doc here with the updates I tried.


Try this macro:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim ws As Worksheet
    Sheets("Held Appts 4 Submission").UsedRange.Offset(1, 0).ClearContents
    For Each ws In Sheets
        LastRow = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
        If WorksheetFunction.CountIf(ws.Range("F13:F" & LastRow), "Held") > 0 Then
            ws.Range("A13:F" & LastRow).AutoFilter Field:=6, Criteria1:="Held"
            ws.Range("A15:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Held Appts 4 Submission").Cells(Sheets("Held Appts 4 Submission").Rows.Count, "A").End(xlUp).Offset(1, 0)
            If ws.AutoFilterMode = True Then ws.AutoFilterMode = False
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
You have 2 macros with the same name. Either re-name or delete the macro in Module1 or delete Module1 entirely.
 
Upvote 0
Click here. This file is working for me.

What updates did you try?
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,947
Members
449,480
Latest member
yesitisasport

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