Página 1 de 1

Tipos incompatíveis

Enviado: 23 Mai 2018 às 14:08
por AMORIM123
Salve, salve galera do fórum.

No "cód. do "frm_rbpa" abaixo tenho um evento que pretendo chamar dentro do evento UserForm_Initialize(), porém ao executar para ver se rodar legal, da "tipo incompatíveis"; se depuro o cód. ele grifa a linha : "datPeriodo = .txt_periodo.Text"

Código: Selecionar todos
Private Sub txt_periodo_AfterUpdate()

Dim lngPriLin, lngUltLin, lngLoopLin       As Long
Dim datPeriodo                             As Date
Dim vlrRPA, vlrFSPA                        As Currency
Dim strbusca                               As String

lngPriLin = 2

With Me
'linha grifada
datPeriodo = .txt_periodo.Text
vlrRPA = .txt_rpa.Text
vlrFSPA = .txt_fspa.Text
End With

With wshComum
    lngUltLin = .Cells(.Rows.Count, 2).End(xlUp).Row
End With

With wshComum
    For lngLoopLin = lngPriLin To lngUltLin Step 1
        strbusca = .Cells(lngLoopLin, 2)

        If strbusca = datPeriodo Then
            .Cells(lngLoopLin, 3) = CCur(vlrRPA)
            .Cells(lngLoopLin, 4) = CCur(vlrFSPA)
        End If
   Next lngLoopLin
End With

End Sub
Código: Selecionar todos
Private Sub UserForm_Initialize()

Call txt_periodo_AfterUpdate
     
End Sub
A quem puder ajudar agradeço!

Re: Tipos incompatíveis

Enviado: 23 Mai 2018 às 15:03
por babdallas
Já que sua variável é do tipo Date, então tente o seguinte:
Código: Selecionar todos
datPeriodo = VBA.Cdate(.txt_periodo.Value)

Tipos incompatíveis

Enviado: 23 Mai 2018 às 15:13
por AMORIM123
babdallas,

não especifiquei meu objetivo com código assim: meu objetivo com evento "afterupdate ": é fazer com que quando digitada uma data que esteja na planilha (Comum) e ao mudar o foco do "txtbox_periodo" os demais sejam preenchidas com os demais dados da mesma linha do registro

fiz algumas alterações e o erro de tipos incompatíveis não está dando mais, porém o objetivo não está sendo executado, veja como está:
Código: Selecionar todos
Private Sub txt_periodo_AfterUpdate()

Dim lngPriLin, lngUltLin, lngLoopLin       As Long
Dim datPeriodo                             As Date
Dim vlrRPA, vlrFSPA                        As Currency
Dim strbusca                               As String


If txt_Peridodo = "" Or IsEmpty(txt_periodo) Then Exit Sub
If txt_rpa = "" Or IsEmpty(txt_rpa) Then Exit Sub
If txt_fspa = "" Or IsEmpty(txt_fspa) Then Exit Sub

lngPriLin = 2


datPeriodo = CDate(txt_periodo.Text)
vlrRPA = txt_rpa.Text
vlrFSPA = txt_fspa.Text


With wshComum

    lngUltLin = .Cells(.Rows.Count, 2).End(xlUp).Row

    For lngLoopLin = lngPriLin To lngUltLin Step 1
        strbusca = .Cells(lngLoopLin, 2)

        If strbusca = datPeriodo Then
            vlrRPA = .Cells(lngLoopLin, 3)
            vlrFSPA = .Cells(lngLoopLin, 4)
        End If
   Next lngLoopLin
End With

End Sub

Tipos incompatíveis

Enviado: 24 Mai 2018 às 10:04
por AMORIM123
O babdallas,

Vlw amigão pela ateção... lá planilhando srobles ajudou-me:

Solução ficou assim:
Código: Selecionar todos
Private Sub txt_periodo_AfterUpdate()

Dim lngPriLin, lngUltLin, lngLoopLin       As Long
Dim datPeriodo                             As Date
Dim strbusca                               As String
    
    lngPriLin = 2
    
    With Me
        On Error GoTo trataErro
        datPeriodo = .txt_periodo.Text
    End With
    
    With wshComum
        lngUltLin = .Cells(.Rows.Count, 2).End(xlUp).Row
    End With
    
    With wshComum
        For lngLoopLin = lngPriLin To lngUltLin Step 1
            strbusca = .Cells(lngLoopLin, 2)
    
            If strbusca = datPeriodo Then
                Me.txt_rpa = CCur(.Cells(lngLoopLin, 3))
                Me.txt_fspa = CCur(.Cells(lngLoopLin, 4))
                Exit For
            End If
        Next lngLoopLin
    End With
trataErro:
If Err.Number = 13 Then
    Me.txt_periodo = ""
    Me.txt_rpa = ""
    Me.txt_fspa = ""
End If

End Sub