【问题标题】:VBA "Match" with string returning error 2042VBA“匹配”字符串返回错误2042
【发布时间】:2018-02-23 00:08:35
【问题描述】:

我开始编写一个代码,该代码将打开一个 csv 文件,查找特定列,然后将它们复制并粘贴到主 Excel 工作簿中。

csv 文件将有各种不同的标题,因为它们是自动生成的。在尝试复制任何信息之前,我想验证 csv 文件以确保存在所有必要的数据。

Dim WebImp As Workbook
Dim BackLog As String
Dim col As Long, res As Variant
Dim SearchValue As String

Private Sub CommandButton1_Click()

planned:
    MsgBox "Open a valid Web Backlog export.", vbExclamation, "Web Import"

    Application.FileDialog(msoFileDialogFilePicker).Show
    BackLog = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
    Set WebImp = Workbooks.Open(BackLog)
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    WebImp.Sheets(1).Activate
    SearchValue = "Summary"
    res = Application.Match(SearchValue, WebImp.Sheets(1).Rows(1), 0)
    If IsError(res) Then
        WebImp.Close
        MsgBox "File missing necessary data.", vbExclamation, "Missing Data"
        GoTo planned
    End If


End Sub

这是标题的示例。突出显示的是我要查找的列,但不能保证它将来会出现在 A 列中。

目前,我的代码默认为

错误 2042

并确定文件不正确,即使存在带有“摘要”的单元格。

我写的语法有什么问题? 为什么不能正确抓取? 有哪些潜在的解决方案?

【问题讨论】:

  • 你应该使用WebImp.Sheets(1).Rows(1) 而不仅仅是Rows(1)
  • 刚刚仔细检查,单元格“摘要”中没有空格
  • Len([A1]) 是什么?您确定该单元格中没有其他非打印字符吗?
  • @DavidZemens 他正在打开一个 CSV,所以应该只有一张纸?
  • Application.Match("*" & SearchValue & "*", WebImp.Sheets(1).Rows(1), 0) 有效吗?如果是这样,则单元格中有一些额外的字符。

标签: vba excel


【解决方案1】:

除非有额外的非打印字符(您可以通过测试Len(WebImp.Sheets(1).Range("A1") 来识别,否则代码应该可以工作。

另一种验证方法是使用Range.Find 方法。

Dim r as Range
Set r = WebImp.Sheets(1).Rows(1)
If r.Find("Summary", LookAt:=xlWhole) Is Nothing Then
    If r.Find("Summary") Is Nothing Then
        MsgBox "Summary doesn't exist in this row"
    Else
        MsgBox "Summary exists, with additional non-printed characters."
    End If
End If

在尝试复制任何信息之前,我想验证 csv 文件以确保所有必要的数据都存在。

您也可以使用流阅读器来验证 csv 的内容。

Function FileContainsSummary(filepath$)
' returns the index position of "Summary" in row 1 of the CSV file
'filepath = "C:\debug\test.csv"
Dim contents As String
Dim headers() As String
Dim i As Long
Dim ret
Dim ff As Long

ff = FreeFile()

Open filepath For Binary As ff
contents = Space$(LOF(ff))
' reads the entire file contents:
'Get ff, , contents

'reads only the first line:
Line Input #ff, contents
Close ff

'~~ Let's see if "Summary" exists at all:
'   You can remove this block after debugging.
If InStr(contents, "Summary") Then
    MsgBox "Exists!"
End If

' ~~ find it's position
headers = Split(contents, ",")

' default value of -1, will return if Summary not found.
ret = -1

For i = LBound(headers) To UBound(headers)
    If headers(i) = "Summary" Then
        ret = i + 1
        GoTo EarlyExit
    End If
    If InStr(headers(i), "Summary") Then
        MsgBox "WARNING: Found, but with additional characters."
        ret = i + 1
        GoTo EarlyExit
    End If
Next

EarlyExit:
    FileContainsSummary = ret
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    相关资源
    最近更新 更多