在VBA中匹配另一个工作表中的行

时间:2016-05-29 18:58:22

标签: excel vba macros

我正在尝试实现一个宏,该宏将根据活动工作表上的单元格值复制另一个工作表的结果。即循环遍历 GWworkStations()“ 中的每个元素,并在” “Col List”“ 的列B中找到匹配项i> sheet,然后将“ C:E” 中的相应值复制到数组 MathedEntries“ 中,以便我可以将它们复制回活动表。但是,下面的代码为“ matchedRow” 返回空,而不是报告行号。有人可以帮忙吗?我似乎无法弄清问题在哪里?运行此代码时,我没有收到错误。提前谢谢。

public static void runscript() throws IOException {
    Runtime rt = Runtime.getRuntime();
    String[] commands = { "/bin/bash", "-c", "node --version" };
    Process proc = null;
    try {
        proc = rt.exec(commands);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

    // read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    String s = null;
    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null) {
        System.out.println(s);
    }
}

1 个答案:

答案 0 :(得分:0)

Excel不喜欢工作表名称中的空格。您可以使用单引号来解决此问题:Range("'Col List'!B:B"),或将Range("Col List!B:B")替换为Sheets("Col List").Columns(2)

你也可以使用Range.Find方法(我更喜欢):

matchedRow = Sheets("Sheet 3").Columns(2).Find(str).Row