Help with VBA code to filter, copy and paste in another worksheet

kevinh2320

Board Regular
Joined
May 13, 2016
Messages
61
I need some help with the code below. On my "Leases" worksheet I have data in columns A through F. I am filtering on the word "Question" in column F then coping those rows into the first available row on my "Question" work sheet columns A through F. The code works fine provided the word "Question" is somewhere on the sheet in column F. However, in cases where there is no "Question" entry the code fails because there isn't anything to paste. I think I need to add or modify the "If Statement" but, not sure how to do that.


Also, I'm sure there are other ways to tighten up this code. Would appreciate any help and suggestions.


Sub MoveTest()


'Move rows from Permit worksheet that contain the word "Question" - column F
Worksheets("Permits").Activate
With ActiveSheet
.AutoFilterMode = False
If Application.CountIf(.Range("F:F"), "*Question*") > 0 Then
With Range("F1", Range("F" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Question*"
.Offset(1).SpecialCells(12).EntireRow.Copy
End With
End If
.AutoFilterMode = False
End With
'Application.CutCopyMode = False


'Go to Questions worksheet and paste records in first available row
Worksheets("Question").Activate
Range("A1048576").Select
Selection.End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.Interior.Color = xlNone
Range("A1").Select


'Release copy mode from Permits worksheet
Worksheets("Permits").Activate
Application.CutCopyMode = False


End Sub

Thank you Thank you
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How about something like this:
Code:
If Application.CountIf(.Range("F:F"), "*Question*") > 0 Then
    With Range("F1", Range("F" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*Question*"
        .Offset(1).SpecialCells(12).EntireRow.Copy
    End With
Else
    Beep
    MsgBox "Question not found", vbInformation, "NO MATCH"
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,936
Latest member
almerpogi

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