Results 1 to 2 of 2

VBA Merge CSV files to excel

Threaded View

  1. #1
    Registered User
    Join Date
    02-07-2020
    Location
    India
    MS-Off Ver
    2016
    Posts
    1

    VBA Merge CSV files to excel

    Hi

    I'm facing issue when merging the csv files to excel. When below code is running, If I try to open a blank excel from desktop, the macro error out and stops.
    Below is the code I'm using to merge the CVS files to excel.

    For i = 1 To csvcount
                    
                      Dim key As String: key = "csvfile:" & i
                      Dim csvinfor As String: csvinfor = info.Item(key)
                      
        
                      Dim CsvInfoTokens() As String: CsvInfoTokens = Split(csvinfor, "|")
                              
                      Dim CsvFilePath As String: CsvFilePath = CsvInfoTokens(0)
                      Dim CsvDelimEncoded As String: CsvDelimEncoded = CsvInfoTokens(1)
                      Dim CsvSheetName As String: CsvSheetName = CsvInfoTokens(2)
                      Dim CsvCellAddress As String: CsvCellAddress = CsvInfoTokens(3)
        
                     
                       If CsvSheetName = "SWDATA" Then
                          ThisWorkbook.Worksheets(CsvSheetName).Visible = True
                          ThisWorkbook.Worksheets(CsvSheetName).Select
                         End If
                         
                      Dim LastAddress As String: LastAddress = Worksheets(CsvSheetName).UsedRange.SpecialCells(xlCellTypeLastCell).address(False, False)
                         'Worksheets(CsvSheetName).Visible = False
                         'In case the new data set has smaller than existing; possible when items have been deleted.
                         ThisWorkbook.ForceFullCalculation = True
                         ThisWorkbook.Worksheets(CsvSheetName).Range(CsvCellAddress, LastAddress).Clear
                         'Live-merge CSV.
                      With ThisWorkbook.Worksheets(CsvSheetName).QueryTables.Add(Connection:="TEXT;" & CsvFilePath, Destination:=Range(CsvSheetName & "!" & CsvCellAddress))
                        .FieldNames = True
                        .RowNumbers = True
                        .FillAdjacentFormulas = True
                        .RefreshOnFileOpen = False
                        .RefreshStyle = Excel.XlCellInsertionMode.xlOverwriteCells
                        .SavePassword = False
                        .SaveData = False
                        .AdjustColumnWidth = True
                        .RefreshPeriod = 0
                        .TextFilePromptOnRefresh = False
                        .TextFileParseType = xlDelimited
                        .TextFileCommaDelimiter = False
                        .TextFileSemicolonDelimiter = False
                        .TextFileSpaceDelimiter = False
                        .TextFileOtherDelimiter = "~"
                        .TextFileStartRow = 1
                        .PreserveFormatting = True  ' Code to support UNICODE Charcters if any.
                        .TextFileTextQualifier = xlTextQualifierDoubleQuote
    
                           ' Treat numbers as en-US
                        .TextFileThousandsSeparator = ","
                        .TextFileDecimalSeparator = "."
                            
                           ' CHANGED FOR NIDEC fOR JAPANESE CHARACTERS
                        .TextFilePlatform = 65001
                        .TextFileConsecutiveDelimiter = False
                        .TextFileTabDelimiter = False
                        .TextFileTrailingMinusNumbers = True
                        .Refresh
                      End With
                      
                      Dim MergeTimeSeconds As Double: MergeTimeSeconds = Round(Timer - MergeStartTime, 2)
                      'If Not Actions.logObject Is Nothing Then Call Actions.logObject.Log("Merged into " & CsvSheetName & " (" & FileLen(CsvFilePath) & "b in " & MergeTimeSeconds & "s).", DEBUG_LEVEL)
    '1100                 PopupAnswer = Shell.Popup("Merged into " & CsvSheetName & " (" & FileLen(CsvFilePath) & "b in " & MergeTimeSeconds & "s).", 0, FriendlyTitle, 64)
                      Kill CsvFilePath
                                                   
                    Next i
    Last edited by Pepe Le Mokko; 02-07-2020 at 03:18 AM. Reason: Code tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Merge 2 excel files /info
    By grunta in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 12-26-2016, 03:46 PM
  2. [SOLVED] Merge Excel Files
    By worswick25 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2013, 03:22 PM
  3. [SOLVED] Macro to Merge different Excel files into one
    By sukanyae in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-06-2012, 10:27 AM
  4. Merge two different PDF files through excel macro
    By vijanand1279 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-30-2012, 10:27 AM
  5. merge excel files
    By mlk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-03-2007, 10:22 AM
  6. Merge two Excel files
    By Teleinternnor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2005, 04:05 PM
  7. merge files in Excel?
    By Hugh Jordan in forum Excel General
    Replies: 1
    Last Post: 04-20-2005, 11:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1