在VBA

时间:2018-01-17 23:16:11

标签: vba excel-vba excel

我有以下代码:

Dim Op1 As String
Dim Op2 As String
Dim operatore As String
operatore = ComboBox1.Value
Op1 = operatore.Substring(0, operatore.IndexOf("<")).Trim()
Op2 = operatore.Split(">")(1)

当我编译时,我得到错误:“限定符无效”指的是变量“operatore”。我该如何解决?

2 个答案:

答案 0 :(得分:0)

如果你关注的是VBA,而不是VB.Net,那么你需要Split()来引用分割数组和OLEObjects()来引用OLEObjects。

这会编译并返回您可能需要的内容:

Option Explicit
Sub TestMe()

    Dim Op1 As String
    Dim Op2 As String
    Dim operatore As String

    operatore = ActiveSheet.OLEObjects("ComboBox1").Object
    Op1 = Split(operatore, "<")(0)
    Op2 = Split(operatore, ">")(1)

End Sub

在VBA IndexOf中不存在。它只在VB.NET中使用 - MSDN IndexOf

答案 1 :(得分:0)

希望这可能在将来节省一些时间:

    ' indexof
    Dim positionInPath As Integer
    positionInPath = InStr("C:\Users\2200522\Documents\", "2200522") ' returns 10
    ' C:\Users\2200522\Documents\
    ' ^^^      ^      
    ' 123      10     
    
    ' substring 
    Dim newPath As String
    newPath = Left("C:\Users\2200522\Documents\", 10) ' returns "C:\Users\2"