У меня есть рабочий лист, в котором я управляю каким-то списком тарифов. Он имеет два листа. Выходной лист выглядит следующим образом.
![enter image description here](https://i.stack.imgur.com/QUluJ.png)
Всего столбцов 18
. Столбец от K
до Z
и содержит прайс-лист. Но в этих столбцах есть много ячеек, которые содержат No price
значение вместо цены в $
.
Я хочу отфильтровать по одному столбцу и скопировать все строки, содержащие No price
, на другой лист. Я написал очень простой макрос, используя несколько операторов if, но я не получаю требуемый вывод. Может кто-нибудь, пожалуйста, помогите мне с этим?
Код ниже.
Sub FilterNoPrice()
Dim myRange As Range
Dim myRow As Variant '### NOTE THIS CHANGE!
Sheets("Output").Select
Set myRange = Range("K3:K10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("K:K").AutoFilter Field:=1, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
'MsgBox "Not found!"
End If
Set myRange = Range("L3:L10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("L:L").AutoFilter Field:=2, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
'MsgBox "Not found!"
End If
Set myRange = Range("M3:M10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("M:M").AutoFilter Field:=3, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("N3:N10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("N:N").AutoFilter Field:=4, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("O3:O10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("O:O").AutoFilter Field:=5, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("P3:P10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("P:P").AutoFilter Field:=6, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("Q3:Q10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("Q:Q").AutoFilter Field:=7, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("R3:R10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("R:R").AutoFilter Field:=8, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("S3:S10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("S:S").AutoFilter Field:=9, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("T3:T10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("T:T").AutoFilter Field:=10, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("U3:U10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("U:U").AutoFilter Field:=11, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("V3:V10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("V:V").AutoFilter Field:=12, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("W3:W10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("W2:W10000").AutoFilter Field:=13, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("X3:X10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("X:X").AutoFilter Field:=14, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("Y3:Y10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("Y:Y").AutoFilter Field:=15, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
Set myRange = Range("Z3:Z10000")
myRow = Application.Match("No price", myRange, False)
If Not IsError(myRow) Then
ActiveSheet.Range("Z:Z").AutoFilter Field:=16, Criteria1:="No price"
' and then select/activate the cell:
'Application.GoTo Cells(1, myRow)
Else
' The value is not found in the range, so inform you:
MsgBox "Not found!"
End If
End Sub