2nd For loop is not running, trying to filter by zip code copy and paste to new sheet

TeddyLu

New Member
Joined
Mar 12, 2015
Messages
2
I'm trying to take data from a previous sheet and trying to filter it by zip codes by going row by row. I'll copy the selected row that fit the right zip code and paste it onto a new sheet. However, my 2nd for loop is not running after the first. Since I'm new to VBA I'm not sure if I'm ending it right or not or there is a faster way by nesting but I can't think of a way. Explanation would be great to know what is going on.

Here is my code so far:

Code:
Sub Remove_Unwanted_Columns_and_Rows()
    
    Dim wb1 As Workbook 'Declarations
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet 'Add Last Worksheet
    Dim LastrowA As Integer
    
    Set ws2 = ActiveWorkbook.Sheets.Add(After:=Worksheets(Worksheets.Count))
    ws2.Name = "Reformed"
    
    Application.ScreenUpdating = False 'Turn off screen updating to increase performance
    
    LastrowA = ThisWorkbook.Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row 'Find the last row on active sheet

'BEGIN FILTERING
    '1st Filter
    
    Set ws3 = ActiveWorkbook.Sheets.Add(After:=Worksheets(Worksheets.Count))
    ws3.Name = "Filtered"
    
    Dim i As Integer 'counter
    
    Sheets("Filtered").Select
    Range("A1").Value = "1st Area"
    Range("A1").Font.Bold = True
    
    For i = 2 To LastrowA
        Sheets("Reformed").Select
        Range("F" & i).Select 'Select column F for Zips
        check_zip = ActiveCell
            If check_zip = "98745" Then
                ActiveCell.EntireRow.Copy 'Copy entire row
                Sheets("Filtered").Select
                RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row 'Count rows for new sheet
                Range("A" & RowCount + 1).Select
                ActiveSheet.Paste
                Sheets("Reformed").Select
            End If
    Next i

    '2nd Area Filter

    Sheets("Filtered").Select
    Area2 = Cells(Cells.Rows.Count, "A").End(xlUp).Row ' Count for placement for next Area Title
    Range("a" & Area2 + 1).Value = "2nd area"
    Range("a" & Area2 + 1).Font.Bold = True
    
    Sheets("Filtered").Select
    ActiveSheet.Columns.AutoFit
    
    For j = 2 To LastrowA
        Sheets("Reformed").Select
        Range("F" & i).Select
        check_zip2 = ActiveCell
            If check_zip2 = "96857" Then
                Active.Cell.EntireRow.Copy
                Sheets("Filtered").Select
                RowsCount2 = Cells(Cells.Rows.Count, "A").End(xlUp).Row
                Range("A" & RowCount2 + 1).Select
                ActiveSheet.Paste
                Sheets("Reformed").Select
            End If
    Next j
    
    Sheets("Filtered").Select
    Area3 = Cells(Cells.Rows.Count, "A").End(xlUp).Row ' Count for placement for next Area Title
    Range("a" & Area2 + 1).Value = "Local"
    Range("a" & Area2 + 1).Font.Bold = True
   
                
End Sub
 
Hi and welcome to the MrExcel Message Board.

I have not checked your code in detail but this line looks wrong:
Code:
Range("F" & i).Select
I think it should be j not i.
 
Upvote 0

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