【问题标题】:Find values in TXT file and replace with values from Excel cells在 TXT 文件中查找值并替换为 Excel 单元格中的值
【发布时间】:2021-06-04 00:10:34
【问题描述】:

我正在寻找一个宏来查找和替换 txt 文件中的值。

txt文件位于路由中

D:\Template\info.txt

这个文件长这样(确实文件有HTML代码,但我这里合成了重要的部分)

The current date and time is: %time%

GPU1 Temperature is: %GPU1T%
GPU1 Fan Speed is: %GPU1F%
GPU1 RPM are: %GPU1R%

GPU2 Temperature is: %GPU2T%
GPU2 Fan Speed is: %GPU2F%
GPU2 RPM are: %GPU2R%

Excel 文件看起来像这样

我需要的是一个可以查找和替换以下内容的宏:

Find %time% in the txt file and replace with the value of cell G2
Find %GPU1T% in the txt file and replace with the value of cell A2
Find %GPU1F% in the txt file and replace with the value of cell C2
Find %GPU1R% in the txt file and replace with the value of cell E2
Find %GPU2T% in the txt file and replace with the value of cell B2
Find %GPU2F% in the txt file and replace with the value of cell D2
Find %GPU2R% in the txt file and replace with the value of cell F2

然后,当然保存文件,但在一个新的位置,即

D:\GPUReport

但名称相同info.txt

到目前为止,尝试处理这个宏没有成功

Sub FindAndReplaceText()

 Dim FileName As String
 Dim FolderPath As String
 Dim FSO As Object
 Dim I As Integer
 Dim SearchForWords As Variant
 Dim SubstituteWords As Variant
 Dim Text As String
 Dim TextFile As Object
 

   SearchForWords = Array("%time%", "%GPU1T%", "%GPU1F%", "%GPU1R%", "%GPU2T%", "%GPU2F%", "%GPU2R% ")
   SubstituteWords = Array(Range("G2"), Range("A2"), Range("C2"), Range("E2"), Range("B2"), Range("D2"), Range("F2"))
 

   FolderPath = "D:\Template\info.txt"
   
     Set FSO = CreateObject("Scripting.FileSystemObject")
   
     FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
     FileName = Dir(FolderPath & "\*.txt")
   
     Do While FileName <> ""
       Filespec = FolderPath & FileName

         Set TextFile = FSO.OpenTextFile(Filespec, 1, False)
           Text = TextFile.ReadAll
         TextFile.Close
         

         Set TextFile = FSO.OpenTextFile(Filespec, 2, False)
           For I = 0 To UBound(SearchForWords)
             Replace Text, SearchForWords(I), SubstituteWords(I)
           Next I
         TextFile.Write Text
         TextFile.Close
       FileName = Dir()
     Loop
     
End Sub

我希望有人可以帮助我。

非常感谢您!

【问题讨论】:

  • 表达式FileName = Dir(FolderPath &amp; "\*.txt") 将搜索带有D:\Template\info.txt\\*.txt 掩码的文件。我认为 FileName 总是为空的。也许 FolderPath 必须是“D:\Template”?

标签: excel vba


【解决方案1】:

试试这样的:

Sub Tester()
    Dim txt As String, arr, c As Long
    
    arr = ThisWorkbook.Sheets("config").Range("A1:G2").Value
    txt = GetContent("C:\Tester\info.txt")
    For c = 1 To UBound(arr, 2)
        txt = Replace(txt, "%" & arr(1, c) & "%", arr(2, c))
    Next c
    PutContent "C:\Tester\info2.txt", txt
End Sub

Function GetContent(f As String) As String
    GetContent = CreateObject("scripting.filesystemobject"). _
                  opentextfile(f, 1).readall()
End Function

Sub PutContent(f As String, content As String)
    CreateObject("scripting.filesystemobject"). _
                  opentextfile(f, 2, True).write content
End Sub

编辑:刚刚注意到您的标头与占位符标记不同,但使它们相同会更容易(但不要在标头周围包含“%”)。或者在列标题下添加另一行以包含该列的占位符文本。

【讨论】:

  • 新文件 info2.txt 是使用 info.txt 的精确副本创建的,但用 % 符号划分的值不会被替换,请帮帮我吗?
  • 查看我上面的编辑 - 如果您使用列标题作为占位符文本会容易得多。
  • 我更改了标题并在 Excel 表中写入了占位符。我的意思是,我将 GPU2 Temp 更改为 %GPU2T% (我对其他标题做了同样的操作),但代码不会替换新 txt 中的文本。我应该在宏中编辑一些特殊的东西吗?
  • 不要在标题名称中包含% - 宏会添加这些,因此您最终会寻找(例如)%%GPU2T%%
  • 是的,这就是问题所在!我在标题中包含 % ,我删除了它并且它正在工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-09-20
  • 2012-11-05
  • 2021-04-22
  • 2023-01-31
  • 2018-08-16
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
相关资源
最近更新 更多