如何使用vba从excel文件中删除逗号?

时间:2017-10-19 17:26:28

标签: vba excel-vba excel

我将自动将文件夹中的文件作为.txt转换为csv文件。现在这个文件上有一些名称,最后包含一个逗号。我想知道是否有办法删除这个逗号。

以下是当前的脚本:

Option Explicit

On Error Resume Next
xlApp.DisplayAlerts = False
XlApp.SetWarnings False
Dim xlApp, xlBook, xlSheet

Const xlCSV = 6

On Error Resume Next
xlApp.DisplayAlerts = False
XlApp.SetWarnings False

Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Open(FileLocation:\& "filename.txt", , , 6, , , , , "|")

Set xlSheet = xlBook.worksheets.item(1)



XlApp.SetWarnings False

On Error Resume Next

xlApp.DisplayAlerts = False

xlSheet.Range("B:B").Delete

xlSheet.Range("C:C").Delete

xlSheet.Range("F:H").Delete

xlSheet.Range("G:K").Delete

xlSheet.Range("P:Q").Delete

xlSheet.Range("A1").EntireRow.Insert

xlSheet.Cells(1,1).Value = "name1"

xlSheet.Cells(1,2).Value = "name2"

xlSheet.Cells(1,3).Value = "name3"

xlSheet.Cells(1,4).Value = "name4"

xlSheet.Cells(1,5).Value = "here is where the comma appears for some files"

xlSheet.Cells(1,6).Value = "name5"

xlSheet.Cells(1,7).Value = "name6"

xlSheet.Cells(1,8).Value = "name7"

xlSheet.Cells(1,9).Value = "name8"

xlSheet.Cells(1,10).Value = "name9"

xlSheet.Cells(1,11).Value = "name10"

xlSheet.Cells(1,12).Value = "name11"

xlSheet.Cells(1,13).Value = "name0"

xlSheet.Cells(1,14).Value = "name22"

xlSheet.Cells(1,15).Value = "name24"




xlApp.DisplayAlerts = False

xlBook.Saveas "Filelocation:\filename.csv", xlCSV

xlApp.DisplayAlerts = False

xlApp.Quit

2 个答案:

答案 0 :(得分:1)

使用替换功能。

xlSheet.Cells(1,5).Value = replace(xlSheet.Cells(1,5).Value, ",", "")

要应用此选项,您需要将新值分配给单元格。 这样的东西将遍历E列中所有使用过的单元格

For Each c In Cells.Range("E1:E" & Cells(ActiveSheet.Rows.Count, "E").End(xlUp).Row)
c.Value = Replace(c.Value, ",", "")
Next c

答案 1 :(得分:0)

我们也可以用that的方式尝试我们的VBA代码

compile 'com.github.ViksaaSkool:AwesomeSplash:v1.0.0'