类型不匹配可能是由于数组被错误解释

时间:2017-03-21 17:29:56

标签: vba microstation

我从这段代码中得到了类型不匹配,如果有人可以提供帮助,那将非常感激。它可能与我使用split命令的地方有关..我试图基本上抓住A2和B2然后A3和B3等等......把它放到另一个程序中(Microstation)

Sub Main()

Dim Find_text() As Variant
Dim Replace_text() As Variant
Dim objExcelApp As Object
Dim wb As Object


     Set objExcelApp = CreateObject("Excel.Application")
     Set wb = objExcelApp.Workbooks.Open("My path")

     Dim ws As Object
     Set ws = wb.Sheets(1)


Find_text = Split(ws.Range("A2:A628"))
Replace_text = Split(ws.Range("B2:B628"))


CadInputQueue.SendKeyin "MDL KEYIN FINDREPLACETEXT,CHNGTXT CHANGE DIALOGTEXT"

For i = 0 To UBound(Find_text)

        CadInputQueue.SendKeyin "FIND DIALOG SEARCHSTRING " & Find_text(i)

        CadInputQueue.SendKeyin "FIND DIALOG REPLACESTRING " & Replace_text(i)

        CadInputQueue.SendKeyin "CHANGE TEXT ALLFILTERED"

Next

End Sub

1 个答案:

答案 0 :(得分:4)

问题在于这两行:

Find_text = Split(ws.Range("A2:A628"))
Replace_text = Split(ws.Range("B2:B628"))

ws.Range("A2:A628").Valuews.Range("B2:B628").Value都不是字符串 - 因此您无法拆分它们。但是,您可以直接将它们分配给数组。

进行以下更改:

Dim Find_text As Variant '() not required
Dim Replace_text As Variant

...

Find_text = ws.Range("A2:A628").Value
Replace_text = ws.Range("B2:B628").Value

...

For i = 1 To UBound(Find_text)

        CadInputQueue.SendKeyin "FIND DIALOG SEARCHSTRING " & Find_text(i,1)

        CadInputQueue.SendKeyin "FIND DIALOG REPLACESTRING " & Replace_text(i,1)

        CadInputQueue.SendKeyin "CHANGE TEXT ALLFILTERED"

Next

当您将多单元格矩形范围的值指定给变体时,它将变为基于1的2维单阵列。这就是为什么循环现在以1开头,为什么Find_text(i)必须被Find_text(i,1)替换(对于Replace_text也是如此)。