Одним из простых способов было бы иметь список местоположений, которые вы хотите сохранить на отдельном листе, затем добавить дополнительный столбец к исходным данным и выполнить VLOOKUP
(или INDEX/MATCH
), затем отфильтровать иудалять.Для одного процесса, это то, что я бы сделал.
Для решения VBA, что-то вроде этого, но вам все еще нужен этот список допустимых мест на отдельном листе:
Sub setDeleteNonUsa()
'Assuming 4 columns:
'A: [names]
'B: [location]
'C: [gender]
'D: [email]
Dim wb As Workbook: Set wb = ActiveWorkbook 'Alternative options: ThisWorkbook or Workbooks("book name")
Dim wsData As Worksheet: Set wsData = wb.Worksheets("Sheet1")
Dim wsLocs As Worksheet: Set wsLocs = wb.Worksheets("Sheet2")
'Declare and set the variable to hold the last row values
Dim lRowData As Long: lRowData = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
Dim lRowLocs As Long: lRowLocs = wsLocs.Cells(wsLocs.Rows.Count, "A").End(xlUp).Row
'Declare and set an array to hold the current data (Column A:D, Sheet1)
Dim arrData As Variant: arrData = wsData.Range("A1:D" & lRowData) 'allocate the data to the array
'Declare and set an array to hold the acceptable locations (column A, Sheet2)
Dim arrLocs As Variant: arrLocs = wsLocs.Range("A1:A" & lRowLocs) 'allocate the locations to the array
'Process the data
Dim Rd As Long, Rl As Long
For Rd = UBound(arrData) To LBound(arrData) + 1 Step -1 'Iterate through each row of the data, skipping headings
For Rl = LBound(arrLocs) + 1 To UBound(arrLocs) 'Iterate through each row of the locs, skipping headings
If arrData(Rd, 2) = arrLocs(Rl, 1) Then Exit For 'location match, skip to next
If Rl = UBound(arrLocs) Then wsData.Cells(Rd, 1).EntireRow.Delete 'we got to last one, no location match
Next Rl
Next Rd
End Sub