1

I have a string that is read from a cell in Excel, which is something like this:

""Horace Lai" & vbNewLine & Date"

or this

"Chr(149) & "level 1" & vbNewLine & Chr(9) & Chr(149) & "level 2" & Chr(149) & "level 1" & vbNewLine & Chr(9) & Chr(149) & "level 2" & vbNewLine & Chr(9) & Chr(9) & Chr(149) & "level 3""

I would like to be able to evaluate these strings so that ""Horace Lai" & vbNewLine & Date" becomes:

Horace Lai 2014-06-20

So far I have tried ScriptControl without success. It doesn't seem to like it when I have double quotes for the string.

Sub testing()

Dim sc As ScriptControl
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"

MsgBox sc.Eval(""Horace Lai" & vbNewLine & Date")

End Sub

The MsgBox line becomes red and I cannot run it.

Any ideas? Thanks -Horace

4

2 回答 2

0

您指定JScript,而不是:

Dim test As String, result As String
test = "Chr(149) & ""level 1"" & vbNewLine & Chr(9) & Chr(149) & ""level 2"" & Chr(149) & ""level 1"" & vbNewLine & Chr(9) & Chr(149) & ""level 2"" & vbNewLine & Chr(9) & Chr(9) & Chr(149) & ""level 3"""

Dim sc As Object
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"

result = sc.Eval(test)

Debug.Print result

•level 1
    •level 2•level 1
    •level 2
        •level 3
于 2014-06-20T11:05:05.387 回答
0

首先将名称加载到变量中,即name = """Horace Lai"""(这将包含引号)

然后使用替换功能搜索名称中的引号name = Replace(name,"""","")

然后运行msgboxMsgBox (name & vbNewLine & Date)

这应该会删除您面临的额外 " 标记

于 2014-06-20T10:53:47.707 回答