我正在处理字符串并获取一些需要在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
为了说明起见,这个子函数调用了我这里没有包含的其他函数。就好像你对功能很好奇。
真的需要你提供一些有用的信息,因为如果你知道代码失败的地方,但是你什么也没说,这太令人厌烦了。 – jsotola
InStr返回0,因为指定的子字符串不在字符串中。使用'debug.print'来检查你的值 – jsotola
@jsotola我已经更新了它。这真的很好,但现在它一直返回0,并且无法返回作业字符串内的“Command Line:”关键字的位置 – JJJ