查找并replace多个CSV文件中的字符

我有一个文件夹中的几个CSV文件。 我想将这些文件添加到一个Excel文件与多个工作表。 在我将它们添加到我的Excel表中之前,我想replace它.,因为我在Excel中的约定。

不过,下面的代码给了我: 在这里输入图像说明

这是我的代码:

 Option Explicit Sub ImportCSVs() 'Summary: Import all CSV files from a folder into separate sheets ' named for the CSV filenames Dim fPath As String Dim fCSV As String Dim fnd As Variant Dim rplc As Variant Dim wbCSV As Workbook 'add your find and replace values! '############################# fnd = "." rplc = "," Application.ScreenUpdating = False 'speed up macro 'path to CSV files, include the final \ '############################# fPath = "C:\Users\Desktop\Data\23-3-2015_Data\" fCSV = Dir(fPath & "*.csv") 'start the CSV file listing Do While Len(fCSV) > 0 Set wbCSV = Workbooks.Open(fPath & fCSV) 'open a CSV file and move 'find and replace the . by , For Each wbCSV In ActiveWorkbook.Worksheets wbCSV.Cells.Replace what:=fnd, Replacement:=rplc, _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False Next wbCSV ActiveSheet.Move After:=ThisWorkbook.Sheets(Sheets.Count) fCSV = Dir 'ready next CSV Loop Application.ScreenUpdating = True Set wbCSV = Nothing End Sub 

任何build议我做错了什么?

我感谢您的回复!

尝试这个:

 'add to your Dim statements: Dim ws as Worksheet 'change your Do loop to: Do While Len(fCSV) > 0 Set wbCSV = Workbooks.Open(fPath & fCSV) 'open a CSV file and move 'find and replace the . by , For Each ws In wbcsv ws.Cells.Replace what:=fnd, Replacement:=rplc, _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False Next ws 

另外,如果将Application.ScreenUpdating = False注释掉,直到所有内容都可以正常工作,我发现debugging起来更容易。 在debugging的时候,你并不担心执行速度。

另一个循环,因为你打开一个CSV,只能有一个工作表,这应该简化了一下:

 Dim DestBook as workbook Set DestBook = ThisWorkbook 'other setup stuff... Do While Len(fCSV) > 0 Set wbCSV = Workbooks.Open(fPath & fCSV) 'open a CSV file and move 'find and replace the . by , wbCSV.worksheet(1).Cells.Replace what:=fnd, Replacement:=rplc, _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False wbcsv.move after:=DestBook.sheets(DestBook.sheets.count) wbCSV.close vFCSV = Dir Loop 

我也注意到,这个举动是把它移动到ThisWorkbook ,并不能保证当你到达那里会是什么。 所以,我宣布了一个新的WorkBookvariables,并在做任何事情之前将它分配给ThisWorkbook ,这样你就可以100%确定你将它移动到哪里。 我也closures了我们打开的CSV,只是为了一些整理。

在错误行中For Each wbCSV In ActiveWorkbook.Worksheets您要遍历所有工作表,但是您使用的是被声明As Workbook wbCSV

要解决types不匹配问题,请添加一个新的variablesDim wsCSV As Worksheet并在循环中使用这个新variables作为每个工作表的参考。

循环可能看起来像这样:

 For Each wsCSV In wbCSV.Worksheets wsCSV.Cells.Replace what:=fnd, Replacement:=rplc, _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False Next wsCSV 

wbCSV是工作簿对象而不是工作表。 尝试这个

 Dim ws as Worksheet for each ws in activeworkbook.worksheets 

我不知道你的方法通过wbCSV,从来没有这样做,但似乎并不正确…

我不能build议你使用我在这里发布的只是切换“xml”为“csv”: https : //stackoverflow.com/questions/29184595/loop-on-all-files-in-the-same-directory-then-检测扩展型/ 29187762#29187762

我认为这将是一个很好的开始,你必须做的! ;)