VBA – replace文本文件中的string与其他文件中的variables值

我是VBA的新手,一直坚持我的代码。 如果我能得到一些帮助,我将不胜感激。

问题:我有一个有两列的Excel文件,我需要从第一列逐个searchstring,如果在另一个文件(这是一个XML文件)中有一个匹配项,我需要用相应的stringreplace该stringstring在第二列。

问题:我写了下面的代码,它对硬编码的值来说工作正常,但是当我尝试用Variablesreplace它时,它运行了但没有做任何事情。 所以我改变了这个代码。 但是,现在我得到错误。

有人可以帮我弄这个吗 ?

这是我的代码:(这是与硬编码值工作)

Private Sub CommandButton1_Click() Call EXLFileRead End Sub Sub EXLFileRead() Dim EXLFile As String Dim OldValue As String Dim NewValue As String Dim x As Long Dim countrow As Long Dim y As Long Dim Z As Long y = 1 Z = 2 EXLFile = Application.GetOpenFilename()` Workbooks.Open (EXLFile)` Open EXLFile For Input As #1 With ActiveSheet countrow = .Cells(.Rows.Count, "A").End(xlUp).Row End With For x = 1 To countrow Call XMLfile(ActiveSheet.Cells(x, y).Value, ActiveSheet.Cells(x, Z).Value) Next x Close End Sub Sub XMLfile(RowOldValue As String, RowNewValue As String) Const ForReading = 1, ForWriting = 2 Dim FSO, FileIn, FileOut Dim strTmp Set FSO = CreateObject("Scripting.FileSystemObject") Set FileIn = FSO.OpenTextFile("C:\Users\AshishA\Desktop\exp\note.xml", ForReading) Set FileOut = FSO.OpenTextFile("C:\Users\AshishA\Desktop\exp\note1.xml", ForWriting, True) Do Until FileIn.AtEndOfStream strTmp = FileIn.ReadLine If Len(strTmp) > 0 Then If InStr(1, strTmp, RowOldValue, vbTextCompare) = 0 Then strRep = Replace(strTmp, "ABCD", "NEW TEXT") FileOut.WriteLine strRep End If End If Loop FileIn.Close FileOut.Close End Sub enter code here ---------------------------------------------------------------------- -----This one is just executing, but actually Not replacing----------- -----I have the old value in RowOldValue and the value to be---------- -------------------replaced in RowNewValue---------------------------- ********************************************************************** Private Sub CommandButton1_Click() Call EXLFileRead End Sub Sub EXLFileRead() Dim EXLFile As String Dim OldValue As String Dim NewValue As String Dim x As Long Dim countrow As Long Dim y As Long Dim Z As Long y = 1 Z = 2 EXLFile = Application.GetOpenFilename()` Workbooks.Open (EXLFile)` Open EXLFile For Input As #1 With ActiveSheet countrow = .Cells(.Rows.Count, "A").End(xlUp).Row End With For x = 1 To countrow Call XMLfile(ActiveSheet.Cells(x, y).Value, ActiveSheet.Cells(x, Z).Value) Next x Close End Sub Sub XMLfile(RowOldValue As String, RowNewValue As String) Const ForReading = 1, ForWriting = 2 Dim FSO, FileIn, FileOut Dim strTmp Set FSO = CreateObject("Scripting.FileSystemObject") Set FileIn = FSO.OpenTextFile("C:\Users\AshishA\Desktop\exp\note.xml", ForReading) Set FileOut = FSO.OpenTextFile("C:\Users\AshishA\Desktop\exp\note1.xml", ForWriting, True) Do Until FileIn.AtEndOfStream strTmp = FileIn.ReadLine If Len(strTmp) > 0 Then If InStr(1, strTmp, RowOldValue, vbTextCompare) = 0 Then strRep = Replace(strTmp, RowOldValue, RowNewValue) FileOut.WriteLine strRep End If End If Loop FileIn.Close FileOut.Close End SubI'm new to VBA and stuck at one point of my code. I would really appreciate if I could get some help . 

问题:我有一个有两列的Excel文件,我需要从第一列逐个searchstring,如果在另一个文件(这是一个XML文件)中有一个匹配项,我需要用相应的stringreplace该stringstring在第二列。

问题:我写了下面的代码,它对硬编码值的工作正常,但是当我尝试使用variables进行replace时,它运行了,但是没有做任何事情。 所以我改变了这个代码。 但是,现在我得到错误。

有人可以帮我弄这个吗 ?

这是我的代码:(这是与硬编码值工作)

  Private Sub CommandButton1_Click() Call EXLFileRead End Sub Sub EXLFileRead() Dim EXLFile As String Dim OldValue As String Dim NewValue As String Dim x As Long Dim countrow As Long Dim y As Long Dim Z As Long y = 1 Z = 2 EXLFile = Application.GetOpenFilename()` Workbooks.Open (EXLFile)` Open EXLFile For Input As #1 With ActiveSheet countrow = .Cells(.Rows.Count, "A").End(xlUp).Row End With For x = 1 To countrow Call XMLfile(ActiveSheet.Cells(x, y).Value, ActiveSheet.Cells(x, Z).Value) Next x Close End Sub Sub XMLfile(RowOldValue As String, RowNewValue As String) Const ForReading = 1, ForWriting = 2 Dim FSO, FileIn, FileOut Dim strTmp Set FSO = CreateObject("Scripting.FileSystemObject") Set FileIn = FSO.OpenTextFile("C:\Users\AshishA\Desktop\exp\note.xml", ForReading) Set FileOut = FSO.OpenTextFile("C:\Users\AshishA\Desktop\exp\note1.xml", ForWriting, True) Do Until FileIn.AtEndOfStream strTmp = FileIn.ReadLine If Len(strTmp) > 0 Then If InStr(1, strTmp, RowOldValue, vbTextCompare) = 0 Then strRep = Replace(strTmp, "ABCD", "NEW TEXT") FileOut.WriteLine strRep End If End If Loop FileIn.Close FileOut.Close End Sub