C++ 到 VBA (Excel)

标签 c++ c vba excel

所以,基本上,在 Excel 中,我有 4 列数据(全部带有字符串)我想处理,并希望将结果放在另一列中,就像这样(不要介意方括号,它们只代表单元格) :

Line    Column1     Column2     Column3     Column4     Result
1:      [a]         [b]         [k]         [YES]       [NO]
2:      [a]         [c]         [l]         [YES]       [NO]
3:      [b]         [e]         []          [YES]       [NO]
4:      [c]         [e]         [f]         [NO]        [NO]
5:      [d]         [h]         [b]         [NO]        [NO]
6:      [d]         []          [w]         [NO]        [NO]
7:      [e]         []          []          [YES]       [NO]
8:      [j]         [m]         []          [YES]       [YES]
9:      [j]         []          []          [YES]       [YES]
10:     []          []          []          [YES]       [YES]

我希望数据经过的过程是这样的:

假设 CheckingLine 是我当前要为其计算结果值的行,而 CurrentLine 是我在给定时刻用于计算结果值的任何行(CheckingLine 除外)。

  1. 如果 Column4[CheckingLine] 为“NO”,结果为“NO”(很简单,不需要帮助);
    1. 示例:CheckingLine = 1 -> Column4[1] = "NO"-> Result = "NO";
  2. 否则,我想确保所有与 CheckingLine 共享公共(public)值的行(在 1 和 3 之间的任何列中)也将 Column4 设置为“YES”(即使没有 VBA,这样做也很简单 - 事实上,我首先在普通 Excel 中进行操作,然后意识到这不是我想要的)-如果发生这种情况,结果为"is";
    1. 示例:CheckingLine = 8 -> 只有共享值为“j” -> CurrentLine = 9 -> Column4[9] = "YES"-> Result = "YES";
    2. 这是棘手的部分:如果其中一行有任何值(同样,在 1 和 3 之间的任何列中)未与 CheckingLine 共享,我想完成整个过程(从 1 重新开始),但检查CurrentLine 代替。
      1. 示例:CheckingLine = 2,“a”与第 1 行共享,c 与第 4 行共享 -> CurrentLine = 1 -> Column4[1] = “YES”,但“b”和“k”不是与 CheckingLine 共享 -> CheckingLine' = 1 -> "b"与第 5 行共享 -> Column4[5] = "NO"-> Result = "NO";

我已经编写了相应的 C++ 代码(有效)(它可以用任何其他语言编写,C++ 正是我目前使用的语言)(并且代码没有以任何方式进行优化,因为它的目的是尽可能清楚地了解它的功能)(上表是运行它的实际结果):

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> column1, column2, column3, column4, contentVector;
unsigned int location, columnsSize;

void InsertInVector(std::string Content)
{
    if(Content == "")
    {
        return;
    }

    for(unsigned int i = 0; i < contentVector.size(); i++)
    {
        if(contentVector[i] == Content)
        {
            return;
        }
    }

    contentVector.push_back(Content);
}

std::string VerifyCurrentVector(unsigned int Start)
{
    std::string result = "";

    if(contentVector.size() == 0)
    {
        result = "YES";
    }
    else
    {
        unsigned int nextStart = contentVector.size();

        for(unsigned int i = 0; i < columnsSize; i++)
        {
            if(i != location)
            {
                for(unsigned int j = Start; j < nextStart; j++)
                {
                    if(column1[i] == contentVector[j])
                    {
                        InsertInVector(column2[i]);
                        InsertInVector(column3[i]);
                    }
                    else if(column2[i] == contentVector[j])
                    {
                        InsertInVector(column1[i]);
                        InsertInVector(column3[i]);
                    }
                    else if(column3[i] == contentVector[j])
                    {
                        InsertInVector(column1[i]);
                        InsertInVector(column2[i]);
                    }
                }
            }
        }

        if(nextStart == contentVector.size())
        {
            for(unsigned int i = 0; i < columnsSize; i++)
            {
                if(i != location)
                {
                    for(unsigned int j = 0; j < nextStart; j++)
                    {
                        if(column1[i] == contentVector[j] || column2[i] ==
                           contentVector[j] || column3[i] == contentVector[j])
                        {
                            if(column4[i] == "NO")
                            {
                                result = "NO";
                                return result;
                            }
                        }
                    }
                }
            }

            result = "YES";
        }
        else
        {
            result = VerifyCurrentVector(nextStart);
        }
    }

    return result;
}

