Excel.Range.Find使用长度超过255个字符的字符串

时间:2012-07-30 14:59:32

标签: c# excel

我在Excel中存储了每个1023个字符的字符串。稍后我需要能够搜索excel文件并找到值所在的行。我可以保证我正在查找的字符串中只出现一次将出现在工作簿中的某个位置。

目前每次尝试搜索时都会出现类型不匹配错误(由于查找功能的长度限制为255)。当我用像“teststring”之类的东西替换我的搜索字符串时,它工作正常。如何在Excel中搜索1023字符串?

foreach (string missingItem in missingItems)
{
    Range currentFind = null;
    foreach (Worksheet searchSheet in oWB.Worksheets)
    {
        Range lookAtRange = (Range)searchSheet.get_Range ("A1", "F20");

        currentFind = lookAtRange.Find (
            missingItem, 
            Missing.Value, 
            XlFindLookIn.xlValues,
            Missing.Value,
            Missing.Value, 
            XlSearchDirection.xlNext, 
            false, 
            false,
            Missing.Value);

        if (currentFind != null)
        {
            Range deleteRow = (Range)searchSheet.Rows[currentFind.Row];
            deleteRow.Delete (XlDirection.xlUp);
            break;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

http://www.vbaexpress.com/forum/archive/index.php/t-9179.html

简而言之,如果所有字符串对于前255个字符都是唯一的,则无需担心其余字符。

您只需要在执行查找之前截断字符串:

missingItem = Left(missingItem,255)

或者:

missingItem = Right (missingItem,255)

否则,如果前255个字符有很多重复,你可以将字符串除以255 - 然后向上舍入 - 然后将原始字符串拆分成单独的字符串,单独搜索它们 - 这对你有帮助吗?

编辑:

其他,以下是如何检查字符串中某个范围内每个单元格的等效性: http://www.vbaexpress.com/kb/getarticle.php?kb_id=167

Sub FindIt()
    Dim Cell As Range
    Dim SearchString As String
    For Each Cell In Range("A1:D500")
        If Cell.Value = "dqwlokjdowqijowqijdoinowiqhdoiwqiophiruegnqpiunrgpiuqgnrgkjffndskfiougpiodghiudhfgtothisansdfldkjflsdffjlksdjflksjfdoiejwfoiwjeoinfoinewoifjwoiejfoiwejfoiwejfoijjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjwerkjwelkfjwelkjflkwejlfkjwelkjflwkjeflknweoifnoweinfoinewfoinoifwnwoienfoinweoddddddddddddddddddddddddddddddddddddddddfinwoioiefldkjsfoijoneiojfoijfewwefweeeeeeeeeeeefwef" Then '<< use "Like" for wildcards in If-Then statements
            MsgBox "An ''it'' was found at " & Cell.Address & " (" & Cell & ")"
        End If
    Next Cell
End Sub

Funny Search String

是的,我确实打算把它当作我老板走过的重要事项。我不认为他被愚弄了 - 因为我只是把它捣碎在键盘上。