Need to read multiple CSV files (e.g. file1, file2..) from folder1 and write each of these files as a separate sheet in the target excel. Also, read similar CSV files(e.g. file1, file2..) from folder2 and write each csv file to the above excel in the already existing sheets.
**** Source ****
Folder1:
file1:
col1, col2, col3
A, B, C
1, 2, 3
file2:
col1, col2, col3
U, V, W
4, 5, 6
Folder2:
file1:
col1, col2, col3
D, E, F
1, 2, 3
file2:
col1, col2, col3
X, Y, Z
4, 5, 6
**** Target ****
Excel:
sheet1 (file1):
col1, col2, col3 col1, col2, col3
A, B, C, D, E, F
1, 2, 3, 4, 5, 6
sheet2 (file2):
col1, col2, col3 col1, col2, col3
U, V, W, X, Y, Z
4, 5, 6, 4, 5, 6
Code:
Private Sub Workbook_Open()
Call OpenCSVFile
End Sub
Sub OpenCSVFile()
Dim prevRunFolderPath As String
Dim destFolderPath As String
Dim prevFileName As String
Dim destFileName As String
Dim row_count As Integer
Dim comp_count As Integer
Dim lineFromFile As String
Dim lineItems() As String
Dim wb As Workbook
Dim prevVer As String
Dim currVer As String
Dim fileExtn As String
prevRunFolderPath = "X:\"
currRunFolderPath = "Y:\"
destFolderPath = "Z:\"
prevFileName = "file1.CSV"
currFileName = "file2.CSV"
destFileName = "Compare.xlsx"
fileExtn = "*.CSV*"
' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
Open prevRunFolderPath & prevFileName For Input As #1
Open currRunFolderPath & currFileName For Input As #2
Set wb = Workbooks.Add
' Workbooks.Add.SaveAs Filename:=destFolderPath & destFileName
wb.SaveAs Filename:=destFolderPath & destFileName
'ObjExcel.Add.SaveAs Filename:=destFolderPath & destFileName
'Workbooks(destFolderPath & destFileName).Activate
'ActiveWorkbook.Worksheets.Add.Name = "Test"
row_count = 1
prevVer = "Prev-Ver"
currVer = "Curr-Ver"
'*** Below sets the column headers ****
wb.Worksheets("Sheet1").Range("A" & row_count).Value = prevVer & "-Desc"
wb.Worksheets("Sheet1").Range("B" & row_count).Value = prevVer & "-Scenario"
wb.Worksheets("Sheet1").Range("C" & row_count).Value = prevVer & "-TC1"
wb.Worksheets("Sheet1").Range("D" & row_count).Value = prevVer & "-TC2"
wb.Worksheets("Sheet1").Range("E" & row_count).Value = prevVer & "-Status"
wb.Worksheets("Sheet1").Range("F" & row_count).Value = currVer & "-Desc"
wb.Worksheets("Sheet1").Range("G" & row_count).Value = currVer & "-Scenario"
wb.Worksheets("Sheet1").Range("H" & row_count).Value = currVer & "-TC1"
wb.Worksheets("Sheet1").Range("I" & row_count).Value = currVer & "-TC2"
wb.Worksheets("Sheet1").Range("J" & row_count).Value = currVer & "-Status"
wb.Worksheets("Sheet1").Range("K" & row_count).Value = "TC1-Comp"
wb.Worksheets("Sheet1").Range("L" & row_count).Value = "TC2-Comp"
wb.Worksheets("Sheet1").Range("M" & row_count).Value = "Status-Comp"
row_count = 2
*** Below loops through file1 and write the data to target excel ****
Do Until EOF(1)
Line Input #1, lineFromFile
'MsgBox "Line is" & LineFromFile
lineItems = Split(lineFromFile, ",")
wb.Worksheets("Sheet1").Range("A" & row_count).Value = lineItems(0)
wb.Worksheets("Sheet1").Range("B" & row_count).Value = lineItems(1)
wb.Worksheets("Sheet1").Range("C" & row_count).Value = lineItems(2)
wb.Worksheets("Sheet1").Range("D" & row_count).Value = lineItems(3)
wb.Worksheets("Sheet1").Range("E" & row_count).Value = lineItems(4)
row_count = row_count + 1
' ActiveCell.Offset(row_number,
Loop 'Until row_count > 4
row_count = 2
'*** Below loops through file2 and write the data to target excel ****
Do Until EOF(2)
Line Input #2, lineFromFile
'MsgBox "Line is" & LineFromFile
lineItems = Split(lineFromFile, ",")
wb.Worksheets("Sheet1").Range("F" & row_count).Value = lineItems(0)
wb.Worksheets("Sheet1").Range("G" & row_count).Value = lineItems(1)
wb.Worksheets("Sheet1").Range("H" & row_count).Value = lineItems(2)
wb.Worksheets("Sheet1").Range("I" & row_count).Value = lineItems(3)
wb.Worksheets("Sheet1").Range("J" & row_count).Value = lineItems(4)
row_count = row_count + 1
' ActiveCell.Offset(row_number,
Loop 'Until row_count > 4
comp_count = 2
'*** Below does comparisions based on the data written to the target excel ***
Do
wb.Worksheets("Sheet1").Range("K" & comp_count).Value = "=C" & comp_count & "=H" & comp_count
wb.Worksheets("Sheet1").Range("L" & comp_count).Value = "=D" & comp_count & "=I" & comp_count
wb.Worksheets("Sheet1").Range("M" & comp_count).Value = "=E" & comp_count & "=J" & comp_count
comp_count = comp_count + 1
' ActiveCell.Offset(row_number,
Loop Until comp_count > row_count
' Close outFileName
Close #1
Close #2
wb.Save
wb.Close
End Sub
Not sure how I can replicate it for the rest of the CSV files as all the CSV files content is being written to a single sheet instead of individual sheets.