Beneindias
Board Regular
- Joined
- Jun 21, 2022
- Messages
- 120
- Office Version
- 365
- Platform
- Windows
- MacOS
Hey,
I bring another problem to you people.
I have some VBA code to check the last row of a certain table and create a checkbox in that row, if there are none.
Problem is, if a row is created in the midle of the table, no checkbox is created in this row, because it is only looking for the last row.
So, my code needs to be changed, so that it loops through all rows in the table, and creates a checkbox in every row, that does not have a checkbox in column B.
This is my code that checks if a Checkbox exists:
This is the code that creates the checkbox:
I'm trying to add a "for each" loop, so the code loops by every row, but I wasn't able to do that yet.
Can anybodu help me with this, as I don't have much free time to have this file ready to be used in my company.
Another thing, I have that "lastRow + 7", because my table starts in row 7. Can this be done without this part? because if someone adds a row before the table, this code will break.
Cheers
I bring another problem to you people.
I have some VBA code to check the last row of a certain table and create a checkbox in that row, if there are none.
Problem is, if a row is created in the midle of the table, no checkbox is created in this row, because it is only looking for the last row.
So, my code needs to be changed, so that it loops through all rows in the table, and creates a checkbox in every row, that does not have a checkbox in column B.
This is my code that checks if a Checkbox exists:
VBA Code:
Public Function CheckBoxExists(ByVal Target As Range) As Boolean
Dim ws As Worksheet
Set ws = Target.Parent
Dim chbx As CheckBox
'Itera pelas Checkboxes existentes na folha
For Each chbx In ws.CheckBoxes
'Checa se existe Checkbox na linha
If Not Intersect(chbx.TopLeftCell, Target) Is Nothing Then
'Existe Checkbox na linha
CheckBoxExists = True
Exit Function
End If
'Passa para a próxima Checkbox
Next chbx
'Não existe Checkbox
CheckBoxExists = False
End Function
This is the code that creates the checkbox:
VBA Code:
Sub Checkboxes_Creation()
Dim lastRow As Long
Dim Sh As Worksheet
Dim worksheet1 As String: worksheet1 = "Salarios" 'Salarios
Dim PagoColumn As String: PagoColumn = "B"
Dim StatusColumn As String: StatusColumn = "C"
Dim LastRowColumn As String: LastRowColumn = "l:l" 'Include Entire Column.
Dim HRnumber As Integer
Set Sh = ActiveSheet
With Sh
'Número da última coluna da tabela
lastRow = WorksheetFunction.CountA(Range(LastRowColumn))
End With
'Checa se não há nenhuma checkbox na linha. O "+7", serve para ignorar as 7 linhas antes da tabela
'(não consegui fazer com que adicionasse a checkbox na linha X da tabela, pelo que tive que fazer X + 7)
If Not CheckBoxExists(Sh.Range(PagoColumn & lastRow + 7)) Then
With Sh.Range(PagoColumn & lastRow)
'Criação da checkbox e suas definições
With Sh.CheckBoxes.Add(Cells(lastRow, PagoColumn).Left, Cells(lastRow + 7, PagoColumn).Top, 10, 10)
.Caption = ""
.Locked = False
.LockedText = False
.Value = xlOff
.LinkedCell = Cells(lastRow + 7, StatusColumn).Address
End With
'Chama o código para centrar a Checkbox na célula
Call CenterCheckbox
End With
End If
End Sub
I'm trying to add a "for each" loop, so the code loops by every row, but I wasn't able to do that yet.
Can anybodu help me with this, as I don't have much free time to have this file ready to be used in my company.
Another thing, I have that "lastRow + 7", because my table starts in row 7. Can this be done without this part? because if someone adds a row before the table, this code will break.
Cheers