首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在VBA中使用Substring和IndexOf方法

在VBA中使用Substring和IndexOf方法
EN

Stack Overflow用户
提问于 2018-01-18 07:16:11
回答 2查看 3.8K关注 0票数 1

我有以下代码:

代码语言:javascript
复制
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“。我怎么才能修复它?

EN

回答 2

Stack Overflow用户

发布于 2018-01-20 01:27:02

如果您关注的是VBA,而不是VB.Net,那么您需要使用Split()来引用拆分的阵列,使用OLEObjects()来引用OLEObjects。

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

代码语言:javascript
复制
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中。

票数 0
EN

Stack Overflow用户

发布于 2020-06-18 10:19:37

希望这能在未来为某人节省一些时间:

代码语言:javascript
复制
    ' 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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48311532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档