VBA字符串与For循环和StrComp匹配

时间:2015-11-03 23:48:08

标签: vba excel-vba excel

我正在尝试使用for循环将列中的单元格更改为" truck"如果条件得到满足。

使用下面的代码,有些行不正确地通过循环。 例如:让我们说密尔沃基是出发城市,而多拉是到达的地方,它仍然会放入一辆卡车"在第13栏。

任何人都可以提供如何修复的方向吗?

Sub VBAtest()

'Declare Variables
Dim Departure As Integer
Dim Arrival As Integer
Dim row As Integer
Dim sh As Worksheet
Dim k As Long

'Initialize Variables
Set sh = ThisWorkbook.ActiveSheet
Departure = 4
Arrival = 8    

'Count Rows and Store in Variable
k = sh.Range("A1", sh.Range("A1").End(xlDown)).Rows.Count

'For Loop that is putting "Truck" in the wrong places
For row = 2 To k
    If StrComp(Cells(Arrival, row), "Doral") = 1 Then
        If StrComp(Cells(Departure, row), "Albany") = 1 Then
            Cells(row, 13) = "truck"
        End If
    End If
Next row

End Sub

0 个答案:

没有答案