2017-08-09 117 views
0

我正在处理字符串并获取一些需要在Excel中复制的字符。它工作得很好,但是当我的Windows PC需要重新启动更新并再次打开时,代码不再工作。我不知道这是否与被禁用的宏有关,或者我需要配置一些东西。真的需要这个帮助,因为手工粘贴每个单元格的细节非常累人。VBA中的InStr函数Excel返回0即使指定的弹簧出现在字符串中

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 

为了说明起见,这个子函数调用了我这里没有包含的其他函数。就好像你对功能很好奇。

+0

真的需要你提供一些有用的信息,因为如果你知道代码失败的地方,但是你什么也没说,这太令人厌烦了。 – jsotola

+0

InStr返回0,因为指定的子字符串不在字符串中。使用'debug.print'来检查你的值 – jsotola

+0

@jsotola我已经更新了它。这真的很好,但现在它一直返回0,并且无法返回作业字符串内的“Command Line:”关键字的位置 – JJJ

回答

1

这是你的答案,我张贴在早期

InStr函数将返回0,因为指定的子字符串不是字符串中出现的意见。使用debug.print来检查你的值

+0

是的,因为我非常肯定字符串真的存在,所以我没有检查和调试。 hehehhehhhe – JJJ