Saulo...agradeço pela ajuda.
Mas, ainda não consegui fazer funcionar.
Não está inserindo "EM ROTA" na planilha DADOS.
Segue o código que adaptei baseando na sua sugestão.
Inseri como "ATUALIZASTATUS".
Trouxe abaixo todo código pra ver se não está em conflito com outra coisa.
Private Sub Btn_gerar_rota_Click()
Dim w As Worksheet
Dim num_minuta As Integer
Set w = Sheets("MINUTAS")
oCol = 5
lista = 0
linha = 1
With w
Do Until w.Cells(linha, 1) = ""
linha = linha + 1
Loop
num_minuta = w.Cells(linha - 1, 1).Value + 1
End With
PegaDados:
With w
Do Until w.Cells(linha, 2) = ""
linha = linha + 1
Loop
w.Cells(linha, 1).Value = num_minuta
w.Cells(linha, 2).Value = Format(TextBox_dt_saida.Value, "mm/dd/yyyy")
w.Cells(linha, 3).Value = UCase(ComboBox_motoristas.Value)
w.Cells(linha, 4).Value = UCase(ComboBox_veiculos.Value)
End With
PreencheMinutas:
With w 'seleciona a planilha e insere os dados
nextRow = .Cells(.Rows.Count, "E").End(xlUp).Offset(1, 0).Row
End With
AtualizaStatus:
With Me.ListBox2
'Tratamento de erro
ND:
'Se o erro for 91 (Não localizado)
If Err = 91 Then
'Informamos o usuário do erro ocorrido
MsgBox "O Item de código " & Me.ListBox2 & " não foi localizado!", vbExclamation, "Erro"
End If
'Para cada item contido no ListBox2 até o último
For itemLista = 0 To .ListCount - 1
'Selecionamos o item
Me.ListBox2.Selected(itemLista) = True
'Se o item em questão tiver seu valor diferente de nulo
If Me.ListBox2 <> "" Then
'Selecionamos a planilha
With ThisWorkbook.Sheets("DADOS").Activate 'Mude Plan1 para o nome da planilha onde estão os dados
'Se houver erro, vá para o parágrafo que trata o erro
On Error GoTo ND
'Fazemos a busca na planilha pelo valor do ListBox2
Cells.Find(Me.ListBox2, ActiveCell, xlValues, xlWhole, xlByRows, xlNext).Activate
'Definimos o Status do item na planilha para o informado
Cells(ActiveCell.Row, "O") = "EM ROTA DE ENTREGA" 'Altere A para a coluna de Status
End With
End If
Next
End With
With Me.ListBox2
For lCount = 0 To .ColumnCount
w.Cells(nextRow, oCol) = .List(lista, lCount)
oCol = oCol + 1
Next
If .ListCount - 1 > lista Then
oCol = 5
lista = lista + 1
GoTo PegaDados
GoTo PreencheMinutas
GoTo AtualizaStatus
Else
ListBox1.Clear
ListBox2.Clear
ComboBox_motoristas.Value = Empty
ComboBox_veiculos.Value = Empty
TextBox_buscar.Value = Empty
MsgBox ("Minuta de entrega gerada com sucesso!")
Exit Sub
End If
TextBox_buscar.SetFocus
End With
End Sub
Private Sub UserForm_Initialize()
TextBox_dt_saida.Value = Date
End Sub
Sub Btn_fechar_rotas_Click()
Unload Me
End Sub
Private Sub Btn_inserir_Click()
Dim qtde_notas As Integer
Item = ListBox1.ListIndex
posicao = ListBox2.ListCount
With ListBox2
.AddItem
.List(posicao, 0) = (Me.ListBox1.List(Item, 0))
.List(posicao, 1) = (Me.ListBox1.List(Item, 1))
.List(posicao, 2) = (Me.ListBox1.List(Item, 2))
.List(posicao, 3) = (Me.ListBox1.List(Item, 3))
qtde_notas = qtde_notas + 1
End With
Me.ListBox1.RemoveItem (Me.ListBox1.ListIndex)
TextBox_notas_selecionadas = qtde_notas
End Sub
Private Sub Btn_retirar_Click()
Item = ListBox2.ListIndex
posicao = ListBox1.ListCount
With ListBox1
.AddItem
.List(posicao, 0) = (Me.ListBox2.List(Item, 0))
.List(posicao, 1) = (Me.ListBox2.List(Item, 1))
.List(posicao, 2) = (Me.ListBox2.List(Item, 2))
.List(posicao, 3) = (Me.ListBox2.List(Item, 3))
End With
Me.ListBox2.RemoveItem (Me.ListBox2.ListIndex)
'Dim soma As integer
'For i = 0 To ListBox2.ListCount - 1
'soma = soma + ListBox2.List(i, 2)
'Next i
'TextBox_notas_selecionadas = FormatNumber(soma, 2)
End Sub
Private Sub TextBox_buscar_Change()
Application.ScreenUpdating = False
valor_pesq = TextBox_buscar.Text
Dim w As Worksheet
Dim linha As Integer
Dim coluna As Integer
Dim linhalistbox As Integer
Dim valor_celula As String
Dim conta_registros As Integer
Dim entrega As String
Dim status As String
Set w = Sheets("Dados")
If Opt_cidade = False And Opt_cliente = False And Opt_fornecedor = False Then
MsgBox ("Selecione um critério de pesquisa.")
Exit Sub
ElseIf Opt_cidade = True Then
coluna = 5
ElseIf Opt_cliente = True Then
coluna = 7
ElseIf Opt_fornecedor = True Then
coluna = 6
End If
linhalistbox = 0
conta_registros = 0
linha = 2
ListBox1.Clear
With w
While .Cells(linha, coluna).Value <> Empty
valor_celula = .Cells(linha, coluna).Value
entrega = .Cells(linha, 13).Value
status = .Cells(linha, 15).Value
If entrega = "" And status = "" Then
If UCase(Left(valor_celula, Len(valor_pesq))) = UCase(valor_pesq) Then
With ListBox1
.AddItem
.List(linhalistbox, 0) = w.Cells(linha, 9) 'numero da nota
.List(linhalistbox, 1) = w.Cells(linha, 6) 'fornecedor
.List(linhalistbox, 2) = w.Cells(linha, 7) 'cliente
.List(linhalistbox, 3) = w.Cells(linha, 5) 'cidade
linhalistbox = linhalistbox + 1
End With
conta_registros = conta_registros + 1
End If
End If
linha = linha + 1
Wend
End With
Me.Textbox_total_notas = conta_registros
Application.ScreenUpdating = True
End Sub