2

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

目前,每次尝试搜索时都会出现类型不匹配错误(由于 find 函数的长度限制为 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;
        }
    }
}
4

1 回答 1

2

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

In short, if all your strings are unique for the first 255 characters, you don't need to worry about the rest.

You just need to truncate the string before you perform a find on it:

missingItem = Left(missingItem,255)

or alternatively:

missingItem = Right (missingItem,255)

Otherwise, if there are many duplicates of the first 255 characters, you could divide the string by 255 - then round up - then split the original string into seperate strings, search them individualy - does that help you?

EDIT:

Otherise, here is how you can check a string for equivalency for each cell in a range: 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

And yes I did pretend to type that as something important as my boss walked by. I don't think he was fooled though - since I just mashed it out on my keyboard.

于 2012-07-30T19:34:40.377 回答