Hi,
I'm trying to write some code that will loop through a column, and copy and paste specific data into a new sheet. So in the example below I want all rows the read "Navy" in the F column to be copy and pasted into a new sheet called Navy. The main issue I'm facing at the moment is the code skips the line ' navyexists = False'. If i put this part one line below instead I get the error: Next without For. Could anyone kindly help me work out the logic here? Thanks
I'm trying to write some code that will loop through a column, and copy and paste specific data into a new sheet. So in the example below I want all rows the read "Navy" in the F column to be copy and pasted into a new sheet called Navy. The main issue I'm facing at the moment is the code skips the line ' navyexists = False'. If i put this part one line below instead I get the error: Next without For. Could anyone kindly help me work out the logic here? Thanks
VBA Code:
Sub nikedata()
Dim c As Variant
Dim i As Integer
Dim Lastrow As Integer
Dim ws As Worksheet
Lastrow = Range("B" & Rows.Count).End(xlUp).Row
c = 0
navyexists = True
For i = 2 To Lastrow
If Range("F" & i) = "Navy" Then
c = c + 1
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Navy" Then [B][I][COLOR=rgb(26, 188, 156)]navyexists = False[/COLOR][/I][/B]
End If
Next ws
If navyexists = False Then
Sheets.Add.Name = "Navy"
Sheets("nike_data_2022_09").Range("F" & i).EntireRow.Copy
Sheets("Navy").Rows.Range("A" & c).PasteSpecial Paste:=xlValues
End If
If navyexists = True Then
Sheets("nike_data_2022_09").Range("F" & i).EntireRow.Copy
Sheets("Navy").Rows.Range("A" & c).PasteSpecial Paste:=xlValues
End If
Next i
End Sub