简体   繁体   English

excel VBA,在将多个 CSV 文件复制到一个工作簿时,在单元格中创建带有工作表或文件名的列

[英]excel VBA, create a column with sheet or file name in cells while copying multiple CSV files to one workbook

I've got 700 CSV files with 7 columns 1000 rows each and I need to get them in one long column.我有 700 个 CSV 文件,每个文件有 7 列 1000 行,我需要将它们放在一个长列中。 Sample code is doing the copying but I don't know how to get it to create a column (the same length as the other columns in that file) with sheet or file name in each cell before copying.示例代码正在复制,但我不知道如何让它在复制之前在每个单元格中创建一个列(与该文件中的其他列长度相同),其中包含工作表或文件名。 I really only need column A (dates), created column (sheet name) and column F (values) from each CSV file, in that order if thats possible.我真的只需要每个 CSV 文件中的 A 列(日期)、创建列(工作表名称)和 F 列(值),如果可能的话。

    Sub ImportData()
Dim lastrow As Long
Dim clastrow As Long
Dim filePath As String
Dim fileName As String
Dim count As Long
Dim importRange As Range
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim cws As Excel.Worksheet
count = 0
Set cws = ThisWorkbook.Sheets(2)
filePath = "C:\Users\user\Desktop\CSV files\"
fileName = Dir(filePath & "*.csv")
Do While fileName <> ""
    count = count + 1
    Set wb = Excel.Workbooks.Open(filePath & fileName)
    Set ws = wb.Worksheets(1)
    lastrow = ws.Cells(Rows.count, "a").End(xlUp).Row
    clastrow = cws.Cells(Rows.count, "a").End(xlUp).Row + 1
    Set importRange = ws.Range("a2:f" & lastrow)           'skips header row
'    cws.Cells(clastrow, 1).End(xlUp).Offset(1, 0).Resize(importRange.Rows.count, importRange.Columns.count) = importRange.Value
    importRange.Copy
    cws.Cells(clastrow, "a").PasteSpecial xlPasteValues
    wb.Application.CutCopyMode = False
    wb.Close
    fileName = Dir
Loop
End Sub

Copy Values by Assignment按赋值复制值

  • Not tested.未测试。

The Code编码

Option Explicit
    
Sub importData()
    
    ' Define constants.
    Const FilePath As String = "C:\Users\user\Desktop\CSV files\"
    
    ' Define Destination First Cell.
    Dim drg As Range
    With ThisWorkbook.Sheets(2)
        Set drg = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    End With
    
    ' Write the first file name to a variable.
    Dim FileName As String: FileName = Dir(FilePath & "*.csv")
    
    ' Declare additional variables.
    Dim srg As Range ' Source Range
    Dim sLastRow As Long ' Source Last Row
    Dim srCount As Long ' Source Rows Count
    Dim fCount As Long ' Files Count
    
    ' Copy values by assignment.
    Application.ScreenUpdating = False
    Do While FileName <> ""
        With Workbooks.Open(FilePath & FileName).Worksheets(1)
            sLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            If sLastRow >= 2 Then
                fCount = fCount + 1
                Set srg = .Range("A2:F" & sLastRow)
                srCount = srg.Rows.Count
                Set drg = drg.Resize(srCount)
                drg.Value = srg.Columns(1).Value
                drg.Offset(, 1).Value = .Name
                drg.Offset(, 2).Value = srg.Columns(6).Value
                Set drg = drg.Cells(1).Offset(srCount)
            End If
            .Parent.Close SaveChanges:=False
        End With
        FileName = Dir
    Loop
    'drg.Worksheet.Parent.Save
    Application.ScreenUpdating = True
    
    ' Inform.
    MsgBox "Files processed: " & fCount, vbInformation, "Success"

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件夹中的多个工作簿合并到一个文件中,每个工作簿作为单独的工作表,文件名 = 工作表名 - Excel VBA 宏 - Combine multiple workbooks in a folder into one file, every workbook as a separate sheet, file name = sheet name - Excel VBA macro 如何从一张 excel 表创建多个 CSV 文件 - VBA - How can I create multiple CSV Files from one excel sheet - VBA VBA Excel宏将一列复制到另一个工作簿中 - VBA Excel Macro Copying One Column into Another Workbook 将列复制到VBA中的新工作簿表 - Copying a column to a new workbook sheet in VBA Excel VBA 循环 - 将单元格从一列复制到另一列多次 - Excel VBA Loop - Copying cells from one column into another multiple times Excel vba:将多个文件合并在一张纸中 - Excel vba: combine multiple files in one sheet VBA代码 - 根据匹配条件将单元格中的单元格复制到Excel中的另一个工作表 - VBA Code - Copying cells from one sheet to another sheet in excel depending on matching condition Excel VBA:将多张工作表复制到新工作簿中 - Excel VBA: Copying multiple sheets into new workbook 如何通过复制CSV文件并从Linux远程运行VBA宏来创建Excel文件 - How to create excel file by copying CSV files and running VBA macros remotely from Linux 将单元格中的单元格复制到多张Excel - VBA中 - copy cells from one sheet into multiple sheets Excel - VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM