使Excel VBA代码适用于所有行

时间:2014-06-05 14:44:11

标签: excel excel-vba vba

我已经创建了下面的语句,它正如我需要的那样有一个缺陷,它只适用于我的工作表的第二行(第一行包含标题行)。

如何使其适用于工作表中的所有行?工作表每天都有不同的行数。

Dim result As String
Adnet = Range("AB2").Value
TSNumber = Range("BQ2").Value

If (Adnet = "goog" Or Adnet = "bing" Or TSNumber = "8006522096" Or TSNumber = "8006522097" Or TSNumber = "8006522098") Then
    result = "PPC"
Else
    result = "None/Other"
End If

Range("AC2").Value = result
End Sub

1 个答案:

答案 0 :(得分:0)

怎么样:

Sub qwerty()
    Dim result As String, N As Long, i As Long
    N = Cells(Rows.Count, "AB").End(xlUp).Row
    For i = 2 To N
        Adnet = Range("AB" & i).Value
        TSNumber = Range("BQ" & i).Value
        If (Adnet = "goog" Or Adnet = "bing" Or TSNumber = "8006522096" Or TSNumber = "8006522097" Or TSNumber = "8006522098") Then
            result = "PPC"
        Else
            result = "None/Other"
        End If
        Range("AC" & i).Value = result
    Next i
End Sub