Excel macro - open files - copy data - rename worksheet close files

swilson2006

New Member
Joined
May 25, 2012
Messages
11
Hi there

I hope someone can help with a small macro.

The macro is to:

1. Look inside a specified folder at all csv files
2. Look at column G of the csv file which contains a date and time in this format 28/02/2018 08:31:10 and take the date from this the second row and then create a worksheet in a pre-set XLSX file with the data as the worksheet name. Copy all the CSV data to the XLSX file and then close all files and move on to the next file.
3. If the worksheet already exists then move on to the next file until all files have been processed.
4. Insert a column before column C called "PYMT TYPE".
5. Close the XLSX file and CSV files.

I would like it so that it does not duplicate a worksheet and just skips it.

Basically I am creating an XLSX file from daily worksheet so I can combine all the data in the CSVs into one file.

I appreciate your help.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
not sure exactly what you want but here's a start....


usage:
getCsvFilesInDir "c:\temp"


Code:
Private Sub getCsvFilesInDir(ByVal pvSrcDir)
Dim FSO, oFolder, oFile, oRX
Dim sCriteria As String, sFile As String
Dim iCnt As Integer
Dim vXFmt, vDate


On Error GoTo errGetFiles


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


Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(pvSrcDir)


For Each oFile In oFolder.Files
  If InStr(oFile.Name, ".csv") > 0 Then            'open CSV file here
     sFile = pvSrcDir & oFile.Name
     
     Workbooks.Add
     ImportCsv sFile
     vDate = Range("G2").Value
     
       'do stuff here?
       
         'save as new name
     vNewFile = "C:\temp\" & Format(vDate, "yyyymmdd-hhnnss") & ".xls"
    
     ActiveWorkbook.SaveAs Filename:=vNewFile, FileFormat:=xlExcel8, _
        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
         
     ActiveWorkbook.Close
  End If
Next
MsgBox "Done"


endit:
Set oFile = Nothing
Set oFolder = Nothing
Set FSO = Nothing
Exit Sub


errGetFiles:
  MsgBox Err.Description, , Err
End Sub




Private Sub ImportCsv(ByVal pvFile)
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & pvFile, Destination:=Range("$A$1"))
        .CommandType = 0
        .Name = "names_1"
        .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
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("C1").Value = "PYMT TYPE"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top