Where did you get your code that you are using?
It looks to be pretty sophisticated object oriented code (not my forte).
It looks like it should already be looping through all the files in your folder, but I do not see any code for inserting any columns.
How many columns does your original data have?
Are you inserting the columns in the middle of the data, or at the end of it?
Also, you talk about having one of the inserted columns be a drop-down, but you don't mention what the available values in it should be.
Hi Joe,
I got the code from google. As you mentioned I did the macro recorder that whatever I needed and club with exiting conversion code see below. However, I am executing the file and getting error on this line (Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove).
Currently, I am doing POC of my project and I have created three .csv file with four column name like
A,B,C,D in folder "C:\VB". I want to convert my .CSV file to excel file after convert need to add two column in excel like
E and F and also in F column I need data validation for
Yes or No
Function ColAdd()
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Select
ActiveCell.FormulaR1C1 = "E"
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("F1").Select
ActiveCell.FormulaR1C1 = "F"
Range("F:F").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$I$1:$I$2"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("I1").Select
ActiveCell.FormulaR1C1 = "yes"
Range("I2").Select
ActiveCell.FormulaR1C1 = "no"
Columns("I:I").Select
Selection.EntireColumn.Hidden = True
End Function
'Constants
Const xlOpenXMLWorkbook = 51 '(without macro's in 2007-2016, xlsx)
Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
Const xlExcel12 = 50 '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
Const xlExcel8 =56 '(97-2003 format in Excel 2007-2016, xls)
' Extensions for old and new files
strExcel = "xlsx"
strCSV = "csv"
strXLS = "xls"
' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\VB"
' Access the folder to process
Set objFolder = objFSO.GetFolder(strFolder)
' Load Excel (hidden) for conversions
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
' Process all files
For Each objFile In objFolder.Files
' Get full path to file
strPath = objFile.Path
' Only convert CSV files
If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Or LCase(objFSO.GetExtensionName(strPath)) = LCase(strXLS) Then
' Display to console each file being converted
Wscript.Echo "Converting """ & strPath & """"
' Load CSV into Excel and save as native Excel file
Set objWorkbook = objExcel.Workbooks.Open(strPath)
strNewPath = objFSO.GetParentFolderName(strPath) & "\" & objFSO.GetBaseName(strPath) & "." & strExcel
objWorkbook.SaveAs strNewPath, xlOpenXMLWorkbook
objWorkbook.Close False
Set objWorkbook = Nothing
End If
Next
For Each objFile In objFolder.Files
' Get full path to file
strPath = objFile.Path
' Only convert CSV files
If (LCase(objFSO.GetExtensionName(strPath)) = LCase(strXLS) Then
Set objWorkbook = objExcel.Workbooks.Open(strPath)
Call ColAdd()
objWorkbook.Save
objWorkbook.Close False
Set objWorkbook = Nothing
End If
Next
'Wrap up
objExcel.Quit
Set objFSO = Nothing
Set objExcel = Nothing