excel vba循环范围和if语句

时间:2014-12-11 15:58:40

标签: excel vba loops if-statement for-loop

我需要帮助循环遍历给定范围,然后是if语句。 我的范围是:

| A    | B     | C    |
-----------------------
|TPA   | C:\   | 1.doc|

|TPA   | C:\   | 2.doc|

|LAX   | D:\   | 3.doc|

我想循环通过A1:C3并且如果列A1:A3 =" TPA"然后取B1和C1的数据。

Dim test1 As Variant
Dim cell As Range
Dim cell1 As Range
cell = Worksheets("Sheet2").Range ("A1:C3")
cell1 = Worksheets("Sheet2").Range ("A1:A3")
For Each cell1 in cell
If test1 = "TPA" Then 
'MsgBox B1 and C1 But I need to MsgBox B2 and C2 as well
End If
Next

最终,我需要msgbox B1 + C1和B2 + C2。

1 个答案:

答案 0 :(得分:1)

未经测试......

Dim test1 As String, c As Range, myRng As Range
Dim Result as string

set myRng = Worksheets("Sheet2").Range("A1:A3")
For Each c in myRng
    If test1 = "TPA" Then 
        Result = Result & c & c.offset(0,1) & vbCrLf
    End If
Next c
MsgBox Result