std::string VerifyCell(unsigned int Location)
{
    std::string result = "";
    location = Location - 1;

    if(column4.size() < Location)
    {
        result = "Error";
    }
    else if(column4[location] == "NO")
    {
        result = "NO";
    }
    else
    {
        contentVector.clear();

        InsertInVector(column1[location]);
        InsertInVector(column2[location]);
        InsertInVector(column3[location]);

        result = VerifyCurrentVector(0);
    }

    return result;
}

void SetUpColumns(std::vector<std::string> &Column1, std::vector<std::string> &Column2,
                   std::vector<std::string> &Column3, std::vector<std::string> &Column4)
{
    if(Column4.size() > Column1.size())
    {
        for(unsigned int i = Column1.size(); i < Column4.size(); i++)
        {
            Column1.push_back("");
        }
    }
    if(Column4.size() > Column2.size())
    {
        for(unsigned int i = Column2.size(); i < Column4.size(); i++)
        {
            Column2.push_back("");
        }
    }
    if(Column4.size() > Column3.size())
    {
        for(unsigned int i = Column3.size(); i < Column4.size(); i++)
        {
            Column3.push_back("");
        }
    }

    column1 = Column1;
    column2 = Column2;
    column3 = Column3;
    column4 = Column4;
    columnsSize = Column4.size();
}

int main()
{
    std::vector<std::string> Column1, Column2, Column3, Column4;

    Column1.push_back("a");
    Column1.push_back("a");
    Column1.push_back("b");
    Column1.push_back("c");
    Column1.push_back("d");
    Column1.push_back("d");
    Column1.push_back("e");
    Column1.push_back("j");
    Column1.push_back("j");

    Column2.push_back("b");
    Column2.push_back("c");
    Column2.push_back("e");
    Column2.push_back("e");
    Column2.push_back("h");
    Column2.push_back("");
    Column2.push_back("");
    Column2.push_back("m");

    Column3.push_back("k");
    Column3.push_back("l");
    Column3.push_back("");
    Column3.push_back("f");
    Column3.push_back("b");
    Column3.push_back("w");

    Column4.push_back("YES");
    Column4.push_back("YES");
    Column4.push_back("YES");
    Column4.push_back("NO");
    Column4.push_back("NO");
    Column4.push_back("NO");
    Column4.push_back("YES");
    Column4.push_back("YES");
    Column4.push_back("YES");
    Column4.push_back("YES");

    SetUpColumns(Column1, Column2, Column3, Column4);
    std::cout << "Line\t" << "Column1\t" << "Column2\t" << "Column3\t" << "Column4\t" <<
        std::endl;

    for(unsigned int i = 0; i < Column4.size(); i++)
    {
        std::cout << i + 1 << ":\t" << "[" << column1[i] << "]\t[" << column2[i] <<
            "]\t[" << column3[i] << "]\t[" << column4[i] << "]\t[" << VerifyCell(i + 1)
            << "]" << std::endl;
    }

    return 0;
}

所以,经过这么长的解释,我想知道的是:

  • 有什么方法可以在 Excel 的 VBA 中执行此操作(或者甚至更好,在没有 VBA 的普通 Excel 中)?
  • 如果没有,我如何让我的代码(我可以轻松地将其翻译成另一种类 C 语言和/或优化)从 Excel 获取数据并将结果传送到 Excel?

最佳答案

Is there any way to do this in Excel's VBA?

是的,你当然可以用 VBA 做到这一点,它是一种完整而强大的编程语言

(or even better, in plain Excel without VBA)?

没有。在没有任何 VBA 代码的情况下,计算似乎太复杂,无法适应 Excel 公式。

If not, how can I have my code (which I can easily translate to another C-like language and/or optimise) get the data from, and deliver the results to, Excel?

您可以通过多种方式从 C++ 访问 Excel。使用 ATL 就是其中之一。另一种更简单的方法是以 CSV 格式导入/导出 Excel 文件,这种格式很容易从 C++ 解析和编写。 还要考虑 C#,它具有完整的 COM 互操作性来访问办公组件。

关于C++ 到 VBA (Excel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587005/

相关文章:

c - 傅里叶级数系数​​ gnuplot

Excel 触发宏

excel-2007 - 在二维 VBA 中循环遍历 Excel 中的单元格

c++ - 打印时指向常量的指针与指向非常量的指针的行为不同

vba - 根据收件人域更改电子邮件签名的 Outlook VBA?

c++ - OpenCV:如何使用 HOGDescriptor::detect 方法?

c++ - 如何使用 C++ 压缩文件目录?

c++ - 如何用C++编写二进制文件

c++ - 保留插入顺序的 C++ HashMap

c - 我的二进制搜索功能无法正常工作