开发者

VBA Excel Replace not always working

OK this is really bugging me now.

Eventually I want to add some ifs and else's to this code but for now I simply have the below code.

' resize string if nessuary  
sname = Replace(arr(x), "ASR Port Provisioning General Questions", "General Questions")  
sname = Replace(arr(x), "ASR Port Provisioning", "ASR PP")  
sname = Replace(arr(x), "ASR Standard Network Device Config Changes", "Std Config Change")  
If Len(sname) > 30 Then sname = Right(sname, 15)  

arr() has the elements

"ASR Port Provisioning General Questions", 
"ASR Port Provisioning",
"ASR Standard Network Device Config Change开发者_运维问答s"

Now for the "ASR Standard Network Device Config Changes" it works and gets shortened to "std Config Change"

But for the other two they get skiped and not replaced?

Now i know they contain the corrent info, I have even tried outputing arr(x) to form the seach string, but it still does not happen :)

OK seems it needed the tidy up, once wraped in if and elses

If InStr(arr(x), "ASR Port") Then

    If InStr(arr(x), "ASR Port Provisioning General Questions") Then
    sname = "General Questions"
    Else
    sname = Replace(arr(x), "ASR Port Provisioning", "ASR PP")
    End If

    Else
    sname = Replace(arr(x), "ASR Standard Network Device Config Changes", "Std Config Change")
End If

Work fine!! any one knwo whey three replace statments ogather stop each other working ?


In your first code block, the problem is that you are resetting the value of sname on each call and thus only the last one "sticks". You either need another array to store all the replaced values, or you could update the existing array, or you could do your work on each item after you do the replace. We would need to know more about what you are doing with the data.

In the second code block, ASR Port must not be in the string arr(x) and thus it only executes the Else statement.

I'm guessing you are trying to do something like:

If InStr(arr(x), "ASR Port Provisioning General Questions") Then
    sname = Replace(arr(x), "ASR Port Provisioning General Questions", "General Questions")  

ElseIf InStr(arr(x), "ASR Port Provisioning") Then
    sname = Replace(arr(x), "ASR Port Provisioning", "ASR PP")  

Else
    sname = Replace(arr(x), "ASR Standard Network Device Config Changes", "Std Config Change") 

End If
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