Página 1 de 1

Codigo com erro 1004

Enviado: 22 Set 2020 às 17:15
por kadety
Pessoal, Boa Tarde!

Estou com erro ao executar uma macro para autocompletar uma caixa de box. Consegui o codigo pesquisando na internet e foi ativado, porem quando mudo de um sheet para outra usando um link, ele aparece o erro "o metodo OLEObjects do objeto _worksheet falhou"

Alguem consegue me ajudar com este erro?

Imagem


O código q estou usando:
Código: Selecionar todos
Option Explicit


'Baseado em código da www.contextures.com
Private Sub TempCombo_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
        
    'Ocultar caixa de combinação e mover a próxima célula com Enter e Tab
    Select Case KeyCode
        Case 9
            ActiveCell.Offset(0, 1).Activate
        Case 13
            ActiveCell.Offset(1, 0).Activate
        Case Else
            'Nada
    End Select

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim cboTemp As OLEObject
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    Set cboTemp = ws.OLEObjects("TempCombo")
    
    cboTemp.Visible = False
End Sub

Public Sub lsChamarAutoPreencher()
    Dim lRng As Range
    Set lRng = ActiveCell

    lsComboAutoPreencher lRng
End Sub

Private Sub lsComboAutoPreencher(ByVal Target As Range)
    Dim str As String
    Dim cboTemp As OLEObject
    Dim ws As Worksheet
    Dim wsList As Worksheet

    Set ws = ActiveSheet
    Set wsList = Sheets(Me.Name)
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    If Application.CutCopyMode Then
      'Permite copiar e colar na planilha
      GoTo errHandler
    End If
    
    Set cboTemp = ws.OLEObjects("TempCombo")
      On Error Resume Next
      With cboTemp
        .Top = 10
        .Left = 10
        .Width = 0
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
        .Value = ""
      End With
    
    On Error GoTo errHandler
      If Target.Validation.Type = 3 Then
        Application.EnableEvents = False
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)
        With cboTemp
            .Visible = True
            .Left = Target.Left
            .Top = Target.Top
            .Width = Target.Width + 15
            .Height = Target.Height + 5
            .ListFillRange = str
            .LinkedCell = Target.Address
        End With
        cboTemp.Activate
        
        'Abrir a lista suspensa automaticamente
        Me.TempCombo.DropDown
        End If

errHandler:
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Exit Sub

End Sub


Quando depuro:

Imagem

Alguém consegue ajudar?

Re: Codigo com erro 1004

Enviado: 22 Set 2020 às 17:29
por babdallas
Não use Activesheet. Use.o codename da planilha que tem a combobox.

Codigo com erro 1004

Enviado: 22 Set 2020 às 18:00
por kadety
Grande mestre, muito obrigado resolveu.