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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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
 
Upvote 0
Solution
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!!!!
 
Upvote 0
@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
 
Upvote 0
As that's a completely different question, it needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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