即使在字符串中出现指定的Spring,VBA Excel中的InStr函数也会返回0

时间:2017-08-09 06:45:31

标签: excel vba excel-vba

我正在处理字符串并获取一些我需要在Excel中复制的字符。它工作得很好但是当我的Windows PC需要重新启动以进行更新并再次打开它时,代码不再起作用了。我不知道这是否与被禁用的宏或我需要配置的东西有关。真的需要这个帮助,因为手动粘贴细胞细节是非常累人的。

Sub appendDetails()
    Dim jobs As String
    Dim CommandLine As String

    'Helper Variables
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer

    'Gets all the job description
    jobs = Cells(2, "B").Value

    If (jobs <> "") Then
     'Get CommandLine My issue is InStr(jobs, "Command Line : ") is returning 0 when in fact "Command Line : " keyword appears inside job string
     If (InStr(jobs, "Command Line : ") = 0) Then
         ActiveCell.Value = ""
         ActiveCell.Offset(0, 1).Activate

     Else
         x = getPosition(jobs, "Command Line : ") + thisPosition("Command Line : ")
         y = getPosition(jobs, "Host/Host Group : ")
         z = getLengthOfString(x, y)
         CommandLine = getString(jobs, x, z)
         ActiveCell.Value = Trim(Replace(CommandLine, Chr(10), ""))
         ActiveCell.Offset(0, 1).Activate
         MsgBox ("Command Line : " + CommandLine)
     End If
  End Sub

为了澄清,这个子函数调用了我未包含的其他函数。就好像你对这些功能感到好奇一样。

1 个答案:

答案 0 :(得分:1)

这是我在之前的评论中发布的答案

InStr返回0,因为字符串中没有出现指定的子字符串。使用debug.print来检查你的值