1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
Option Explicit
Sub mainExe()
Call ImportCSVs
Call ListWorkbooks
End Sub
Sub ImportCSVs()
'Author: Jerry Beaucaire
'Date: 8/16/2010
'Summary: Import all CSV files from a folder into separate sheets
' named for the CSV filenames
'Update: 2/8/2013 Macro replaces existing sheets if they already exist in master workbook
Dim fPath As String
Dim fCSV As String
Dim wbCSV As Workbook
Dim wbMST As Workbook
Set wbMST = ThisWorkbook
fPath = ThisWorkbook.Path & "\" 'path to CSV files, include the final \
Debug.Print "fPath:" & fPath
Application.ScreenUpdating = False 'speed up macro
Application.DisplayAlerts = False 'no error messages, take default answers
fCSV = Dir(fPath & "*.csv") 'start the CSV file listing
On Error Resume Next
Do While Len(fCSV) > 0
Set wbCSV = Workbooks.Open(fPath & fCSV) 'open a CSV file
wbCSV.Sheets(ActiveSheet.Name).Delete 'delete sheet if it exists
'wbMST.ActiveSheet.TextFileSemicolonDelimiter = True
ActiveSheet.Move After:=wbMST.Sheets(wbMST.Sheets.Count) 'move new sheet into Mstr
'wbMST.ActiveSheet.TextFileSemicolonDelimiter = True
Columns.AutoFit 'clean up display
'ActiveSheet.TextFileSemicolonDelimiter = True
fCSV = Dir 'ready next CSV
Loop
Application.ScreenUpdating = True
Set wbCSV = Nothing
End Sub
Sub ListWorkbooks()
Dim wb As Workbook
Dim ws As Worksheet
Dim wbW As Workbook
Dim wsW As Worksheet
Dim fPath As String
Dim NameStr As String
Dim i As Single, j As Single
Dim k As Long
Dim lCol As Long
Dim NbIrreduciblePoly As Long
Dim NbPolyTested As Long
Set wbW = ThisWorkbook
Set wsW = ThisWorkbook.Worksheets("Work")
wsW.Cells.Select
Selection.ClearContents
fPath = ThisWorkbook.Path & "\"
For j = 1 To Workbooks.Count
wsW.Cells(j, 1) = Workbooks(j).Name
Set wb = Workbooks(j)
For i = 1 To Workbooks(j).Sheets.Count
Set ws = wb.Sheets(i)
'i stay at 1 value (CSV files) only one sheet possible!
wsW.Cells(j, i + 1) = ws.Name
If j > 1 Then
NameStr = "PrimitivesPoly"
If InStr(1, ws.Name, NameStr, vbTextCompare) = 0 Then
'here it's a mult or add file
ws.Cells(1, 1).EntireRow.Interior.ColorIndex = 7
ws.Cells(1, 1).EntireColumn.Interior.ColorIndex = 7
' ws.Cells(1, 1).EntireRow.HorizontalAlignment = xlCenter
' ws.Cells(1, 1).EntireColumn.HorizontalAlignment = xlCenter
' ws.Cells(1, 1).EntireRow.VerticalAlignment = xlCenter
' ws.Cells(1, 1).EntireColumn.VerticalAlignment = xlCenter
ws.Cells(1, 1).EntireRow.RowHeight = 25
ws.Cells.Columns.VerticalAlignment = xlCenter
ws.Cells.Columns.HorizontalAlignment = xlCenter
Else
'here it's a Primitives Polynomial files
ws.Cells(1, 1).EntireRow.Interior.ColorIndex = 7
ws.Cells(2, 1).Interior.ColorIndex = 7
' ws.Cells(1, 1).EntireRow.HorizontalAlignment = xlCenter
' ws.Cells(1, 1).EntireColumn.HorizontalAlignment = xlCenter
' ws.Cells(1, 1).EntireRow.VerticalAlignment = xlCenter
' ws.Cells(1, 1).EntireColumn.VerticalAlignment = xlCenter
ws.Cells(1, 1).EntireRow.RowHeight = 25
ws.Cells.Columns.VerticalAlignment = xlCenter
ws.Cells.Columns.HorizontalAlignment = xlCenter
'Find the last non-blank cell in row 1
lCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Debug.Print "end of column:" & lCol
NbIrreduciblePoly = 0
NbPolyTested = 0
For k = 1 To lCol
If ws.Cells(2, k) = 1 Then
ws.Cells(2, k).Interior.Color = RGB(100, 250, 100)
NbIrreduciblePoly = NbIrreduciblePoly + 1
End If
NbPolyTested = NbPolyTested + 1
Next k
ws.Cells(3, 1) = "NbIrreduciblePoly:"
ws.Cells(3, 2) = NbIrreduciblePoly
ws.Cells(4, 1) = "NbPolyTested:"
ws.Cells(4, 2) = NbPolyTested
'Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = NameStr & "Classified"
End If
' save file in xls format xlExcel9795 43 Excel9795
'workbook.SaveAs(FileName, FileFormat)
wb.SaveAs Filename:=fPath & wb.Sheets(i).Name, FileFormat:=xlWorkbookNormal
End If
Set ws = Nothing
Next i
Set wb = Nothing
Next j
End Sub |
Partager