【问题标题】:vba replacing two quotation marks with one / generating txt filesvba用一个替换两个引号/生成txt文件
【发布时间】:2017-04-05 15:06:18
【问题描述】:

我创建了一个宏,它可以生成 txt 文件,其中包含 excel 中每一行的数据。我对“(引号)有疑问

这是 VBA 中出现问题的一行:

wsTemp.Cells(6, 1).Value = "The ""Number"" is " & wsSource.Cells(r, 3).Value

我需要这个:The "Number" is MYNUMBER

而不是我得到这个:" The ""Number"" is" MYNUMBER

这也不起作用:

wsTemp.Cells(6, 1).Value = "The" & Chr(34) & "Number"" is " & Chr(34) & wsSource.Cells(r, 3).Val(得到相同的输出)

我尝试用Chr(34) 替换""(没用)

最后我也尝试了替换功能:

wsTemp.Cells(6, 1).Value = Replace(wsTemp.Cells(6, 1).Value, """", """)(没用)和 wsTemp.Cells(6, 1).Value = Replace(wsTemp.Cells(6, 1).Value, """", Chr(34))(效果不佳)

Replace() 确实适用于其他 Chr()Replace(wsTemp.Cells(6, 1).Value, """", "") 但不适用于 Chr(34)

无论如何,我需要一个 ",而不是两个 ' 或其他任何东西

我将生成的文件保存为 xlCurrentPlatformText 和 .txt,我也尝试过其他格式

另外,当我手动将 excel 中的相同文本保存为 txt 时,我注意到了这一点

我几乎尝试了所有方法,但没有任何效果

我使用的是 Win 7 和 Excel 2013

【问题讨论】:

  • 我认为问题在于 Excel 如何保存到文本文件,而不是您使用的公式。见HERE。您可能需要使用 VBA 以您想要的方式编写文本文件,或者进行其他一些技巧(例如,使用 $$$ 作为引号的占位符,然后对文本文件进行搜索/替换)。

标签: vba excel excel-2013 string-concatenation


【解决方案1】:

尝试其中之一,

With wsTemp.Cells(6, 1)
    .Value = Format(wsSource.Cells(r, 3).Value2, "\T\h\e \""\N\u\m\b\e\r\"" \i\s 0")
    .Offset(1, 0) = "The ""Number"" is " & wsSource.Cells(r, 3).Value2
    .Offset(2, 0) = "The " & Chr(34) & "Number" & Chr(34) & " is " & wsSource.Cells(r, 3).Value2
End With

【讨论】:

    【解决方案2】:

    所以我按照@xidgel 的建议做了,用 FindAndReplaceText(放屁)

    我将所有" 替换为$$$$

    我创建了一个文件 replace.bat

    fart -r "MYLOCATION*.txt" $$$$ \"
    

    并在vba宏末尾添加shell执行命令

    Call Shell("C:\LOCATION\replace.bat", vbNormalFocus)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 2010-12-09
      • 2019-06-01
      • 1970-01-01
      • 2020-12-26
      • 2021-06-15
      相关资源
      最近更新 更多