在多个csv文件中查找并替换char

时间:2015-03-23 16:16:54

标签: excel vba excel-vba spreadsheet

我在一个文件夹中有几个csv文件。我想将这些文件添加到一个包含多个工作表的excel文件中。 在我在Excel工作表中添加它们之前,我想将.替换为,,因为我在excel中的约定。

但是,下面的代码告诉我: enter image description here

这是我的代码:

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

任何建议我做错了什么?

感谢您的回复!

4 个答案:

答案 0 :(得分:3)

试试这个:

'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注释掉,直到一切正常,我发现调试更容易。在调试时,你并不担心执行速度。

另一个循环,因为你打开了一个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并且无法保证当你到达那里时会是什么。所以,我宣布了一个新的WorkBook变量并在执行任何操作之前将其分配给ThisWorkbook,这样您就可以100%确定要将其移动到哪里。我还关闭了我们打开的CSV,只是为了整理一下。

答案 1 :(得分:2)

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

Dim ws as Worksheet
  for each ws in activeworkbook.worksheets

答案 2 :(得分:2)

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

要解决类型不匹配,请添加一个新变量Dim wsCSV As Worksheet,并在循环中使用此新变量作为每个工作表的参考。

循环可能如下所示:

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

答案 3 :(得分:0)

我不知道你通过wbCSV的方法,从来没有做过,但看起来不对......

我建议你只使用切换" xml"就可以使用我在这里发布的内容。到" csv" : https://stackoverflow.com/questions/29184595/loop-on-all-files-in-the-same-directory-then-detect-extension-type/29187762#29187762

我认为这对你所要做的事情来说是一个相当好的开始! ;)