VBA Copy& Paste Based on Single Criteria

mdonovan890

New Member
Joined
Dec 22, 2016
Messages
24
Hello,
Below is the code that I am currently using to copy all the rows on each of the specified worksheets based upon a single criteria (Column N="Y"). The Marco then pastes these rows as values on Sheet 4. This code functions properly however, it is extremely slow. The verison of Excel that I working in is 32 BIT Excel 2016.I am hoping to get some suggestions on how to make this VBA run more efficiently. Many thanks!



Public Sub COPYOUSDATA()


Dim ws As Worksheet
Dim destinationWorksheet As Worksheet
Dim columnN As Range
Dim c As Range
Dim count As Long

Set destinationWorksheet = ActiveWorkbook.Worksheets("Sheet4")

destinationWorksheet.Cells.ClearContents

count = 1
For Each ws In ActiveWorkbook.Worksheets

If ws.Name = "{OUS Region A} Europe" Or ws.Name = "{OUS Region B} Asia-Pacific" Or ws.Name = "{OUS Region C} Latin America"

Set columnN = ws.Range("N:N") 'columnN
For Each c In columnN
If WorksheetFunction.IsText(c.Value) Then
If InStr(c.Value, "Y") > 0 Then
c.EntireRow.Copy
destinationWorksheet.Cells(count, 1).PasteSpecial xlPasteValuesAndNumberFormats
count = count + 1
End If
End If
Next c

End If

Next ws


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How about
Code:
Public Sub COPYOUSDATA()
Dim ws As Worksheet
Dim destinationWorksheet As Worksheet
Dim count As Long

Set destinationWorksheet = ActiveWorkbook.Worksheets("Sheet4")

destinationWorksheet.Cells.ClearContents

count = 1
For Each ws In ActiveWorkbook.Worksheets
   If ws.Name = "{OUS Region A} Europe" Or ws.Name = "{OUS Region B} Asia-Pacific" Or ws.Name = "{OUS Region C} Latin America" Then
      If Application.CountIf(ws.Range("N:N"), "Y") > 0 Then
         ws.Range("N:N").AutoFilter 1, "Y"
         ws.AutoFilter.Range.Offset(1).Copy
         destinationWorksheet.Cells(count, 1).PasteSpecial xlPasteValuesAndNumberFormats
         count = destinationWorksheet.Cells(Rows.count, 1).End(xlUp).Offset(1).Row
      End If
   End If
Next ws
End Sub
 
Upvote 0
Hello,
This code is only copying the information in column N, i would need all the data for from each row. Also after running, the tabs are showing as filtered and each one needs to be cleared out before new data can be entered. Is there a way to improve this code while still copying the entire row and automatically clearing out the any filtering that was done with the VBA? Thanks for your help.
 
Upvote 0
Try
Code:
Public Sub COPYOUSDATA()
Dim ws As Worksheet
Dim destinationWorksheet As Worksheet
Dim count As Long

Set destinationWorksheet = ActiveWorkbook.Worksheets("Sheet4")

destinationWorksheet.Cells.ClearContents

count = 1
For Each ws In ActiveWorkbook.Worksheets
   If ws.Name = "{OUS Region A} Europe" Or ws.Name = "{OUS Region B} Asia-Pacific" Or ws.Name = "{OUS Region C} Latin America" Then
      If Application.CountIf(ws.Range("N:N"), "Y") > 0 Then
         ws.Range("N:N").AutoFilter 1, "Y"
         ws.AutoFilter.Range.Offset(1).EntireRow.Copy
         destinationWorksheet.Cells(count, 1).PasteSpecial xlPasteValuesAndNumberFormats
         count = destinationWorksheet.Cells(Rows.count, 1).End(xlUp).Offset(1).Row
         ws.AutoFilterMode = False
      End If
   End If
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,638
Messages
6,173,494
Members
452,516
Latest member
druck21

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