无法准确地作出条件陈述

时间:2018-01-22 09:42:09

标签: vba excel-vba excel

我正在尝试创建一个宏,它将从工作表中获取值(有条件地)并在即时窗口中打印它。我希望我的宏只会选择0中只有Range("A1")的值,然后它会查找两列偏移量(在同一行中)以查看该字段是否为空。如果是,那么它将打印范围(“A1”)和范围(“B1”)的值。如果不满足条件,它将简单地忽略该行并转到下一行以获得正确的匹配,直到它达到范围“A4”)。我哪里错了?提前谢谢。

这就是我正在尝试的:

Sub testing()
   Dim cNo As String, cName As String

   For Each cel In Range("A1:A4")
           If cel.Value = 0 And cel.Offset(, 2) = "" Then
                cNo = cel.Value
                cName = Replace(cel.Offset(0, 1).Value, " ", "+")
            End If

            Debug.Print cNo, cName
    Next cel
End Sub

我正在尝试这个值:

ColumnA  ColumnB        ColumnC
0        HERSHE ST  
2816     SHERWICK ST    FRANKLIN JANICE
8034     ANTOINE DR     MALDONADO JESUS
0        ELLINGTON ST   

此宏正在打印以下内容(重复第一个直到另一个匹配):

0             HERSHE+ST
0             HERSHE+ST
0             HERSHE+ST
0             ELLINGTON+ST

我的预期输出:

0             HERSHE+ST


0             ELLINGTON+ST

1 个答案:

答案 0 :(得分:1)

正如我所说,不确定为什么要使用立即窗口,但要获得结果,您需要在If语句中包含结果并添加一个Else子句,如果条件不是,则插入一个空行满足。

int num_rows = 5, num_cols = 7;
// create Matrix filled with 0 (as you mentioned in Case1)
std::vector<std::vector<int>> Matrix(num_rows, std::vector<int>(num_cols, 0));
// Iterating over each row
for (auto it_row = Matrix.begin(); it_row != Matrix.end(); it_row++)
{
    // Getting each (i,j) element and assigning random value to it
    for (auto it_col = it_row->begin(); it_col != it_row->end(); it_col++)
    {
        *it_col = rand() % 100;
    }
}

// let's print the Matrix
for (auto it_row = Matrix.begin(); it_row != Matrix.end(); it_row++)
{
    for (auto it_col = it_row->begin(); it_col != it_row->end(); it_col++)
    {
        std::cout << *it_col << " ";
    }

    std::cout << std::endl;
}