如何保存对现有excel文件的更改,而不是创建包含更改的txt文件?

时间:2014-08-05 02:25:16

标签: excel vbscript save

我的代码从excel文件中读取B列,并删除在字符末尾有任何下划线的值,然后将其保存到带有更改的新txt文件中。但是现在我希望它保存代码在现有excel文件中所做的任何更改(删除下划线)并覆盖它而不是创建一个带有更改的新txt文件...帮助任何人?

'********** Declare the Output Values **********

Dim v_Parent, v_Child, v_Alias

Dim v_Output2 

Dim c_Delimiter, c_Max_Length, c_Underscore, c_Comma


'********** Declare the Shell, Directory & File Properties **********

Dim objShell, objShell2, objFile


'********** Declare the Excel Properties **********

Dim objExcel, objWorkbook


'********** Declare the Other Variables **********

Dim c_Char

dim row

c_Underscore = "_"
c_Char = 1
c_B2 = "B2" 
c_FirstWksht      = 1

Set objShell = CreateObject("Wscript.Shell")
Set objExcel = CreateObject("Excel.Application")

Set objFSO2 = CreateObject("Scripting.FileSystemObject")
    Set objWorkbook2 = objExcel.Workbooks.Open("D:\Shared\tempname\test\test2.xlsx")
          Set objFile = objFSO2.CreateTextFile("D:\Shared\tempname\test\test2.txt")

          '********** Instantiate the Workbook.Worksheets **********
          '               object to open a particular
          '                    Excel Worksheet
          Set objWorksheet2 = objWorkbook2.Worksheets(c_FirstWksht)

            set Description_column_n = objWorksheet2.Range(c_B2).EntireColumn

            For row = 1 To (Description_column_n.Rows.Count - 1)
            v_Output2 = Description_column_n.Cells(row,1).Value

             Do while (Right(v_Output2, c_Char) = c_Underscore)
               v_Output2 = left(v_Output2, len(v_Output2) - c_Char)

               Loop
            objFile.Write v_Output2 & vbCRLF 
            next



            Set objWorkbook2 = nothing
             objExcel.Quit


                    '********** Exit the Script **********

                    Wscript.Quit

1 个答案:

答案 0 :(得分:1)

也许令人惊讶的是,你会save工作簿:

...
objWorkbook2.Save
objWorkbook2.Close

objExcel.Quit