1
I have a userform to record a data in the table Tabelaviagens. In the form I select the trip in the combobox and to register the data I use event in the button approve. However the code is not working, always passing to Msgbox. I checked the value that the matrix(i, 1) is receiving, and always refers to the value of the last record in column 1. For some reason cb_viagem.Value passes this value.
How to correct?
Following file. I shorten the database to facilitate the tests. But still can not identify the problem. Download
Private Sub Aprovar_Click()
ActiveSheet.Unprotect Password:="gpq"
If cb_viagem.Value = "" Then
MsgBox "Selecione a Viagem!!"
Else
Dim ws As Worksheet
Dim tbl As ListObject
Dim i As Long
Dim matriz As Variant
Set ws = ThisWorkbook.Sheets("Planejamento") 'ou para declarar com o nome da planilha: ThisWorkbook.Sheets("Agosto")
Set tbl = ws.ListObjects("TabelaViagens") 'No seu caso: .ListObjects("Atividades")
With tbl
matriz = .DataBodyRange 'Cria matriz
For i = UBound(matriz) To LBound(matriz) Step -1
If matriz(i, 1) = cb_viagem.Value And matriz(i, 28) = 0 Then
matriz(i, 28).Value = "1"
matriz(i, 29).Value = "Aprovado"
' .ListRows(i).Delete
Exit For
Else: MsgBox "Viagem já foi aprovada ou negada!"
Exit For
End If
Next i
End With
End If
End Sub
If you can create a [mcve] with a table with values that generate this error. Because the code seems to be correct.
– danieltakeshi
I added the file. The same problem is occurring with the deletion userform.
– Jarbas Martinho