【问题标题】:Visual Basic If Then errorVisual Basic If Then 错误
【发布时间】:2015-08-17 04:28:56
【问题描述】:

我有一个 VB 项目,我正在尝试使用一些 VBA 代码来定位基于 Excel 表的第一列的重复行,并创建一个“1”标志,但是在 VB 中使用时 VBA 代码给出了错误:

InvalidCastException 未处理 重载解析失败,因为无法使用以下参数调用 Public '': '公共共享运算符 (a As String, b As String) As Boolean': 参数匹配参数 'a' 无法从 '__ComObject' 转换为 'String'。

Public Sub btnRun_Click(sender As System.Object, e As System.EventArgs) Handles btnRun.Click

    Dim xlApp As Excel.Application
    Dim xlWorkBook1 As Excel.Workbook ' Interactions
    Dim xlWorkBooks As Excel.Workbooks
    Dim MainSheet1 As Excel.Worksheet

    xlApp = New Excel.Application
    xlWorkBooks = xlApp.Workbooks
    xlWorkBook1 = xlWorkBooks.Open(File1_name)
    xlApp.Visible = False

    MainSheet1 = xlWorkBook1.Sheets(1)
    MainSheet1.Activate()

    Dim InteractionRows As Long = MainSheet1.UsedRange.Rows.Count ' Total number of rows in the Interaction worksheet
    Dim Duplicate_Found_Index As Long ' Stores all match index values for duplicate interactions
    Dim Duplicate_ID As Long ' Defining the first column to loop through and match duplicate interactions

    ' **** Duplicate Interaction ****
    ' Flag Creation
    For Duplicate_ID = 1 To (InteractionRows)
        If MainSheet1.Cells(Duplicate_ID, 1) <> "" Then 'checking if the cell is having any item, skipping if it is blank.
            Duplicate_Found_Index = xlApp.WorksheetFunction.Match(MainSheet1.Cells(Duplicate_ID, 1), MainSheet1.Range("A1:A" & InteractionRows), 0) 'getting match index number for the value of the cell.
            If Duplicate_ID <> Duplicate_Found_Index Then 'if the match index is not equals to current row number, then it is a duplicate value
                MainSheet1.Cells(Duplicate_ID, 34) = "1" ' Print the flag "1" in the 34th column if duplicate found
            Else
                MainSheet1.Cells(Duplicate_ID, 34) = "0" ' Print the flag "0" in the 34th column if no duplicate
            End If
        End If
    Next
End Sub

任何帮助都将非常感谢您了解为什么会发生此错误

【问题讨论】:

  • 在哪一行抛出异常?
  • Switch Option Strict On 如果您还没有这样做

标签: vb.net excel


【解决方案1】:

当单元格为空白时,调用 MainSheet1.Cells(Duplicate_ID, 1) 给出 Nothing (null)。所以不要将它与字符串匹配,而是检查它的空值。

If MainSheet1.Cells(Duplicate_ID, 1) IsNot Nothing Then

End If

【讨论】:

  • 干杯,我已经尝试了 IsNot 建议。到目前为止看起来不错,但是我会做更多的测试,然后与 IsNot Nothing 进行比较。
  • 我有一段类似的代码,它根据多列查找重复项。更复杂一点并引发不同的错误。我也打算发一下。
【解决方案2】:

使用IsNot 代替&lt;&gt;

            If Duplicate_ID IsNot Duplicate_Found_Index Then 'if the match index is not equals to current row number, then it is a duplicate value
                MainSheet1.Cells(Duplicate_ID, 34) = "1" 
            Else
                MainSheet1.Cells(Duplicate_ID, 34) = "0" 
            End If

【讨论】:

  • 除了我认为解释器反对的&lt;&gt; 运算符不是这个,而是将MainSheet1.Cells(Duplicate_ID, 1) 与空字符串进行比较的那个。 Long 值没有问题,但字符串有问题。
  • 这是不正确的 Duplicate_IDLong(值类型),所以 &lt;&gt; 是正确的运算符。 IsNot 适用于引用类型
  • 所以应该是 什么都没有?
  • 实际上。我已经使用“IsNot Nothing”对此进行了测试,并且效果很好。重复项被标记,程序成功运行。
  • 实际上,在尝试解决其他问题时,我已经打开了 Option Strict On,现在同样的代码正在抛出 COMException was unhandled
猜你喜欢
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多