VBA Multiple If then statement help

jdb2313

New Member
Joined
Oct 19, 2018
Messages
7
Hello,
I am trying to modify this vba code that would find rows containing 20 products and cut/paste them onto another sheet. I tried to add an elseif to this code but it is not working. I am new to this so any help would be appreciated.


Sub CopyYes()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet


' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")


j = 1 ' Start copying to row 1 in target sheet
For Each c In Source.Range("a1:a1000000") ' Do 1000 rows
If c = "Product1" Then
Source.Rows(c.Row).Copy Target.Rows(j)
j = j + 1

End If
Next c
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Do you have a list of the 20 products somewhere in your workbook, perhaps on another sheet? If so, what is the name of that sheet and in which range are the products located? For example, Sheet2, A1:A20.
 
Upvote 0
This macro assumes that you have headers in row 1 and the data starts in row 2. Since you have your product list in column A of Sheet2, the data will be copied starting at row 21 of Sheet2.
Code:
Sub CopyYes()
   Dim Ary As Variant
   With Sheets("Sheet2")
      Ary = Application.Transpose(.Range("A2:A20").Value)
   End With
   With Sheets("Sheet1").Cells(1).CurrentRegion
      .AutoFilter 1, Ary, xlFilterValues
      Sheets("Sheet1").AutoFilter.Range.Offset(1).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
      Sheets("Sheet1").Cells(1).AutoFilter
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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