SaraWitch
Active Member
- Joined
- Sep 29, 2015
- Messages
- 389
- Office Version
- 365
- Platform
- Windows
Hello peeps.
I have a waiting list where I want a checkbox to appear in column V if data is entered in column C, from row 3. I have a code that (kind of!) works (I just have to manually add a checkbox to V3). But the code's not working to delete the checkbox when the row is cut or deleted (as well as row deletion, I want functionality to be able to move cancelled/completed applications to another sheet).
Any help would be appreciated!
I have a waiting list where I want a checkbox to appear in column V if data is entered in column C, from row 3. I have a code that (kind of!) works (I just have to manually add a checkbox to V3). But the code's not working to delete the checkbox when the row is cut or deleted (as well as row deletion, I want functionality to be able to move cancelled/completed applications to another sheet).
¦ MrExcel Queries.xlsm | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | |||
3 | test | Active | FALSE | ||||||||||||||||||||
4 | test | Active | |||||||||||||||||||||
5 | test | Active | |||||||||||||||||||||
6 | test | Active | |||||||||||||||||||||
LEC Current Waiting List (2) |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D3:D6 | D3 | =IF(C3="","",IF(NOT(ISBLANK(U3)),"Complete",IF(V3=TRUE,"Cancelled/Removed",IF(R3="Offered","Offer in progress",IF(R3="Accepted","Offer accepted",IF(S3=1,"One refused offer",IF(S3=2,"Two refused offers",IF(S3=3,"Three refused offers", "Active")))))))) |
J3:J6 | J3 | =IF(K3="","",IF(ISNUMBER(MATCH(K3,'[LEC Garage Waiting List.xlsm]Lookups'!$E$4:$E$12, 0)), "Eastbourne Borough Council", "Lewes District Council")) |
Cells with Conditional Formatting | ||||
---|---|---|---|---|
Cell | Condition | Cell Format | Stop If True | |
V3:V1048576 | Expression | =$V3="TRUE" | text | NO |
B3:V6 | Expression | =$D3="Cancelled" | text | NO |
B3:V6 | Expression | =$D3="Complete" | text | NO |
B3:V6 | Expression | =$D3="Three refused offers" | text | NO |
B3:V6 | Expression | =$D3="Two refused offers" | text | NO |
B3:V6 | Expression | =$D3="One refused offer" | text | NO |
B3:V6 | Expression | =$D3="Offer in progress" | text | NO |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
E3:E6 | List | =Priority |
R3:R1048576 | List | =OfferStatus |
S3:S1048576 | List | =NoOfOffers |
U3:U6 | Date | between 01/01/1900 and 31/12/3000 |
I3:I6 | Date | between 01/01/1900 and 31/12/3000 |
N3 | List | =YesNo |
K3:L6 | List | =Area |
M3:M6 | List | =IntendedUse |
N4:O1048576 | List | =Area |
P4:P1048576 | List | =IntendedUse |
Q4:Q1048576 | List | =YesNo |
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim chkBox As CheckBox
Dim cmToPoints As Double
Dim chkWidth As Double, chkHeight As Double
Dim lastRow As Long
Dim rowNum As Long
Dim cell As Range
Dim checkboxExists As Boolean
' Set the worksheet to the active sheet
Set ws = Me
cmToPoints = 28.35 ' Conversion factor from cm to points
' Define the size of the checkbox (in points)
chkWidth = 0.6 * cmToPoints ' 0.6 cm in points
chkHeight = 0.59 * cmToPoints ' 0.59 cm in points
' Determine the last row with data in column C
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
' Check if the change is within the range of column C (where new data is entered)
If Not Intersect(Target, ws.Range("C3:C" & lastRow)) Is Nothing Then
' Loop through each cell in the changed range
For Each cell In Intersect(Target, ws.Range("C3:C" & lastRow))
rowNum = cell.Row
checkboxExists = False
' Check if there's already a checkbox in the current row in column V
For Each chkBox In ws.CheckBoxes
If chkBox.TopLeftCell.Row = rowNum Then
checkboxExists = True
Exit For
End If
Next chkBox
' Only add a checkbox if one does not already exist
If Not checkboxExists Then
' Add the checkbox to the current row in column V
Set chkBox = ws.CheckBoxes.Add(Top:=ws.Cells(rowNum, "V").Top, _
Left:=ws.Cells(rowNum, "V").Left, _
Width:=chkWidth, _
Height:=chkHeight)
' Link the checkbox to the same cell in column V
chkBox.LinkedCell = ws.Cells(rowNum, "V").Address
' Optional: Set the checkbox to default to uncheck
chkBox.Value = xlOff
' Remove the text from the checkbox (this clears the label in the cell)
chkBox.Text = ""
' Center the checkbox horizontally and vertically within the cell
chkBox.Top = ws.Cells(rowNum, "V").Top + (ws.Cells(rowNum, "V").Height - chkHeight) / 2
chkBox.Left = ws.Cells(rowNum, "V").Left + (ws.Cells(rowNum, "V").Width - chkWidth) / 2
End If
Next cell
End If
' Check if the data in column C is cleared (row deleted or cut)
If Not Intersect(Target, Me.Range("C3:C" & lastRow)) Is Nothing Then
For Each cell In Intersect(Target, Me.Range("C3:C" & lastRow))
If IsEmpty(cell.Value) Then
' Row is cleared, now delete the checkbox from the corresponding row in column V
For Each chkBox In ws.CheckBoxes
If chkBox.TopLeftCell.Row = cell.Row Then
chkBox.Delete
End If
Next chkBox
End If
Next cell
End If
End Sub
Any help would be appreciated!
