【发布时间】: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 如果您还没有这样做