Copy entire row if cell contains specific text and paste values into others worksheets

edsonpaula

New Member
Joined
Mar 22, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello, I have a worksheet with many rows and I need to copy and paste the entire row (paste value) to other sheets based on the value in column F.
I’m running the code below, but I’m not able to remove the blanc lines in the target sheet and paste values only, can someone help with that?



VBA Code:
Sub macro1()

Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet
Dim Target1 As Worksheet

Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")
Set Target1 = ActiveWorkbook.Worksheets("Sheet3")

j = 1
For Each c In Source.Range("F1:F" & Cells(Rows.Count, 1).End(xlUp).Row)
If c = "Ações" Then Source.Rows(c.Row).Copy Target.Rows(j)
If c = "Outros" Then Source.Rows(c.Row).Copy Target1.Rows(j)
j = j + 1
Next c

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,468
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub macro1()

Dim c As Range
Dim Source As Worksheet
Dim Target As Worksheet
Dim Target1 As Worksheet

Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")
Set Target1 = ActiveWorkbook.Worksheets("Sheet3")

For Each c In Source.Range("F1:F" & Source.Cells(Rows.Count, 1).End(xlUp).Row)
   If c = "Ações" Then
      c.EntireRow.Copy
      Target.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   ElseIf c = "Outros" Then
      c.EntireRow.Copy
      Target1.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   End If
Next c
End Sub
 
Solution

edsonpaula

New Member
Joined
Mar 22, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub macro1()

Dim c As Range
Dim Source As Worksheet
Dim Target As Worksheet
Dim Target1 As Worksheet

Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")
Set Target1 = ActiveWorkbook.Worksheets("Sheet3")

For Each c In Source.Range("F1:F" & Source.Cells(Rows.Count, 1).End(xlUp).Row)
   If c = "Ações" Then
      c.EntireRow.Copy
      Target.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   ElseIf c = "Outros" Then
      c.EntireRow.Copy
      Target1.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   End If
Next c
End Sub
It worked perfect! Thank you very much!!!!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,468
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

edsonpaula

New Member
Joined
Mar 22, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

@Fluff let me ask you another stupid question.
I'd like to clean all those sheets before running this macro, I have created a private sub to delete the rows, and worked fine, but I'm having trouble making it run in all sheets in the range, any idea how can I accomplish it?

VBA Code:
Private Sub DeleteRows()
   
    Range("A3:AC3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.EntireRow.Delete
End Sub

Thank you very much for all the help!

Edson
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,468
Office Version
  1. 365
Platform
  1. Windows
As that's a completely different question, it needs a new thread. Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,129,383
Messages
5,635,942
Members
416,889
Latest member
dhegs

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
Top