Because I just started to use VBA last friday, and I want to learn it more efficiently I would like to ask if you want to review my VBA code.
To keep it clear what it should do, I will give some details about my excel sheets. If you just want to see the code, please scroll down
Sheet1 = Data entry
Sheet 2 = Database
Database
The main thing is my database. This is the most important sheet of my whole workbook.
I keep a record of certificates and I need them to update every 6 months. After 2 years, the certificates receive a 'major update' and the process restarts.
To keep me informed about when a certificate has to be updated, and which certificates are allready updated I build checkboxes with VBA. This checkbox has 3 functions:
1) It keeps me informed in what 'fase' the certificate is. So 2 checks are 1 year.
2) It adds a certain amount of days to the startdate of the certificate, so that excel can give me a signal when an update is needed.
3) When checked, certain information is copied to sheet 1, where information is updated (information about the certificate)
After the two years, and 4 filled checkboxes, a commandbutton is pressed to uncheck al the checkboxes, and the process is restarted.
So every row of information has four checkboxes, and 1 commandbutton. The only problem with this process is that it requires 750 buttons, when i have 150 rows of information.
In row 1, this is the code:
Checkbox 1
Checkbox 2, 3 and 4 (small changes in code)
Commandbutton:
Data entry
Here I want two things.
1) New data entry and write thit to the last line in the database:
and 2) Modify certificate information and write this to a second (now it is sheet 3, this can be changed) database.
I really hope you would give me some comments on my vba codes! I know it is a lot of information, but without it, it makes no sense.
To keep it clear what it should do, I will give some details about my excel sheets. If you just want to see the code, please scroll down
Sheet1 = Data entry
Sheet 2 = Database
Database
The main thing is my database. This is the most important sheet of my whole workbook.
I keep a record of certificates and I need them to update every 6 months. After 2 years, the certificates receive a 'major update' and the process restarts.
To keep me informed about when a certificate has to be updated, and which certificates are allready updated I build checkboxes with VBA. This checkbox has 3 functions:
1) It keeps me informed in what 'fase' the certificate is. So 2 checks are 1 year.
2) It adds a certain amount of days to the startdate of the certificate, so that excel can give me a signal when an update is needed.
3) When checked, certain information is copied to sheet 1, where information is updated (information about the certificate)
After the two years, and 4 filled checkboxes, a commandbutton is pressed to uncheck al the checkboxes, and the process is restarted.
So every row of information has four checkboxes, and 1 commandbutton. The only problem with this process is that it requires 750 buttons, when i have 150 rows of information.
In row 1, this is the code:
Checkbox 1
Code:
Private Sub Checkbox1_Change()
If CheckBox1.Value = True Then
Datum = Worksheets("Sheet2").Range("H4").Value
Worksheets("Sheet2").Range("H4").Value = Datum + 150
Datum = Worksheets("Sheet2").Range("I4").Value
Worksheets("Sheet2").Range("I4").Value = Datum + 183
Dim CopyRange As Range, NextCell As Range
Set CopyRange = Sheet2.Range("A4,B4,E4")
Set NextCell = Sheet1.Range("B65536").End(xlUp).Offset(1, 0)
CopyRange.Copy
NextCell.PasteSpecial (xlValues)
Application.CutCopyMode = False
Sheets("Sheet1").Select
If CheckBox1.Value = False Then
Else
Exit Sub
End If
End If
End Sub
Code:
Private Sub Checkbox2_Change()
If CheckBox1.Value = False Then
Exit Sub
Else
If CheckBox2.Value = True Then
Datum = Worksheets("Sheet2").Range("H4").Value
Worksheets("Sheet2").Range("H4").Value = Datum + 150
Datum = Worksheets("Sheet2").Range("I4").Value
Worksheets("Sheet2").Range("I4").Value = Datum + 183
Dim CopyRange As Range, NextCell As Range
Set CopyRange = Sheet2.Range("A4,B4,E4")
Set NextCell = Sheet1.Range("B65536").End(xlUp).Offset(1, 0)
CopyRange.Copy
NextCell.PasteSpecial (xlValues)
Application.CutCopyMode = False
Sheets("Sheet1").Select
Else
Exit Sub
End If
End If
End Sub
Commandbutton:
Code:
Private Sub CommandButton1_click()
If CommandButton1.Enabled = True Then
CheckBox1.Value = False
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
Else
If CommandButton1.Enabled = False Then
Exit Sub
End If
End If
End Sub
Data entry
Here I want two things.
1) New data entry and write thit to the last line in the database:
Code:
Private Sub CommandButton1_click()
Call Wegschrijven_data
End Sub
Private Sub Wegschrijven_data()
Dim CopyRange As Range, NextCell As Range
Set CopyRange = Sheet1.Range("B7:G7")
Set NextCell = Sheet2.Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
CopyRange.Copy
NextCell.PasteSpecial (xlValues)
Application.CutCopyMode = False
Worksheets("Sheet1").Range("B7:G7").Select
Selection.ClearContents
End Sub
and 2) Modify certificate information and write this to a second (now it is sheet 3, this can be changed) database.
Code:
Private Sub CommandButton3_click()
Call Wegschrijven_data2
End Sub
Private Sub Wegschrijven_data2()
Dim CopyRange As Range, NextCell As Range
Set CopyRange = Sheet1.Range("B12:F" & Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Row)
Set NextCell = Sheet3.Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
CopyRange.Copy
NextCell.PasteSpecial (xlValues)
Application.CutCopyMode = False
Worksheets("Sheet1").Range("B12:F" & Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Row).ClearContents
Selection.ClearContents
End Sub
I really hope you would give me some comments on my vba codes! I know it is a lot of information, but without it, it makes no sense.