将值比较范围替换为excel中的另一个范围

时间:2016-05-19 12:02:25

标签: excel vba excel-vba

我在excel的两列中有数据,“a列”中的数据需要替换为另一个单值,可以说“Newdata”匹配“b列”中的数据,例如 4001024 列b中的 4000521 也存在于列a中,因此我需要将列a中的值替换为“newData”。

示例数据和输出如下所示。 我需要查看列B的所有值,列b包含aroudn 1000条目,列a包含超过5000个条目

原始数据预期输出 column a column b column a column b 4000520 4001024 4000520 4001024 4000520 4001204 4000520 4001204 4000520 4002475 4000520 4002475 4000521 4002477 newData 4002477 4000521 4002517 newData 4002517 4000521 4003062 newData 4003062 4000521 4000521 newData 4000521 4000521 newData 4000521 newData 4000521 newData 4000522 4000522 4000522 4000522 4001024 newData 4001024 newData

2 个答案:

答案 0 :(得分:1)

我会在C列中创建一个辅助列,并在单元格C2中使用此公式并将其向下拖动:

=IF(ISERROR(VLOOKUP(A2,B:B,1,FALSE)),A2,"NewData")

如果在列B中找到A列的值,则它将替换为“NewData”,否则它将显示为原始值。

然后,您可以在原始列A上复制C列和粘贴值。

答案 1 :(得分:1)

如果您使用VBA,代码可能是这样的:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

 public class Election2012 {

 public static void main(String[] args) throws FileNotFoundException
    {
    //declarations

         String [] statesEV = new String[52];
         String [] states = new String[52];
         String []  EV = new String [52];
         String [] tokens = new String[5];

    System.out.print("What is the EV file name? ");
    Scanner keyboard = new Scanner(System.in);
    String evFileName = keyboard.nextLine();

    System.out.print("What is the vote data file name? ");
    String dataFileName = keyboard.nextLine();

    // Scanner for the electoral votes.
    FileReader fileReader = new FileReader(evFileName);
    Scanner evScanner = new Scanner(fileReader);

    // Scanner for the election data.
    fileReader = new FileReader(dataFileName);
    Scanner dataScanner = new Scanner(fileReader);



    // we don't need the header.
    evScanner.nextLine(); // burn it.

    // while the file is not empty.
            int i = 0;
    while (evScanner.hasNext())
            {
        statesEV[i]=evScanner.nextLine();
                    tokens = statesEV[i].split(",");
                    states[i]=tokens[0];
                    EV[i]=tokens[1];
                    i++;

            }

    // print to be sure.
    for (i=0 ; i< states.length;i++ )
            {




            }
    // TODO: Go through and process the dataScanner to find the winner.

    // close all Scanners.
    keyboard.close();
    evScanner.close();
    dataScanner.close();
}

将此代码发布到单独的模块中。改变&#34; Sheet1&#34;到你的工作表的名称。