将CSV合并到1个Excel工作表中并删除标题

时间:2017-04-13 17:33:18

标签: excel vba excel-vba csv

我尝试将存储在文件夹中的所有CVS's合并到一个Excel工作表中。在他们合并之后,我将运行一个单独的宏来处理所有格式,而不是单独格式化每个单独的CSV文件。

以下代码是我目前所拥有的:

Sub MergeFiles_Click()

Dim strSourcePath As String
Dim strDestPath As String
Dim strFile As String
Dim strData As String
Dim x As Variant
Dim Cnt As Long
Dim r As Long
Dim c As Long

Application.ScreenUpdating = False

strSourcePath = Sheet1.Range("G2").Value

If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"

strFile = Dir(strSourcePath & "*.csv")

Do While Len(strFile) > 0

    Cnt = Cnt + 1

    If Cnt = 1 Then
            r = 6
        Else
            r = Cells(Rows.Count, "A").End(xlUp).Row + 1
    End If


    Open strSourcePath & strFile For Input As #1
    Do Until EOF(1)
            Line Input #1, strData
            x = Split(strData, ",")
            For c = 0 To UBound(x)
                Cells(r, c + 1).Value = Trim(x(c))
            Next c
            r = r + 1
        Loop

    Close #1

    strFile = Dir
Loop

Application.ScreenUpdating = True

If Cnt = 0 Then _
    MsgBox "No CSV files were found...", vbExclamation

End Sub

这会将所有CSV个文件合并为一个工作表,但每个CSV文件都有一个标题,顶部的其他无用信息占用12行。

我希望将第12行保留在excel中的第一个CSV的顶部,但在将其放入Excel工作表之前从剩余的文件中删除这12行。

我基本上只希望文件显示为一个而不是看起来像文件被复制并粘贴到工作表中。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:3)

对现有代码的最简单更改是仅包含代码,以便在<?php function mergeSort( $result){ $minIp = ip2long('10.0.0.11') ; $maxIp = ip2long('10.255.255.255'); $count =0; $tmpIp = $minIp; while($temIp <= $maxIp){ if( empty($result)){ break ; } $tmp = array_pop($result); $dbIp =$tmp['ip']; while($temIp < $dbIp){ // echo temIp ; // i repalce it by count ++ , i don't want it //full my teminal . $count ++; $temIp ++; } $temIp ++; } while($temIp <= $maxIp){ //echo $temIp ; replace by $count++ $count ++; $temIp++; } return $count -1; } $servername = "localhost"; $username = "root"; $password = "aaaaa"; $dbname = "IP"; $conn = new PDO('mysql:host=' . $servername . ';dbname=' . $dbname , $username, $password); $conn->setAttribute(PDO::ATTR_AUTOCOMMIT , true); $stmt = $conn->prepare("select * from ipTable order by ip desc"); $stmt->execute(); $result = $stmt->fetchAll(); $count = mergeSort($result); echo $count ; ?> 为1时仅复制前12行,否则忽略它们:

Cnt

答案 1 :(得分:0)

正如Yow E3K所说,您可以只复制第一次的前十二行。 我的偏好是首先将它们放在模板上,然后再从不复制它们。

下面的代码(来自VBA Copy data from an unopened CSV file to worksheet without opening closed CSV-谢谢Chancea)已经过半修改,可以通过放入第二行开始复制 .TextFileStartRow = 2

Sub ImportFromCSVWithoutHeaders()

Dim MyDocuments, strFileName, myToday, file, strConnection As String

MyDocuments = Environ$("USERPROFILE") & "\My Documents"
myToday = Format(Date, "mmddyy")
strFileName = "DataFile" & myToday & ".csv"

Dim row As Integer
row = 1
On Error Resume Next
row = Range("A1048576").End(xlUp).row + 1

strConnection = "TEXT;" & MyDocuments & "\DataFolder\" & strFileName

With ActiveSheet.QueryTables.Add(Connection:= _
     strConnection, Destination:=Range("$A$" & row))
    .Name = "temp"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    '.TextFileStartRow = 1
    .TextFileStartRow = 2
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
End Sub