VBA Cells.Replace更改整个电子表格

时间:2018-05-10 10:53:21

标签: excel vba excel-vba

SETUP:

使用ACCESS 2010 VBA在Excel中创建工作表。以下问题已经在处理导出的Excel文件;

问题:

我有以下代码(也试过各种代码),但遗憾的是它改变了每一个" - "为" _"在整个电子表格中,而不是仅在提到的单元格中。我不知道为什么会这样。

For i = 2 To k
If Cells(i, 15).Value Like "*" & "-" & "*" & ":" & "*" Then
Cells(i, 15).Replace What:="-", Replacement:="_" - changes all
Cells(i, 15).Replace What:=":", Replacement:="." - changes all
End If
Next i

非常感谢任何帮助。提前谢谢!

2 个答案:

答案 0 :(得分:0)

不确定为什么不起作用(我无法复制)。你能看出这是否有效吗?

For i = 2 To k
    If Cells(i, 15).Value Like "*" & "-" & "*" & ":" & "*" Then
        Cells(i, 15) = Replace(Cells(i, 15), "-", "_")
        Cells(i, 15) = Replace(Cells(i, 15), ":", ".")
    End If
Next i

答案 1 :(得分:0)

Op的代码似乎是Replace函数和Range.Replace的混合。方法

  

<强> Replace Function

     

返回:一个字符串,其中指定的子字符串已被指定次数的另一个子字符串替换。

     

语法:替换(表达式,查找,替换,[开始],[计数],[比较])

  

<强> Range.Replace Method (Excel)

     

返回:一个布尔值,指示指定范围内的单元格中的字符。使用此方法不会更改选择或活动单元格。

     

语法: Range.Replace(What,Replacement,[LookAt],[SearchOrder],[MatchCase],[MatchByte],[SearchFormat],[ReplaceFormat])

Replace函数需要Cell Cell Range.ReplaceRange.Replace方法可以应用于整个范围。但是,当Cells(i, 15).Replace What:="-", Replacement:="_" Cells(i, 15).Replace What:=":", Replacement:="." 方法应用于单个单元格时,将在整个工作表上执行替换。

......这就是这些行正在做的事情:

What

如果要替换的字符串(即With Range(Cells(2, 15), Cells(k, 15)) 'change as required .Replace What:="-", Replacement:="_", LookAt:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False .Replace What:=":", Replacement:=".", LookAt:=xlPart End With ),则不需要逐个单元地应用Range.Replace方法来进行验证,而是将其直接应用于整个范围。试试这段代码:

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

int main()
{
    std::ifstream men("resources/men_names.txt");
    std::ifstream women("resources/women_names.txt");
    std::ifstream renters("resources/renter_names.txt");

    std::vector<std::string> menNames;
    std::vector<std::string> womenNames;
    std::vector<std::string> renterNames;
    std::vector<std::string> knownRenters;
    std::vector<std::string> unknownRenters;

    std::string name;

    while (men >> name)
        menNames.push_back(name);

    men.close();

    while (women >> name)
        womenNames.push_back(name);

    women.close();

    while (renters >> name)
        renterNames.push_back(name);

    renters.close();

    std::vector<std::string>::iterator itMen;
    std::vector<std::string>::iterator itWomen;
    std::vector<std::string>::iterator itRenters;

    for (itRenters = renterNames.begin(); itRenters != renterNames.end(); itRenters++)
    {
        bool found = false;

        for (itMen = menNames.begin(); itMen != menNames.end(); itMen++)
        {
            if ((*itMen) == (*itRenters))
            {
                found = true;
                knownRenters.push_back((*itMen));
            }
        }
        if (!found)
        {
            for (itWomen = womenNames.begin(); itWomen != womenNames.end(); itWomen++)
            {   
                if ((*itWomen) == (*itRenters))
                {
                    found = true;
                    knownRenters.push_back((*itWomen));
                }
            }   
        }
        if (!found)
            unknownRenters.push_back((*itRenters));
    }

    std::cout << knownRenters.size() << '\n';
    std::cout << unknownRenters.size() << '\n';

    std::cin.get();

    return 0;
}