Vídeo recomendado
https://youtu.be/diWPPPhW-9E

CARREGAR SUB-INTERVALO DE TABELA APÓS AUTOFILTER EM LISTVIEW

Fórum para dúvidas sobre os fundamentos da linguagem de programação Visual Basic no contexto do VBA
RAFAEL AMORIM
Colaborador
Colaborador
Mensagens: 69
Registrado em: Sáb Jul 22, 2017 10:17 am

CARREGAR SUB-INTERVALO DE TABELA APÓS AUTOFILTER EM LISTVIEW

Mensagem por RAFAEL AMORIM »

Fala galera!


O código abaixo é executado em formulário que criei; bem o objetivo dele é realizar filtros na tabela a partir do preenchimento de seus campos e então pegar o sub-intervalo, após aplicação do filtro na tabela e na sequência carregar no listview.

Antes de realizar qualquer filtro a variável htx = ShtNFe.Range("A1").CurrentRegion.Rows.Count tem valor 9 que é a altura da minha tabela sem filtros aplicados, após aplicar filtros esse valor continua o mesmo, independente de rodar a variável antes ou depois do filtro. Tô quebrando a cabeça aqui… ainda não consegui obter resultado diferente, se alguém puder ajudar🤙🏽

IMAGEM_DO_FORM

"Sei que consigo fazer de outras formas, mas no poste atual queria mais um reflexão do porquê não funciona como esperado, pois CurrentRegion deveria me fornecer os o intervalo atual e as propriedades .Rows.Count a altura do intervalo atual (Entender isso é uma condição pra mim, pois não vejo impedimento para que funcione.)"

Código: Selecionar todos

Sub Filtro_Criterio()

'REPRESENTA A ALTURA 'x' DO INTERVALO ATUAL
Dim htx             As Long

'VARIAVEIS QUE RECEBEM OS VALORES DOS 'txtbox'
Dim txtCNPJ, txtFornecedor, txtNota, txtChave, txtCodFisc, txtProduto, cbDest, cbAnex  As String
Dim txtcod, txtncm As String

'ARRAY QUE DEVE SER CARREGADO COM DADOS DA REGIAO ATUAL APOS EXECUCAO DO FILTRO
Dim arrCurrent      As Variant
'LOOP
Dim i               As Long
'MATRIZ UTILIZADA P/ RECEBER DADOS DE 'arrCurrent' P/ CARREGAR NO LISTBOX
Dim arrMatrix()     As Variant

On Error Resume Next

With Frm_Ajuste
    txtCNPJ = "*" & .txt_cnpj.Text & "*"
    txtFornecedor = "*" & .txt_fornecedor.Text & "*"
    txtNota = "*" & .txt_nota.Text & "*"
    txtChave = "*" & .txt_chave.Text & "*"
    txtCodFisc = "*" & .txt_cfop.Text & "*"
    txtProduto = "*" & .txt_produto.Text & "*"
    cbDest = "*" & .cb_dest_pesq.Text & "*"
    cbAnex = "*" & .cb_anexo_pesq.Text & "*"
    txtcod = "*" & .txt_cod.Text & "*"
    txtncm = "*" & .txt_ncm.Text & "*"
End With


ShtNFe.Activate
htx = ShtNFe.Range("A1").CurrentRegion.Rows.Count

With ShtNFe.Range("A1")
    .AutoFilter Field:=7, Criteria1:=txtCNPJ, visibleDropDown:=False
    .AutoFilter Field:=8, Criteria1:=txtFornecedor, visibleDropDown:=False
    .AutoFilter Field:=5, Criteria1:=txtNota, visibleDropDown:=False
    .AutoFilter Field:=1, Criteria1:=txtChave, visibleDropDown:=False
    .AutoFilter Field:=18, Criteria1:=txtCodFisc, visibleDropDown:=False
    .AutoFilter Field:=15, Criteria1:=txtProduto, visibleDropDown:=False
    .AutoFilter Field:=52, Criteria1:=cbDest, visibleDropDown:=False
    .AutoFilter Field:=51, Criteria1:=cbAnex, visibleDropDown:=False
    .AutoFilter Field:=14, Criteria1:=txtcod, visibleDropDown:=False
    .AutoFilter Field:=16, Criteria1:=txtncm, visibleDropDown:=False
End With
    
    
    arrCurrent = ShtNFe.Range("A1").CurrentRegion.Offset(1).Resize(htx - 1)
    
    ReDim arrMatrix(1 To htx, 1 To 9) As Variant
    
    For i = 1 To UBound(arrCurrent)
        arrMatrix(htx, 1) = arrCurrent(i, 5)
        arrMatrix(htx, 2) = arrCurrent(i, 14)
        arrMatrix(htx, 3) = arrCurrent(i, 15)
        arrMatrix(htx, 4) = arrCurrent(i, 16)
        arrMatrix(htx, 5) = arrCurrent(i, 47)
        arrMatrix(htx, 6) = arrCurrent(i, 48)
        arrMatrix(htx, 7) = arrCurrent(i, 49)
        arrMatrix(htx, 8) = arrCurrent(i, 50)
        arrMatrix(htx, 9) = arrCurrent(i, 52)
        htx = htx - 1
    Next i

Frm_Ajuste.ListBox2.ColumnWidths = "53;77;255;55;38;38;38;40;10"
Frm_Ajuste.ListBox2.List = arrMatrix

End Sub


Disable adblock

This site is supported by ads and donations.
If you see this text you are blocking our ads.
Please consider a Donation to support the site.


Responder