code to copy/paste entire rows based on cell contents in column B

Reddog94

Board Regular
Joined
Dec 20, 2011
Messages
52
I'm trying to copy/paste rows (within a range of rows) based on the value in column B. If the value in column B is "Current Forecast", I would like to copy those entire rows and paste them below in the first available blank row.

Here is the code I have so far, but I keep getting various errors such as Variable not Defined on the first Target.

Can anyone see what I am missing? Thanks.


PHP:
Sub CopyPaste_CurrFCST()
    Dim nxtRow As Integer
    
    If Target.Column = 2 Then
        If Target.Value = "Current Forecast" Then
            nxtRow = Sheets(2).Range("B" & Rows.Count).End(xlUp).Row + 1
            Target.EntireRow.Copy _
            Destination:=Sheets(2).Range("A" & nxtRow)
        End If
    End If
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Reddog94,

If you need a sub to get all rows and copy over:
Code:
Sub CopyPaste_CurrFCST()
    
    With Intersect(ActiveSheet.UsedRange, Columns("B"))
        .AutoFilter 1, "Current Forecast"
        .Offset(1).EntireRow.Copy Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1)
        .AutoFilter
    End With
    
End Sub



How you had it setup was similar to a worksheet_change event. This is an example of what the code should look like for a worksheet_change event:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rngChg As Range
    Dim chgCell As Range
    Dim rngCopy As Range
    
    Set rngChg = Intersect(Target, Columns("B"))
    
    If Not rngChg Is Nothing Then
        For Each chgCell In rngChg
            If chgCell.Value = "Current Forecast" Then
                If rngCopy Is Nothing Then
                    Set rngCopy = chgCell
                Else
                    Set rngCopy = Union(rngCopy, chgCell)
                End If
            End If
        Next chgCell
        If Not rngCopy Is Nothing Then rngCopy.EntireRow.Copy Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    
End Sub
 
Upvote 0
Ah, so I have fixed the object defined problem. Now I am getting a Type Mismatch error for the second if statement (If Target.Value = "current forecast" Then.

Updated code:

PHP:
Sub CopyPaste_CurrFCST()
    Dim nxtRow As Integer
    Dim Target As Range
        Set Target = Range("Filter_Data")
    
    If Target.Column = 2 Then
        If Target.Value = "Current Forecast" Then
            nxtRow = Sheets(2).Range("B" & Rows.Count).End(xlUp).Row + 1
            Target.EntireRow.Copy _
            Destination:=Sheets(2).Range("A" & nxtRow)
        End If
    End If
End Sub
 
Upvote 0
The first code worked...halfway. I get run-time error 1004, selection not valid, now on the row that starts with .Offset(1).
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,901
Members
449,097
Latest member
dbomb1414

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