Option Explicit
Option Compare Text
Public ButtonEvents As Collection, ButtonEvent
Public Const NL As String = vbNewLine
Public Const DNL As String = vbNewLine & vbNewLine
Public Const TB As String = vbTab
Public Const DTB As String = vbTab & vbTab
Sub UpdateQuery()
Dim wb As Workbook, ws As Worksheet
Dim wbTEMP As Workbook, wsTEMP As Worksheet
Dim rngLook As Range, rngFind As Range, c As Range, r As Range, x As Range
Dim rngMIRNLoop As Range, rngMIRN As Range
Dim strRptFolder As String, strFolder As String, strFile As String
Dim strPathTEMP As String, strFileTEMP As String, strDate As String
Dim i As Long, lngMIRN As Long, fDate As Date, iRow As Long, iMIRN As Variant
Dim blnWasOpenTEMP As Boolean, blnDataCopied As Boolean
strRptFolder = "C:\Reports"
strFolder = "C:\"
strFile = "Data_Monitor_2006_test.xls"
If FolderExists(strRptFolder) = False Then
MsgBox "Folder """ & strRptFolder & """ does not exist!" & DNL & "Project halted.", vbCritical, "ERROR!"
GoTo ExitHere
End If
With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With
With Application.FileSearch
.fileName = "*.csv"
.SearchSubFolders = False
.LookIn = strRptFolder
.Execute msoSortByFileName
If .FoundFiles.Count > 0 Then
If IsWbOpen(strFile) Then
Set wb = Workbooks(strFile)
Else
Set wb = Workbooks.Open(strFolder & strFile)
End If
Set ws = wb.Sheets(1) 'assumes it's the first sheet
Set rngLook = ws.Range("A:A")
For i = 1 To .FoundFiles.Count
Application.StatusBar = Format(i / .FoundFiles.Count, "Percent") & " complete.."
If Right(.FoundFiles(i), 3) <> "csv" Then GoTo SkipWbTEMP
strFileTEMP = Right(.FoundFiles(i), Len(.FoundFiles(i)) - InStrRev(.FoundFiles(i), "\"))
strPathTEMP = Left(.FoundFiles(i), Len(.FoundFiles(i)) - Len(strFileTEMP))
strDate = CStr(Mid(strFileTEMP, InStrRev(strFileTEMP, "_") + 1, 8))
If IsWbOpen(strFileTEMP) Then
blnWasOpenTEMP = True
Set wbTEMP = Workbooks(strFileTEMP)
Else
blnWasOpenTEMP = False
Set wbTEMP = Workbooks.Open(strPathTEMP & strFileTEMP)
End If
Set wsTEMP = wbTEMP.Sheets(1) 'assuming it's the first sheet
Set c = wsTEMP.Range("C2")
Set rngFind = rngLook.Find(c.Value)
blnDataCopied = True
wb.Activate
If rngFind Is Nothing Then
iRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
Else
iRow = rngFind.Row
End If
Set x = ws.Cells(iRow, 1)
fDate = DateSerial(CLng(Left(strDate, 4)), _
CLng(Mid(strDate, 5, 2)), CLng(Right(strDate, 2)))
Set r = wsTEMP.Cells(1, wsTEMP.Columns.Count).End(xlToLeft).Offset(0, -2)
lngMIRN = CLng(Right(r.Value, 2))
'// New Row of data
If Not rngFind Is Nothing Then
x.EntireRow.Insert
Set x = x.Offset(-1)
End If
'// Gas Day value
x.Value = c.Value
x.NumberFormat = "dd-mmm-yy"
'// Calendar Day value (from filename)
x.Offset(, 1).Value = fDate
x.Offset(, 1).NumberFormat = "dd-mmm-yy"
'// Filename to Remco
x.Offset(, 5).Value = strFileTEMP
'// Total MIRN's
x.Offset(, 6).Value = wsTEMP.Cells(wsTEMP.Rows.Count, 1).End(xlUp).Row - 1
'// Enter as Original or Latest file
' If x.Row = 33 Then Stop
If rngFind Is Nothing Then x.Offset(, 2).Value = "O"
If Not rngFind Is Nothing Then x.Offset(, 3).Value = "L"
'// Total of Type_Of_Read A
x.Offset(, 7).Value = WorksheetFunction.CountIf(wsTEMP.Cells(1, wsTEMP.Columns.Count).End(xlToLeft).EntireColumn, "A")
'// Total of Type_Of_Read E
x.Offset(, 8).Value = WorksheetFunction.CountIf(wsTEMP.Cells(1, wsTEMP.Columns.Count).End(xlToLeft).EntireColumn, "E")
'// Total of Type_Of_Read S
x.Offset(, 9).Value = WorksheetFunction.CountIf(wsTEMP.Cells(1, wsTEMP.Columns.Count).End(xlToLeft).EntireColumn, "S")
Set rngFind = Nothing
Set rngMIRNLoop = ws.Range("K" & x.Row & ":" & ColLet(ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column) & x.Row)
Debug.Print rngMIRNLoop.Address
For Each rngMIRN In rngMIRNLoop
Debug.Print rngMIRN.Address(0, 0)
iMIRN = Evaluate("=VLOOKUP(" & ColLet(rngMIRN.Column) & "2," & strFileTEMP & "!A:AC,28,0)")
If Not IsError(iMIRN) Then
ws.Cells(iRow, rngMIRN.Column).Value = iMIRN
End If
Next rngMIRN
If Len(ws.Range("GP" & x.Row).Value) = 0 Then ws.Range("GP" & x.Row).Value = 0
ws.Range("A" & x.Row & ":E" & x.Row).HorizontalAlignment = xlCenter
ws.Range("F" & x.Row & ":J" & x.Row).InsertIndent 1
ws.Range("GQ" & x.Row).Formula = "=SUM(K" & x.Row & ":GP" & x.Row & ")/1000000"
ws.Range("K5:GO" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row).NumberFormat = "0,000"
If blnWasOpenTEMP = False Then
wbTEMP.Close False
End If
Set wbTEMP = Nothing
Set wsTEMP = Nothing
SkipWbTEMP:
Next i
End If
End With
With ws.Range("A5", ws.Cells(ws.Cells(ws.Rows.Count, 1).End(xlUp).Row, ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column))
.Sort Key1:=ws.Range("A5"), Order1:=xlAscending, Key2:=ws.Range("F5"), Order2:=xlAscending, Header:=xlNo
End With
ExitHere:
With Application
.CutCopyMode = False
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
.StatusBar = False
End With
If blnDataCopied = True Then
MsgBox "Data copied over.", vbInformation, "Complete!"
Else
MsgBox "No data was copied over.", vbInformation, "Complete!"
End If
End Sub