|
|
Question : How to open, populate, and save a .csv file using an .xls spreadsheet ?
|
|
Hi,
I would like to write code that will create, populate (certain cells), and save a .csv file using data from an .xls spreadsheet. I have both the .xls and .csv files as templates but would like the .xls file to create the .csv template/file each time the user clicks on save/submit button. The only thing that is missing is the code in the .xls file that will create, populate (data from its sheet into certain cells in .csv), and save this .csv template. This code should drop the .csv file if it already exists, and create a new one before population.
I can send you the templates if it will help you in resolving this issue.
I appreciate your help.
Thanks. masaimara
|
Answer : How to open, populate, and save a .csv file using an .xls spreadsheet ?
|
|
Thanks for the compliment :)
Replace the code in Module1 with the code below. The macro now saves Export.csv to the same directory as the main file.
For more info on the various propertie and methods, take a look at the Visual Basic Editor's help file--it is actually pretty good for VBA beginners.
_____________________________________________________________________
Option Explicit
Private Sub MakeCSV()
Dim Dest As Workbook Dim LinesToDo As Long Dim Proj Dim Task Dim xType Dim Hrs(6) Dim Comm(6) Dim Counter As Long Dim ExportPath As String ExportPath = ThisWorkbook.Path & "\Export.csv"
' Turn off screen updating to make macro run faster ' Turn off alerts so Excel won't ask for confirmation to overwrite Export.csv
With Application .ScreenUpdating = False .DisplayAlerts = False End With
' Function determines how many time card items there are
LinesToDo = NumOfLines()
' Make a copy of the export template
ThisWorkbook.Worksheets("CSVTemplate").Copy Set Dest = ActiveWorkbook
' Insert enough lines in the export template to accommodate time card records
Rows("29:" & (29 + LinesToDo - 1)).Insert Range("a29").Select ThisWorkbook.Activate Worksheets("Timecard").Select
' Loop populates variables with info from a time card record, and then writes those ' data to the export file. Loop terminates when it finds a blank cell time card record
Do Until ActiveCell = "" With ActiveCell Proj = .Value Task = .Offset(0, 1) xType = .Offset(0, 2) For Counter = 0 To 6 Hrs(Counter) = .Offset(0, Counter + 4) Comm(Counter) = .Offset(1, Counter + 4) Next End With ActiveCell.Offset(2, 0).Select Dest.Activate With ActiveCell .Value = Proj .Offset(0, 1) = Task .Offset(0, 2) = xType For Counter = 0 To 6 .Offset(0, Counter * 2 + 3) = Hrs(Counter) .Offset(0, Counter * 2 + 4) = Comm(Counter) Next End With ActiveCell.Offset(1, 0).Select ThisWorkbook.Activate Loop
' Save and close export file
Dest.SaveAs ExportPath, xlCSV Dest.Close ThisWorkbook.Activate
' Put alerts back on
With Application .ScreenUpdating = True .DisplayAlerts = True End With MsgBox "Export file saved to: " & ExportPath
End Sub Private Function NumOfLines() As Long ' Function determines how many time card entries are present
NumOfLines = 0 Range("c9").Select ' Loop counts time card records Do Until ActiveCell = "" NumOfLines = NumOfLines + 1 ActiveCell.Offset(2, 0).Select Loop Range("c9").Select
End Function
|
|
|
|
|