Option Explicit
Option Private Module
Public InvoerbestandCheck As Boolean, FinanceSheetCheck As Boolean, WeekKolomCheck As Boolean
Public KPIPad As String, KPIFile As String, FinanceSheet As String, FinanceSheetKolom As String
Public ShtSel As Worksheet
Public Check As Integer
Public strPath As String, strFile As String, strSheet As String, strCell As String, strTable As String, strColumn As String, strTarget As String, strFormula As String
Sub DataOphalen()
START:
'On Error GoTo Einde
Application.ScreenUpdating = True 'Switch to False when routine works correctly.
Application.DisplayAlerts = False
Call PROTOFF
Call Validatie
If Check <> 0 Then
MsgBox "De VBA Code is niet volledig door de validatie heen gekomen. De bewerking wordt afgebroken.", vbCritical, "Bewerking afgebroken"
GoTo Einde
End If
Call GegevensOphalen
Call Timestamp
Einde:
'Call PROTON
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub Validatie()
'Controleren of het Invoerbestand en de doelgegevens bestaan:
KPIPad = ThisWorkbook.Sheets("Parameters").Range("B4").Value
KPIFile = ThisWorkbook.Sheets("Parameters").Range("B5").Value
FinanceSheet = ThisWorkbook.Sheets("Parameters").Range("B10").Value
FinanceSheetKolom = ThisWorkbook.Sheets("Parameters").Range("B11").Value
If KPIPad = "" Or KPIFile = "" Then
MsgBox "De waarde voor de KPI/Facturatie Inputfile is nog niet aanwezig of kon niet worden gevonden.", vbExclamation, "Controle Invoerbestand"
ThisWorkbook.Sheets("Parameters").Range("I4").Value = "Nee"
InvoerbestandCheck = 1
Else
ThisWorkbook.Sheets("Parameters").Range("I4").Value = "Ja"
InvoerbestandCheck = 0
End If
If FinanceSheet = "" Then
MsgBox "De ingestelde Sheet voor de Doellocatie is nog niet aanwezig of kon niet worden gevonden.", vbExclamation, "Controle Doelgegevens"
ThisWorkbook.Sheets("Parameters").Range("I10").Value = "Nee"
FinanceSheetCheck = 1
Else
ThisWorkbook.Sheets("Parameters").Range("I10").Value = "Ja"
FinanceSheetCheck = 0
End If
If FinanceSheetKolom = "" Then
MsgBox "De ingestelde Kolom voor de Doellocatie is nog niet aanwezig of kon niet worden gevonden.", vbExclamation, "Controle Doelgegevens"
ThisWorkbook.Sheets("Parameters").Range("I11").Value = "Nee"
WeekKolomCheck = 1
Else
ThisWorkbook.Sheets("Parameters").Range("I11").Value = "Ja"
WeekKolomCheck = 0
End If
Check = InvoerbestandCheck + FinanceSheetCheck + WeekKolomCheck
End Sub
Sub GegevensOphalen()
' Variabelen:
' ===========
Dim sPath As String, sFile As String, sSheet As String, sCell As String, sTable As String, sColumn As String, sTarget As String
Dim TabelRij As Variant, Row As Range, i As Integer, Cell As Range
Dim x As Integer, y As Integer, LastRow As Integer, Smt As Double, errval As Variant
TabelRij = ThisWorkbook.Sheets("Parameters").Range("A65000").End(xlUp).Row
sPath = ThisWorkbook.Sheets("Parameters").Range("B4").Value
sFile = ThisWorkbook.Sheets("Parameters").Range("B5").Value
'
' ===============================================================
' Gegevens ophalen middels VLOOKUP voor iedere regel in de tabel:
' ===============================================================
i = 16
For Each Row In ThisWorkbook.Sheets("Parameters").Range("A16:" & "A" & TabelRij)
sSheet = ThisWorkbook.Sheets("Parameters").Range("H" & i).Value
sCell = ThisWorkbook.Sheets("Parameters").Range("I" & i).Value
sTable = ThisWorkbook.Sheets("Parameters").Range("J" & i).Value
sColumn = ThisWorkbook.Sheets("Parameters").Range("K" & i).Value
sTarget = "M" & i
Call Vlookup(sPath, sFile, sSheet, sCell, sTable, sColumn, sTarget)
i = i + 1
Next
' Opgehaalde formules omzetten naar waardes zodat de sheet snel blijft werken:
' ============================================================================
Sheets("Parameters").Range("M16:M" & TabelRij).Formula = Sheets("Parameters").Range("M16:M" & TabelRij).Value
'De kolom met opgehaalde data wordt doorzocht op #REF! errors en deze worden vervangen met 0
'===========================================================================================
For Each Cell In ThisWorkbook.Sheets("Parameters").Range("M16:M" & TabelRij)
If Cell.Value = CStr("#REF!") Then
Cell.Value = 0
End If
Next
' Relaties van opgehaalde data wordt opgezocht en doorgevoerd naar kolom Eind Resultaat:
' ======================================================================================
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 16 To LastRow
If IsNumeric(Cells(x, 13)) Then
Smt = Cells(x, 13).Value
Else
GoTo EndLine
End If
For y = 16 To LastRow
If Cells(y, 14) = x Then
Smt = Smt + Cells(y, 13).Value
End If
Next y
Cells(x, 15).Value = Smt
EndLine:
Next x
End Sub
Sub OpenKPIMap()
KPIPad = ThisWorkbook.Sheets("Parameters").Range("B4").Value
Shell "Explorer.exe" & " " & KPIPad, vbNormalFocus
End Sub
Sub KPIBestandKiezen()
Application.DisplayAlerts = False
Call PROTOFF
Dim SelectedFile As String
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Kies het gewenste KPI Invoerbestand"
.InitialFileName = "U:\NDNL_Tilburg\03_TLB_Operatie\3.1) Customer Service\Customer Service Support Robby\KPI\"
If .Show = -1 Then
'Ok clicked
SelectedFile = .SelectedItems(1)
ThisWorkbook.Sheets("Parameters").Range("B4").Value = GetFoldernameFromPath(SelectedFile)
ThisWorkbook.Sheets("Parameters").Range("B5").Value = GetFilenameFromPath(SelectedFile)
Else
'Cancel clicked
End If
End With
Call PROTON
Application.DisplayAlerts = True
End Sub
Sub SelectSheet()
Application.DisplayAlerts = False
Call PROTOFF
Dim cmdBar As CommandBar
Dim cmdBarBtn As CommandBarButton
Dim Sht As Worksheet
RegDel
On Error Resume Next
Set cmdBar = Application.CommandBars.Add("Register", msoBarPopup)
For Each Sht In ThisWorkbook.Worksheets
Set cmdBarBtn = cmdBar.Controls.Add
cmdBarBtn.Caption = Sht.Name
cmdBarBtn.Style = msoButtonCaption
cmdBarBtn.OnAction = "SelectThatSheet"
Next Sht
cmdBar.ShowPopup
On Error GoTo Einde
'MsgBox "you selected sheet '" & ShtSel.Name & "'"
If ShtSel.Name = "" Then
MsgBox "Er is geen Sheet geselecteerd.", vbExclamation, "Controle Invoer geldige Sheet"
ThisWorkbook.Sheets("Parameters").Range("B10").SetFocus
Exit Sub
Else
ThisWorkbook.Sheets("Parameters").Range("B10").Value = ShtSel.Name
End If
Einde:
Call PROTON
Application.DisplayAlerts = True
End Sub
Sub SelectThatSheet()
Set ShtSel = Worksheets(Application.Caller(1))
RegDel
End Sub
Sub RegDel()
On Error Resume Next
Application.CommandBars("Register").Delete
On Error GoTo 0
End Sub
Sub RangeSelectionPrompt()
Application.DisplayAlerts = False
Call PROTOFF
Dim rng As Range, KolomL As String, KolomR As String, Kolom As String
Dim Sht As String
Sht = ThisWorkbook.Sheets("Parameters").Range("B10").Value
ThisWorkbook.Sheets(Sht).Select
On Error Resume Next
Set rng = Application.InputBox("Kies een Cell in de gewenste uitvoer kolom", "Uitvoer kolom bepalen", Type:=8)
On Error GoTo Einde
KolomL = Right(rng.Address, Len(rng.Address) - 1)
KolomR = Mid(KolomL, 1, InStrRev(KolomL, "$") - 1)
ThisWorkbook.Sheets("Parameters").Range("B11").Value = KolomR
Einde:
ThisWorkbook.Sheets("Parameters").Select
Call PROTON
Application.DisplayAlerts = True
End Sub
Sub PROTOFF()
Sheets("Parameters").Unprotect ("12345678")
End Sub
Sub PROTON()
Sheets("Parameters").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
, AllowFiltering:=True, Password:="12345678"
End Sub
Sub Timestamp()
If ThisWorkbook.Sheets("Parameters").ProtectContents = False Then
ThisWorkbook.Sheets("Parameters").Range("I7").Value = Format(Now, "DD-MM-YY HH:MM")
Else
Call PROTOFF
ThisWorkbook.Sheets("Parameters").Range("I7").Value = Format(Now, "DD-MM-YY HH:MM")
End If
End Sub
Function DoesFileExist(s_directory As String, s_fileName As String) As Boolean
DoesFileExist = _
CreateObject("Scripting.FileSystemObject").fileExists(s_directory & "\" & s_fileName)
End Function
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
Function GetFoldernameFromPath(ByVal strPath As String) As String
Dim strFullFilePath As String
strFullFilePath = strPath
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetFoldernameFromPath = fso.GetParentFolderName(strFullFilePath) & "\"
End Function
Sub Vlookup(strPath As String, strFile As String, strSheet As String, strCell As String, strTable As String, strColumn As String, strTarget As String)
Dim strFormula As String
If strPath = "" Or strFile = "" Or strSheet = "" Or strCell = "" Or strTable = "" Or strColumn = "" Or strTarget = "" Then
strFormula = "#ERR"
Else
strFormula = "=VLOOKUP(""" & strCell & """,'" & strPath & "[" & strFile & "]" & strSheet & "'!" & strTable & "," & strColumn & ",0)"
End If
ThisWorkbook.Sheets("Parameters").Range(strTarget).Formula = strFormula
End Sub