alessandra.macedo
10-11-2014 20:33:59
Boa tarde
Adaptei o código de Filtra_ListBox_VBA_Custom para buscar dados de outra planilha.
Está dando erro de a variavel do bloco with não foi definida.
Será que teriam como ver onde está meu erro?
obrigada
Segue código do form:
Option Explicit
'Autor: Tomás Vásquez
' www.tomasvasquez.com.br
' www.tomasvasquez.com.br/blog
' www.tomasvasquez.com.br/forum
' www.tomasvasquez.com.br/cursocsharp
Private Const NomePlanilha As String = "Fornecedores"
Private Const LinhaCabecalho As Integer = 1
Private wsCadastro As Worksheet
Private wbCadastro As Workbook
Private Sub ComboBoxCampos_Change()
End Sub
Private Sub TextBoxFiltro_Change()
If Me.ComboBoxCampos.ListIndex <> -1 Then
Call PreencheLista(TextBoxFiltro.Text)
End If
End Sub
Private Sub UserForm_Initialize()
Call PreencheCampos
End Sub
Private Sub PreencheCampos()
Dim coluna As Integer
Dim linha As Integer
Dim wsCadastro As Worksheet
coluna = 1
linha = LinhaCabecalho
With wsCadastro
While .Cells(linha, coluna).Value <> Empty
Me.ComboBoxCampos.AddItem .Cells(linha, coluna)
coluna = coluna + 1
Wend
End With
End Sub
Private Sub PreencheCabecalho(ByRef Lista())
Dim coluna As Integer
Dim linha As Integer
Dim wsCadastro As Worksheet
coluna = 1
linha = LinhaCabecalho
With wsCadastro
While .Cells(linha, coluna).Value <> Empty
Lista(coluna - 1, 0) = .Cells(linha, coluna)
coluna = coluna + 1
Wend
End With
End Sub
Private Sub PreencheLista(ByVal TextoDigitado As String)
Dim wsCadastro As Worksheet
Dim i As Integer
Dim x As Integer
Dim indiceLista As Integer
Dim coluna As Integer
Dim TextoCelula As String
Dim Lista()
ReDim Lista(wsCadastro.UsedRange.Columns.Count, 0)
i = LinhaCabecalho + 1
indiceLista = 1
coluna = Me.ComboBoxCampos.ListIndex + 1
Call PreencheCabecalho(Lista)
ListBoxLista.Clear
With wsCadastro
While .Cells(i, coluna).Value <> Empty
TextoCelula = .Cells(i, coluna).Value
If UCase(Left(TextoCelula, Len(TextoDigitado))) = UCase(TextoDigitado) Then
For x = 0 To ws.UsedRange.Columns.Count - 1
ReDim Preserve Lista(ws.UsedRange.Columns.Count, indiceLista)
Lista(x, indiceLista) = .Cells(i, x + 1)
Next
indiceLista = indiceLista + 1
End If
i = i + 1
Wend
End With
Lista = Array2DTranspose(Lista)
Me.ListBoxLista.List = Lista
End Sub
Function Array2DTranspose(avValues As Variant) As Variant
Dim lThisCol As Long, lThisRow As Long
Dim lUb2 As Long, lLb2 As Long
Dim lUb1 As Long, lLb1 As Long
Dim avTransposed As Variant
If IsArray(avValues) Then
On Error GoTo ErrFailed
lUb2 = UBound(avValues, 2)
lLb2 = LBound(avValues, 2)
lUb1 = UBound(avValues, 1)
lLb1 = LBound(avValues, 1)
ReDim avTransposed(lLb2 To lUb2, lLb1 To lUb1)
For lThisCol = lLb1 To lUb1
For lThisRow = lLb2 To lUb2
avTransposed(lThisRow, lThisCol) = avValues(lThisCol, lThisRow)
Next
Next
End If
Array2DTranspose = avTransposed
Exit Function
ErrFailed:
Debug.Print Err.Description
Debug.Assert False
Array2DTranspose = Empty
Exit Function
Resume
End Function
Private Sub DefinePlanilhaDados()
Dim abrirArquivo As Boolean
Dim wb As Workbook
Dim caminhoCompleto As String
Dim ARQUIVO_DADOS As String
Dim PASTA_DADOS As String
abrirArquivo = True
ARQUIVO_DADOS = Range("ARQUIVO_DADOS").Value
PASTA_DADOS = Range("PASTA_DADOS").Value
If ThisWorkbook.Name <> ARQUIVO_DADOS Then
'monta a string do caminho completo
If PASTA_DADOS = vbNullString Or PASTA_DADOS = "" Then
caminhoCompleto = Replace(ThisWorkbook.FullName, ThisWorkbook.Name, vbNullString) & ARQUIVO_DADOS
Else
If Right(PASTA_DADOS, 1) = "\" Then
caminhoCompleto = PASTA_DADOS & ARQUIVO_DADOS
Else
caminhoCompleto = PASTA_DADOS & "\" & ARQUIVO_DADOS
End If
End If
'verifica se o arquivo não está aberto
For Each wb In Application.Workbooks
If wb.Name = ARQUIVO_DADOS Then
abrirArquivo = False
Exit For
End If
Next
'atribui o arquivo
If abrirArquivo Then
Set wbCadastro = Workbooks.Open(Filename:=caminhoCompleto, ReadOnly:=True)
Else
Set wbCadastro = Workbooks(ARQUIVO_DADOS)
End If
Else
Set wbCadastro = ThisWorkbook
End If
Set wsCadastro = wbCadastro.Worksheets("Registro")
'oculta o arquivo de dados
wbCadastro.Windows(1).Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'fecha a planilha de dados, se estiver aberta
If Not wbCadastro Is Nothing Then
wbCadastro.Saved = True
wbCadastro.Close SaveChanges:=False
End If
Set wbCadastro = Nothing
End Sub
Adaptei o código de Filtra_ListBox_VBA_Custom para buscar dados de outra planilha.
Está dando erro de a variavel do bloco with não foi definida.
Será que teriam como ver onde está meu erro?
obrigada
Segue código do form:
Option Explicit
'Autor: Tomás Vásquez
' www.tomasvasquez.com.br
' www.tomasvasquez.com.br/blog
' www.tomasvasquez.com.br/forum
' www.tomasvasquez.com.br/cursocsharp
Private Const NomePlanilha As String = "Fornecedores"
Private Const LinhaCabecalho As Integer = 1
Private wsCadastro As Worksheet
Private wbCadastro As Workbook
Private Sub ComboBoxCampos_Change()
End Sub
Private Sub TextBoxFiltro_Change()
If Me.ComboBoxCampos.ListIndex <> -1 Then
Call PreencheLista(TextBoxFiltro.Text)
End If
End Sub
Private Sub UserForm_Initialize()
Call PreencheCampos
End Sub
Private Sub PreencheCampos()
Dim coluna As Integer
Dim linha As Integer
Dim wsCadastro As Worksheet
coluna = 1
linha = LinhaCabecalho
With wsCadastro
While .Cells(linha, coluna).Value <> Empty
Me.ComboBoxCampos.AddItem .Cells(linha, coluna)
coluna = coluna + 1
Wend
End With
End Sub
Private Sub PreencheCabecalho(ByRef Lista())
Dim coluna As Integer
Dim linha As Integer
Dim wsCadastro As Worksheet
coluna = 1
linha = LinhaCabecalho
With wsCadastro
While .Cells(linha, coluna).Value <> Empty
Lista(coluna - 1, 0) = .Cells(linha, coluna)
coluna = coluna + 1
Wend
End With
End Sub
Private Sub PreencheLista(ByVal TextoDigitado As String)
Dim wsCadastro As Worksheet
Dim i As Integer
Dim x As Integer
Dim indiceLista As Integer
Dim coluna As Integer
Dim TextoCelula As String
Dim Lista()
ReDim Lista(wsCadastro.UsedRange.Columns.Count, 0)
i = LinhaCabecalho + 1
indiceLista = 1
coluna = Me.ComboBoxCampos.ListIndex + 1
Call PreencheCabecalho(Lista)
ListBoxLista.Clear
With wsCadastro
While .Cells(i, coluna).Value <> Empty
TextoCelula = .Cells(i, coluna).Value
If UCase(Left(TextoCelula, Len(TextoDigitado))) = UCase(TextoDigitado) Then
For x = 0 To ws.UsedRange.Columns.Count - 1
ReDim Preserve Lista(ws.UsedRange.Columns.Count, indiceLista)
Lista(x, indiceLista) = .Cells(i, x + 1)
Next
indiceLista = indiceLista + 1
End If
i = i + 1
Wend
End With
Lista = Array2DTranspose(Lista)
Me.ListBoxLista.List = Lista
End Sub
Function Array2DTranspose(avValues As Variant) As Variant
Dim lThisCol As Long, lThisRow As Long
Dim lUb2 As Long, lLb2 As Long
Dim lUb1 As Long, lLb1 As Long
Dim avTransposed As Variant
If IsArray(avValues) Then
On Error GoTo ErrFailed
lUb2 = UBound(avValues, 2)
lLb2 = LBound(avValues, 2)
lUb1 = UBound(avValues, 1)
lLb1 = LBound(avValues, 1)
ReDim avTransposed(lLb2 To lUb2, lLb1 To lUb1)
For lThisCol = lLb1 To lUb1
For lThisRow = lLb2 To lUb2
avTransposed(lThisRow, lThisCol) = avValues(lThisCol, lThisRow)
Next
Next
End If
Array2DTranspose = avTransposed
Exit Function
ErrFailed:
Debug.Print Err.Description
Debug.Assert False
Array2DTranspose = Empty
Exit Function
Resume
End Function
Private Sub DefinePlanilhaDados()
Dim abrirArquivo As Boolean
Dim wb As Workbook
Dim caminhoCompleto As String
Dim ARQUIVO_DADOS As String
Dim PASTA_DADOS As String
abrirArquivo = True
ARQUIVO_DADOS = Range("ARQUIVO_DADOS").Value
PASTA_DADOS = Range("PASTA_DADOS").Value
If ThisWorkbook.Name <> ARQUIVO_DADOS Then
'monta a string do caminho completo
If PASTA_DADOS = vbNullString Or PASTA_DADOS = "" Then
caminhoCompleto = Replace(ThisWorkbook.FullName, ThisWorkbook.Name, vbNullString) & ARQUIVO_DADOS
Else
If Right(PASTA_DADOS, 1) = "\" Then
caminhoCompleto = PASTA_DADOS & ARQUIVO_DADOS
Else
caminhoCompleto = PASTA_DADOS & "\" & ARQUIVO_DADOS
End If
End If
'verifica se o arquivo não está aberto
For Each wb In Application.Workbooks
If wb.Name = ARQUIVO_DADOS Then
abrirArquivo = False
Exit For
End If
Next
'atribui o arquivo
If abrirArquivo Then
Set wbCadastro = Workbooks.Open(Filename:=caminhoCompleto, ReadOnly:=True)
Else
Set wbCadastro = Workbooks(ARQUIVO_DADOS)
End If
Else
Set wbCadastro = ThisWorkbook
End If
Set wsCadastro = wbCadastro.Worksheets("Registro")
'oculta o arquivo de dados
wbCadastro.Windows(1).Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'fecha a planilha de dados, se estiver aberta
If Not wbCadastro Is Nothing Then
wbCadastro.Saved = True
wbCadastro.Close SaveChanges:=False
End If
Set wbCadastro = Nothing
End Sub