Option Explicit
Dim xRow As Integer
Dim fPath As String, fCSV As String
Dim wbCSV As Workbook, wbMST As Workbook
Dim myInSht As Worksheet, myOutSht As Worksheet, xSheet As Worksheet
Dim aRow As Range, aCol As Range, myInCol As Range, myOutCol As Range, cell As Range
Dim shortmodifdate As Variant, longmodifdate As Variant, modiftime As Variant, entrytime As Double, exittime As Double
Dim calcState As Long, scrUpdateState As Long, iLoop As Long, jLoop As Long, calcState As Long, scrUpdateState As Long
Sub ImportCSVs()
Set wbMST = ThisWorkbook
fPath = "C:\Users\***\Documents\test" 'path to CSV files, include the final \
Application.ScreenUpdating = False 'speed up macro
Application.DisplayAlerts = False 'no error messages, take default answers
fCSV = Dir(fPath & "*.csv") 'start the CSV file listing
OnErrorResumeNext
DoWhile Len(fCSV) > 0
Set wbCSV = Workbooks.Open(fPath & fCSV) 'open a CSV file
wbMST.Sheets(ActiveSheet.Name).Delete 'delete sheet if it exists
ActiveSheet.Move After:=wbMST.Sheets(wbMST.Sheets.Count) 'move new sheet into Mstr
Columns.AutoFit 'clean up display
fCSV = Dir 'ready next CSV
longmodifdate = ThisWorkbook.BuiltinDocumentProperties("Last Save Time")
entrytime = 0.583331 ' numeric value for 2:00 pm you can change if needed
exittime = 0.958331 ' numerci value for 11:00 pm you can change if needed
longmodifdate = CDbl(longmodifdate)
shortmodifdate = CLng(longmodifdate)
modiftime = longmodifdate - shortmodifdate ' return only the time
If modiftime < 0 Then ' after noon the time will be negative so we need to revert back to positive value
modiftime = modiftime * (-1)
End If
If modiftime > entrytime And modiftime > exittime Then
tosave = True ' When true the last time modified is between 2pm and 11 pm
End If
If tosave = True Then
Set wbCSV = Nothing
Call Collect
Call Worksheet_Activate
Else
'do what you need to do
End If
Loop
Application.ScreenUpdating = True
End Sub
Sub Collect()
jLoop = 2
' loop through the worksheets
For Each myInSht In ActiveWorkbook.Worksheets
' pick only the worksheets of interest
'If myInSht.Name = "a" Or myInSht.Name = "aa" Or myInSht.Name = "aaa" Then
' find the columns of interest in the worksheet
For Each aCol In myInSht.UsedRange.Columns
Set myOutCol = Nothing
If aCol.Cells(1, 1).Value = "timestamp" Then Set myOutCol = Sheets("Summary").Range("B2:B1000")
If aCol.Cells(1, 1).Value = "ip" Then Set myOutCol = Sheets("Summary").Range("C2:C1000")
If aCol.Cells(1, 1).Value = "protocol" Then Set myOutCol = Sheets("Summary").Range("D2:D1000")
If aCol.Cells(1, 1).Value = "port" Then Set myOutCol = Sheets("Summary").Range("E2:E1000")
If aCol.Cells(1, 1).Value = "hostname" Then Set myOutCol = Sheets("Summary").Range("F2:F1000")
If aCol.Cells(1, 1).Value = "tag" Then Set myOutCol = Sheets("Summary").Range("G2:G1000")
If aCol.Cells(1, 1).Value = "asn" Then Set myOutCol = Sheets("Summary").Range("I2:I1000")
If aCol.Cells(1, 1).Value = "geo" Then Set myOutCol = Sheets("Summary").Range("J2:J1000")
If aCol.Cells(1, 1).Value = "region" Then Set myOutCol = Sheets("Summary").Range("K2:K1000")
If aCol.Cells(1, 1).Value = "naics" Then Set myOutCol = Sheets("Summary").Range("L2:L1000")
If aCol.Cells(1, 1).Value = "sic" Then Set myOutCol = Sheets("Summary").Range("M2:M1000")
If aCol.Cells(1, 1).Value = "server_name" Then Set myOutCol = Sheets("Summary").Range("H2:H1000")
If Not myOutCol Is Nothing Then
' don't move the top line, it contains the headers - no data
Set myInCol = aCol
Set myInCol = myInCol.Offset(1, 0).Resize(myInCol.Rows.Count, myInCol.Columns.Count)
' transfer data from the project tab to the consolidated tab
iLoop = jLoop
For Each aRow In myInCol.Rows
myOutCol.Cells(iLoop, 1).Value = aRow.Cells(1, 1).Value
iLoop = iLoop + 1
Next aRow
End If
Next aCol
If iLoop > jLoop Then jLoop = iLoop
Next myInSht
End Sub
Sub Worksheet_Activate()
'Updateby20150305
Application.ScreenUpdating = False
xRow = 4
With Me
.Columns(1).ClearContents
'.Cells(1, 1) = "Summary"
'.Cells(1, 1).Name = "Summary"
End With
For Each xSheet In Application.Worksheets
If xSheet.Name <> Me.Name Then
xRow = xRow + 1
With xSheet
.Range("P1").Name = "Start_" & xSheet.Index
.Hyperlinks.Add Anchor:=.Range("P1"), Address:="", _
SubAddress:="Summery", TextToDisplay:="time stamp"
End With
Me.Hyperlinks.Add Anchor:=Me.Cells(xRow, 1), Address:="", _
SubAddress:="Start_" & xSheet.Index, TextToDisplay:=xSheet.Name
End If
Next
Application.ScreenUpdating = True
End Sub