Excel VBA:字符串操作无效

时间:2015-05-26 20:07:24

标签: string excel vba excel-vba

我正在尝试使用字符串(从.txt复制到单元格中),如下所示:

"                                    ========   =============   ========   =====================   ==================   =================================================================   ==================================================   =============   ==================" 
基本上,它是等号和空格的组合,但我需要稍后用它做一些事情。这就是我所做的:

targetstring3 = Range("A7").Value
Range("B8").Value = Mid(targetstring3, 57, 2)

它又回来了

  

“应用程序/对象定义的错误”

我在下面尝试了这个,它仍然无效:

targetstring3 = Cstr(Range("A7").Value)
Range("B8").Value = Mid(targetstring3, 57, 2)

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

很明显,您会收到错误消息。请记住,Excel公式以=符号开头。因此你得到一个错误。

将您的代码更改为

Range("B8").Value = "'" & Mid(targetstring3, 57, 2)

Range("B8").Value = Chr(34) & Mid(targetstring3, 57, 2) & Chr(34)

修改

评论后续跟进

我试过这个

Sub Sample()
    targetstring3 = Range("A1").Value
    Range("B8").Value = Chr(34) & Mid(targetstring3, 57, 2) & Chr(34)
    Range("B9").Value = "'" & Mid(targetstring3, 57, 2)
End Sub

<强>输出

enter image description here