Dag iedereen,
Ik ben een beginner wat betreft VBA.
Dit wil zeggen dat ik de meeste code in het lang schrijf. Voorlopig is dat het best voor mijn eigen controles.
Onderstaande code werkt dus perfect.
Ook als ik in een ander tabblad bezig ben, wat wel belangrijk is.
Nu wordt het wel eens tijd om de code te verfraaien.
Ik heb al enkele zaken geprobeerd, zoals een range definiëren of de With-End With functie.
Helaas zonder succes.
"Hebben jullie tips om mijn code compacter te maken?"
Hartelijk Dank! Ik ben benieuwd!
Ik ben een beginner wat betreft VBA.
Dit wil zeggen dat ik de meeste code in het lang schrijf. Voorlopig is dat het best voor mijn eigen controles.
Onderstaande code werkt dus perfect.
Ook als ik in een ander tabblad bezig ben, wat wel belangrijk is.
Nu wordt het wel eens tijd om de code te verfraaien.
Ik heb al enkele zaken geprobeerd, zoals een range definiëren of de With-End With functie.
Helaas zonder succes.
"Hebben jullie tips om mijn code compacter te maken?"
Code:
Sub Macro1()
'Definiëer de start en stop datum
Dim Begin_Datum As Date
Dim Eind_Datum As Date
Begin_Datum = ThisWorkbook.Sheets("CSR Review").Range("I4").Value
Eind_Datum = ThisWorkbook.Sheets("CSR Review").Range("I5").Value
'Berekenen van start en stop cell in kolom van de datums en bereken aantal data-punten
Dim Lookup_Date As Range
Dim Start_Cell As Double
Dim Stop_Cell As Double
Set Lookup_Date = ThisWorkbook.Sheets("Analyse").Range("A:A")
Start_Cell = WorksheetFunction.Match(CLng(Begin_Datum), Lookup_Date, 1) + 1
Stop_Cell = WorksheetFunction.Match(CLng(Eind_Datum), Lookup_Date, 1) + 1
Count = Stop_Cell - Start_Cell + 1
'Berekeningen Fe MF
Dim FeMF_Column As Integer
Dim FeMF_Avg As Double
Dim FeMF_Count As Double
Dim FeMF_Count_High As Double
Dim FeMF_Count_OK As Double
FeMF_Column = ThisWorkbook.Worksheets("Analyse").Range("Fe_MF").Columns.Column
FeMF_Avg = Application.WorksheetFunction.AverageIf(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, FeMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, FeMF_Column)), "<>#N/A")
FeMF_Count = Application.WorksheetFunction.Count(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, FeMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, FeMF_Column)))
FeMF_Count_High = Application.WorksheetFunction.CountIf(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, FeMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, FeMF_Column)), ">1")
FeMF_Count_OK = Application.WorksheetFunction.CountIf(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, FeMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, FeMF_Column)), "<=1")
'Berekeningen pH MF
Dim pHMF_Column As Integer
Dim pHMF_Avg As Double
Dim pHMF_Count As Double
Dim pHMF_Count_High As Double
Dim pHMF_Count_Low As Double
Dim pHMF_Count_OK As Double
pHMF_Column = ThisWorkbook.Worksheets("Analyse").Range("pH_MF").Columns.Column
pHMF_Avg = Application.WorksheetFunction.AverageIf(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, pHMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, pHMF_Column)), "<>#N/A")
pHMF_Count = Application.WorksheetFunction.Count(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, pHMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, pHMF_Column)))
pHMF_Count_High = Application.WorksheetFunction.CountIf(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, pHMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, pHMF_Column)), ">6.5")
pHMF_Count_Low = Application.WorksheetFunction.CountIf(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, pHMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, pHMF_Column)), "<5.5")
pHMF_Count_OK = Application.WorksheetFunction.CountIfs(Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, pHMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, pHMF_Column)), "<=6.5", Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, pHMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, pHMF_Column)), ">=5.5")
'Debugging
'Range(ThisWorkbook.Worksheets("Analyse").Cells(Start_Cell, FeMF_Column), ThisWorkbook.Worksheets("Analyse").Cells(Stop_Cell, FeMF_Column)).Select
ThisWorkbook.Worksheets("Analyse").Activate
ThisWorkbook.Worksheets("Analyse").Range(Cells(Start_Cell, pHMF_Column), Cells(Stop_Cell, pHMF_Column)).Select
Debug.Print "Van Cell " & Start_Cell & " (" & Begin_Datum & ") tot Cell " & Stop_Cell & " (" & Eind_Datum & ")"
Debug.Print "Aantal cellen: " & Count
Debug.Print "Gemiddelde: " & pHMF_Avg
Debug.Print "Aantal waarden: " & pHMF_Count
Debug.Print "Te hoog: " & pHMF_Count_High
Debug.Print "Te laag: " & pHMF_Count_Low
Debug.Print "OK: " & pHMF_Count_OK
End Sub
Hartelijk Dank! Ik ben benieuwd!