unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Guys,

Is there any way where I can copy the whole row if the country is United States and the State is "N/A" ?

CodeCityStateCountry
001New YorkN/AUnited States
002New YorkNYUnited States

<tbody>
</tbody>

I want to copy the data in another tab like this and header will be pasted starting in F1 (code):

CodeCityStateCountryNote
001New YorkN/AUnited StatesPlease Check

<tbody>
</tbody>



Thanks for the help.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try
Code:
Sub movena()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lr1 As Long
Dim lr2 As Long


Set ws1 = Sheets("sheet1")
Set ws2 = Sheets("sheet2")
lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row


ws2.Range("F1") = "Code"
ws2.Range("G1") = "City"
ws2.Range("H1") = "State"
ws2.Range("I1") = "Country"
ws2.Range("j1") = "Note"


lr2 = ws2.Cells(Rows.Count, "f").End(xlUp).Row


For x = 2 To lr1
    If UCase(ws1.Cells(x, 4)) = "UNITED STATES" And UCase(ws1.Cells(x, 3)) = "N/A" Then
        ws1.Range("A" & x & ":D" & x).Copy ws2.Range("F" & lr2 + 1)
        ws2.Cells(lr2 + 1, "J") = "Please Check"
        lr2 = ws2.Cells(Rows.Count, "f").End(xlUp).Row
    End If
    
Next x


End Sub
 
Upvote 0
Try this

Code:
Sub Macro2()
    Sheets("Sheet1").Range("A1").AutoFilter 3, "N/A"
    Sheets("Sheet1").Range("A1").AutoFilter 4, "United States"
    Sheets("Sheet1").AutoFilter.Range.Copy Sheets("Sheet2").Range("F1")
    Sheets("Sheet1").ShowAllData
End Sub
 
Upvote 0
Try
Code:
Sub movena()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lr1 As Long
Dim lr2 As Long

Set ws1 = Sheets("sheet1")
Set ws2 = Sheets("sheet2")
lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row

[B][COLOR="#FF0000"]ws2.Range("F1") = "Code"
ws2.Range("G1") = "City"
ws2.Range("H1") = "State"
ws2.Range("I1") = "Country"
ws2.Range("j1") = "Note"[/COLOR][/B]

lr2 = ws2.Cells(Rows.Count, "f").End(xlUp).Row

For x = 2 To lr1
    If UCase(ws1.Cells(x, 4)) = "UNITED STATES" And UCase(ws1.Cells(x, 3)) = "N/A" Then
        ws1.Range("A" & x & ":D" & x).Copy ws2.Range("F" & lr2 + 1)
        ws2.Cells(lr2 + 1, "J") = "Please Check"
        lr2 = ws2.Cells(Rows.Count, "f").End(xlUp).Row
    End If
    
Next x

End Sub

You might find it interesting to know that the five highlighted lines of code above can be replaced by this single line of code...
Code:
ws2.Range("F1:J1") = Array("Code", "City", "State", "Country", "Note")
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,432
Messages
6,119,468
Members
448,900
Latest member
Fairooza

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