VBA macro if criteria is met copy cells to another worksheet

slohman

Board Regular
Joined
Mar 31, 2012
Messages
110
I have this macro that I found but it gives me an error, I have need it to start in cell B3 not B3 as I have merged headers in Rows A1:H2
VBA Code:
Sub Copy_Products()
Dim lastrow As Long, erow As Long

Application.ScreenUpdating = False

Worksheets("Swings").Activate
Range("A2:XDH700").ClearContents


'to check the last filled row on sheet named Product Info

lastrow = Worksheets("Product Info").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow

If Worksheets("Product Info").Cells(i, 27).Value = "Freestanding > Freestanding Swings, Play" Then

    Worksheets("Product Info").Cells(i, 3).Copy

    erow = Worksheets("Swings").Cells(Rows.Count, 3).End(xlUp).Row

    Worksheets("Product Info").Paste Destination:=Worksheets("Swings").Cells(erow + 1, 2)

    Worksheets("Product Info").Cells(i, 4).Copy

    Worksheets("Product Info").Paste Destination:=Worksheets("Swings").Cells(erow + 1, 3)

    Worksheets("Products").Cells(i, 5).Copy

    Worksheets("Products").Paste Destination:=Worksheets("Swings").Cells(erow + 1, 4)
    
    Worksheets("Products").Cells(i, 8).Copy

    Worksheets("Products").Paste Destination:=Worksheets("Swings").Cells(erow + 1, 5)
    
End If

Next i

Application.ScreenUpdating = True

End Sub

1684898552606.png
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
So, anything relates to row 2 should be 3?
VBA Code:
Range("A2:XDH700").ClearContents
become
VBA Code:
Range("A3:XDH700").ClearContents

and
VBA Code:
For i = 2 To lastrow
become
VBA Code:
For i = 3 To lastrow

???
 
Upvote 0
1684898991401.png

So, anything relates to row 2 should be 3?
VBA Code:
Range("A2:XDH700").ClearContents
become
VBA Code:
Range("A3:XDH700").ClearContents

and
VBA Code:
For i = 2 To lastrow
become
VBA Code:
For i = 3 To lastrow

???
I'm getting an error on this line
1684900873065.png
 
Upvote 0
May be:
VBA Code:
 Worksheets("Swings").Paste Destination:=Worksheets("Swings").Cells(erow + 1, 2)
 
Upvote 0
May be:
VBA Code:
 Worksheets("Swings").Paste Destination:=Worksheets("Swings").Cells(erow + 1, 2)
1684901511259.png


Sorry obviously didn't make myself clear I'm still getting the merge error and this is the line that turns yellow when I run the vba
 
Upvote 0
May be not only ClearContents, also ClearFormat (to clear merge cells)?
VBA Code:
With Range("A2:XDH700")
   .ClearContents
   .ClearFormats
End with
 
Upvote 0
I need A1:H2 to be merged, but nothing else is merged in my worksheet but I'm still getting the error the yellow highlighting is when I debug after the error
1684902477171.png
 
Upvote 0
Another problem I have found with the macro it is copying the formulas not values is there a way of doing this.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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