Hello,
I have a problem I have not been able to solve:
I am working on a project to manage information about employees' training. In this part of the project I have a macro that is intended to create new posts and add the names of mandatory trainings (for said post) to a matrix.
On page called App I capture Post Name and Type (office, plant, etc.). Below that, I have a list of mandatory trainings (column F). This list comes from a table on sheet called "Obliga".
On colum I of "App" I included some checkboxes for the user to check the trainings that will be mandatory for that new post.
The macro is supposed to:
1. Add the new post to a table on sheet "Training". (This part is working well).
2. Add a column to a table called "Obliga" on "Obliga" sheet. (this part is also working well).
3. Read the names of trainings on column F of "App", compare with the names of trainings on colum C of "Obliga" and write an "x" in the matching rows, last column.
The problem I have is that tThe "x" never gets written. I already check the cell formating, so the problem is not related to font color. How can I get this done?
This is the code I have so far:
I have a problem I have not been able to solve:
I am working on a project to manage information about employees' training. In this part of the project I have a macro that is intended to create new posts and add the names of mandatory trainings (for said post) to a matrix.
On page called App I capture Post Name and Type (office, plant, etc.). Below that, I have a list of mandatory trainings (column F). This list comes from a table on sheet called "Obliga".
On colum I of "App" I included some checkboxes for the user to check the trainings that will be mandatory for that new post.
The macro is supposed to:
1. Add the new post to a table on sheet "Training". (This part is working well).
2. Add a column to a table called "Obliga" on "Obliga" sheet. (this part is also working well).
3. Read the names of trainings on column F of "App", compare with the names of trainings on colum C of "Obliga" and write an "x" in the matching rows, last column.
The problem I have is that tThe "x" never gets written. I already check the cell formating, so the problem is not related to font color. How can I get this done?
This is the code I have so far:
VBA Code:
Sub NewPost()
Dim appSheet As Worksheet
Dim formationsSheet As Worksheet
Dim obligSheet As Worksheet
Dim postesTable As ListObject
Dim obligTable As ListObject
Dim newRow As ListRow
Dim jobName As String
Dim postType As String
Dim i As Integer
Dim lastColIndex As Integer
Dim lastCol As ListColumn
Dim trainingName As String
Dim obligRow As ListRow
Dim startRowObliga As Long, endRowObliga As Long
' Define the tables
Set postesTable = Formations.ListObjects("Postes")
Set obligTable = Obligatoire.ListObjects("Obliga")
' Get the job name and post type from the App sheet
jobName = App.Range("H35").Value
postType = App.Range("H37").Value
' Add the job name and post type to the Postes table
Set newRow = postesTable.ListRows.Add
newRow.Range(1, 1).Value = jobName
newRow.Range(1, 2).Value = postType
' Add the job name to the last column of the Obliga table
obligTable.ListColumns.Add
lastColIndex = obligTable.ListColumns.Count
obligTable.HeaderRowRange.Cells(1, lastColIndex).Value = jobName
' Define the start and end rows for comparison in the "Obliga" table
startRowObliga = 4 ' assuming the training names start from row 4
endRowObliga = Obligatoire.Cells(Obligatoire.Rows.Count, "C").End(xlUp).Row
' Locate the checkbox in the current row
For Each chk In App.CheckBoxes
' Check if the checkbox is within the range of rows 42 to 66
If chk.TopLeftCell.Row >= 42 And chk.TopLeftCell.Row <= 66 Then
If chk.Value = 1 Then
' Get the training name from the same row
trainingName = App.Cells(chk.TopLeftCell.Row, "F").Value
' Compare with each training name in the "Obliga" table
For j = startRowObliga To endRowObliga
obligaTrainingName = Obligatoire.Cells(j, "C").Text
Debug.Print "Comparing App Training '" & trainingName & "' with Obliga Training '" & obligaTrainingName & "'"
If trainingName = obligaTrainingName Then
Obligatoire.Cells(j, lastColIndex).Value = "x"
Debug.Print "Marked 'x' for training: " & obligaTrainingName
End If
Next j
End If
End If
Next chk
MsgBox "Information sauvegardée.", vbInformation
End Sub