m trying to use one VBA code which filters the city and paste the data accordingly in another sheet with reference to some column names
M trying to make a long list in array but it not taking a value more than 35, below is the mentioned code
Sub CopyDataByCity()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim citiesToFilter() As Variant
Dim city As Variant
Dim headerRow As Range
Dim currentRow As Long
' Set the source sheet
Set sourceSheet = ThisWorkbook.Sheets("Portfolio Summary")
' Set the target sheet
Set targetSheet = ThisWorkbook.Sheets("RAW DATA")
' Define the list of cities to filter on in the desired order
citiesToFilter = Array("Jacksonville, FL", "Houston-The Woodlands-Sugar Land, TX", "Seattle-Tacoma-Bellevue, WA", "Minneapolis-St. Paul-Bloomington, MN-WI", "San Antonio-New Braunfels, TX", "Portland-Vancouver-Hillsboro, OR-WA", "Philadelphia-Camden-Wilmington, PA-NJ-DE-MD", "Boston-Cambridge-Newton, MA-NH", "Nashville-Davidson--Murfreesboro--Franklin, TN", "Columbus, OH", "Indianapolis-Carmel-Anderson, IN", "Raleigh-Cary, NC", "Cincinnati, OH-KY-IN", "Richmond, VA", "Urban Honolulu, HI", "Greensboro-High Point, NC", "Sacramento-Roseville-Folsom, CA", "Colorado Springs, CO", "Stockton, CA", "San Jose-Sunnyvale-Santa Clara, CA", "Harrisburg-Carlisle, PA", "San Francisco-Oakland-Berkeley, CA", "Louisville/Jefferson County, KY-IN", "Reno, NV", "Lakeland-Winter Haven, FL", "San Diego-Chula Vista-Carlsbad, CA", "Fort Collins, CO", "Salt Lake City, UT", "Boulder, CO", "Kansas City, MO-KS", "Baltimore-Columbia-Towson, MD", "Memphis, TN-MS-AR", "Tallahassee, FL", "North Port-Sarasota-Bradenton, FL", "Tucson, AZ")
' Find the last row of data in the source sheet
lastRow = sourceSheet.Cells(Rows.Count, "A").End(xlUp).Row
' Get the header row
Set headerRow = sourceSheet.Range("6:6")
' Set the starting row in the target sheet
currentRow = 2 ' Assuming the header row is in row 1
' Loop through the cities to filter on in the desired order
For Each city In citiesToFilter
' Loop through the rows in the source sheet
For i = 8 To lastRow ' Assuming the header row is in row 7
' Check if the city name in column Q matches the current city
If sourceSheet.Cells(i, "Q").Value = city Then
' If the city name matches, copy the specified columns and paste into the target sheet
targetSheet.Range("A" & currentRow).Value = sourceSheet.Cells(i, "C").Value ' Copy column
targetSheet.Range("B" & currentRow).Value = sourceSheet.Cells(i, "H").Value ' Copy column
targetSheet.Range("C" & currentRow).Value = sourceSheet.Cells(i, "D").Value ' Copy column
targetSheet.Range("D" & currentRow).Value = sourceSheet.Cells(i, "S").Value ' Copy column
targetSheet.Range("E" & currentRow).Value = sourceSheet.Cells(i, "I").Value ' Copy column
targetSheet.Range("F" & currentRow).Value = sourceSheet.Cells(i, "R").Value ' Copy column
targetSheet.Range("G" & currentRow).Value = sourceSheet.Cells(i, "W").Value ' Copy column
targetSheet.Range("H" & currentRow).Value = sourceSheet.Cells(i, "N").Value ' Copy column
targetSheet.Cells(currentRow, "I").Value = city ' Paste the city name to column I in the target sheet
currentRow = currentRow + 1 ' Increment the current row variable
End If
' Check if the next row is blank or if the city changes
If sourceSheet.Cells(i + 1, "A").Value = "" Or sourceSheet.Cells(i + 1, "Q").Value <> city Then
' If the next row is blank or if the city changes, skip over it
i = i + 1
End If
Next i
Next city
End Sub