VBA Excel Multiline String搜索 您所在的位置:网站首页 vba中worksheetfunction函数 VBA Excel Multiline String搜索

VBA Excel Multiline String搜索

2023-04-13 05:19| 来源: 网络整理| 查看: 265

问题描述

我想使用VBA Excel宏搜索文本文件中的多行字符串. 我尝试使用仪器功能,但它无法正常工作.我的确切目的是读取存储在单元格中的多行字符串,并检查是否在文本文件中可用.为此,我要做的就是将文本文件读取到一个变量中,将保存在单元格中的字符串读取到另一个变量,然后使用inter使用二进制比较进行比较.仪器会为多线字符串工作吗?如果没有其他比较的方法?

这是我的代码

Public Function string_compare() As String Dim strFilename As String Dim strSearch As String strFilename = "D:\test.txt" Dim strFileContent As String Dim iFile As Integer: iFile = FreeFile Open strFilename For Input As #iFile strFileContent = Input(LOF(iFile), iFile) Close #iFile strSearch = Sheet1.Cells(9, 1).Value If InStr(1, strFileContent, strSearch, vbBinaryCompare) > 0 Then MsgBox "success" Else MsgBox "failed" End If End Function

当我检查字符串时,两者似乎都是相同的.尽管字符串是相同的,但搜索结果总是失败.任何建议都会有所帮助.

推荐答案

蒂姆和Mrig建议我从文本中删除了CR和CRLF,如下所示.现在它的工作正常.我可以将其用于比较多线字符串.我在这里发布我的代码段.希望它也可以帮助其他人.

Public Function stringcompare(sourcefile As String, Workbookname As Worksheet) As String Dim strSearch As String Dim strFileContent As String Dim iFile As Integer: iFile = FreeFile Open sourcefile For Input As #iFile strFileContent = Input(LOF(iFile), iFile) Close #iFile strSearch = Workbookname.Cells(1, 1).Value strFileContent = Application.WorksheetFunction.Substitute(strFileContent, vbCrLf, "") strSearch = Application.WorksheetFunction.Substitute(strSearch, vbLf, "") If StrComp(strFileContent, strSearch, vbBinaryCompare) = 0 Then MsgBox "success" Else MsgBox "failed" End If End Function

本文地址:https://www.itbaoku.cn/post/2582967.html



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有