如果在其他页面上存在,则比较两个动态字段的vba

时间:2018-02-07 01:44:20

标签: excel vba excel-vba

Sub numbers()
Dim rngCell As Range
For Each rngCell In Sheets("Final").Range("F13:F" & rowLast)
    If WorksheetFunction.CountIf(Sheets("Filter").Range("A1:A" & rowLast), rngCell) <> 0 And _
       Range("I" & rngCell.Row).Value <= 0 And _
         Range("D" & rngCell.Row).Value < 50 Or _
          Range("D" & rngCell.Row).Value > 59 Then

       MsgBox "Please add correct number " & rngCell
       End
    End If

Next

End Sub

我有这段代码,但是我想从过滤页表B中设置这个比较值,而不是像if语句的第一行那样手动编写数字。

希望在这方面做出改变:

Range("D" & rngCell.Row).Value < 50 Or _
Range("D" & rngCell.Row).Value > 59 Then

可以使此过滤器比较列B中的值,如If WorksheetFunction.CountIf(Sheets("Filter").Range("A1:A" & rowLast), rngCell) <> 0 And _

欢迎任何建议!

1 个答案:

答案 0 :(得分:0)

Sub numbers()
Dim rngCell As Range
Dim lR_final, lR_filter As Long

lR_final = Worksheets("Final").Cells(Rows.Count, 6).End(xlUp).Row
lR_filter = Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Row

For Each rngCell In Sheets("Final").Range("F13:F" & lR_final)
    If WorksheetFunction.CountIf(Sheets("Filter").Range("A1:A" & lR_filter), rngCell) <> 0 And _
       Range("I" & rngCell.Row).Value <= 0 And _
         Range("D" & rngCell.Row).Value < WorksheetFunction.CountIf(Sheets("Filter").Range("B1:B" & lR_filter), rngCell) Or _
          Range("D" & rngCell.Row).Value > WorksheetFunction.CountIf(Sheets("Filter").Range("B1:B" & lR_filter), rngCell) Then

       MsgBox "Please add correct number " & rngCell
       End
    End If

Next

End Sub

试试这个并告诉我们它是否有效或是否有错误。