Olá, alguém poderia ajudar-me a criar uma pesquisa no VBA, utilizando 2 ranges para busca, exemplo: pesquisar CNPJ e NF, e retornar o resultado, em um formulário, com base nesses dois critérios.
Tenho os seguintes códigos abaixo para realizar a pesquisa, mas não consegui nenhum resultado ou erro nos Labels, TextBoxs, etc.
O código no qual fiz as devidas adaptações segue abaixo:
Private Sub btPesquisarDados_Click() Dim CNPJ_CPF As String Dim NF As String Dim Linhas As Long Dim i As Long Dim Resultado As String Linhas = [A1].CurrentRegion.Rows.Count CNPJ_CPF = frmCadastro_Dados.txtCNPJ_CPF.Value NF = frmCadastro_Dados.txtDoc_Num.Value For i = 1 To Linhas If Cells(i, "A").Value = CNPJ And Cells(i, "B").Value = NF Then Resultado = Cells(i, "C").Value 'Recupera os dados da planilha no formulário lblCod.Caption = Sheets("Cadastro_Dados").Cells(i, 1).Value txtCNPJ_CPF.Value = Sheets("Cadastro_Dados").Cells(i, 2).Value cbxClientes.Value = Sheets("Cadastro_Dados").Cells(i, 3).Value txtNome.Value = Sheets("Cadastro_Dados").Cells(i, 4).Value txtDoc_Num.Value = Sheets("Cadastro_Dados").Cells(i, 7).Value txtDt_Emissao.Value = Sheets("Cadastro_Dados").Cells(i, 8).Value txtValorBruto.Value = Sheets("Cadastro_Dados").Cells(i, 24).Value txtDt_Vencto.Value = Sheets("Cadastro_Dados").Cells(i, 12).Value cbxSimples.Value = Sheets("Cadastro_Dados").Cells(i, 17).Value cbxLocacao.Value = Sheets("Cadastro_Dados").Cells(i, 18).Value cbxServicos.Value = Sheets("Cadastro_Dados").Cells(i, 19).Value txtMat_Aplic.Value = Sheets("Cadastro_Dados").Cells(i, 20).Value txtCod_ISS.Value = Sheets("Cadastro_Dados").Cells(i, 54).Value txtAli_ISS.Value = Sheets("Cadastro_Dados").Cells(i, 53).Value txtIRRF.Value = Sheets("Cadastro_Dados").Cells(i, 52).Value lblISS.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 28).Value, "#,##0.00") lblINSS.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 32).Value, "#,##0.00") lblCodINSS.Caption = Sheets("Cadastro_Dados").Cells(i, 56).Value lblPIS.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 36).Value, "#,##0.00") lblCodPIS.Caption = Sheets("Cadastro_Dados").Cells(i, 59).Value lblCOFINS.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 40).Value, "#,##0.00") lblCodCOFINS.Caption = Sheets("Cadastro_Dados").Cells(i, 63).Value lblCSSLL.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 44).Value, "#,##0.00") lblCodCSSLL.Caption = Sheets("Cadastro_Dados").Cells(i, 67).Value lblIRRF.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 48).Value, "#,##0.00") lblCodIRRF.Caption = Sheets("Cadastro_Dados").Cells(i, 73).Value lblVL.Caption = Format(Sheets("Cadastro_Dados").Cells(i, 79).Value, "#,##0.00") Exit For End If Next End Sub
Desde já agradeço.
Atenciosamente,
Reginaldo
Reginaldo HB
Site: Reginaldo HB
É possível fazer sem VBA.
Se tiver interesse poste a planilha.
Abs,
Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel
Bom dia!!!
Poderia postar seu modelo COMPACTADO???
Att
Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel
Reginaldo, analizando somente pela rotina que postou, eu deduzo que o erro é devido as Variaveis, por exemplo :
Você Declarou a Variável com o seguinte nome :
Dim CNPJ_CPF As String
E nesta Linha carregamos o valor do TextBox na Variável : CNPJ_CPF = frmCadastro_Dados.txtCNPJ_CPF.Value
E nas condições você colocou :
If Cells(i, "A").Value = CNPJ And Cells(i, "B").Value = NF Then
Então acredito que o erro esteja nesta Variavel que não existe, por isto que eu costumo sempre utilizar a Instrução Option Explicit, esta instrução nos força a Declarar as Variáveis, facilitando observarmos quando a mesma não existe ou está com erro de ortografia.
Altere a Variável, faça o teste e se ainda não resolver, o ideal é postar um exemplo reduzido e compactado para podermos analizar o conjunto todo.
[]s
Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel