VBA IF Then Statement

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" ?

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Code[/TD]
[TD]City[/TD]
[TD]State[/TD]
[TD]Country[/TD]
[/TR]
[TR]
[TD]001[/TD]
[TD]New York[/TD]
[TD]N/A[/TD]
[TD]United States[/TD]
[/TR]
[TR]
[TD]002[/TD]
[TD]New York[/TD]
[TD]NY[/TD]
[TD]United States[/TD]
[/TR]
</tbody>[/TABLE]

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

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Code[/TD]
[TD]City[/TD]
[TD]State[/TD]
[TD]Country[/TD]
[TD]Note[/TD]
[/TR]
[TR]
[TD]001[/TD]
[TD]New York[/TD]
[TD]N/A[/TD]
[TD]United States[/TD]
[TD]Please Check[/TD]
[/TR]
</tbody>[/TABLE]



Thanks for the help.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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

Forum statistics

Threads
1,223,978
Messages
6,175,755
Members
452,667
Latest member
vanessavalentino83

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