Ir ao conteúdo
  • Cadastre-se

Wendell Menezes

Membro Pleno
  • Posts

    550
  • Cadastrado em

  • Última visita

posts postados por Wendell Menezes

  1. Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim Cell As Range
        Dim r    As Integer
        
        If Target.Address(False, False) = "B3" Then
            Range("A8:B506").ClearContents
                Else
            For Each Cell In Target
                If Cell.Row >= 8 And Cell.Column <= 2 Then
                    With Sheets("Registo de horas")
                        For r = 8 To .Range("AS6")
                            If .Range("AT" & r) = "Pago" Then
                                .Range("Q" & .Range("AS" & r)) = "Pago"
                                .Range("R" & .Range("AS" & r)) = .Range("AU" & r)
                            End If
                        Next
                    End With
                End If
            Next
        End If
    End Sub

     

     

    Cole o código acima no módulo da sheet "Consulta a um Piloto"

     

    As colunas "AV" e "AW" da sheet "Registo de horas" não são necessárias.

     

    image.thumb.png.ecd050457d4fca634e893b3382afa9cb.png

    • Curtir 1
  2. Oi Angelo,

     

    Só para confirmar, você entende que o meu código é uma SUB comum que você deve adicionar à algum botão ou pressionar F5 com curso dentro do código para executar ele e ver o resultado?

     

    Fiquei com essa dúvida porque o seu exemplo abaixo é uma SUB vinculada ao evento de alteração da planilha. Ou seja, o VBA ativa a macro automaticamente quando você altera o valor de qualquer célula.

     

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B3")) Is Nothing Then
            Range("A7:A506").ClearContents
            Range("B7:B506").ClearContents
        End If
    End Sub

     

    Você quer que o LOOP seja feito quando? Quando você ativá-lo manualmente (como eu pensei que era) ou quando você alterar alguma célula / coluna em particular (de forma automática)?

  3. Experimente essa versão:

     

    Private Sub Worksheet_Change(ByVal Target As Range) 'Por Wendell
    
        Dim Cell As Range
        
        For Each Cell In Target
            With Cell
                If .Column = 12 Then 'Coluna "L"
                    Range("AY" & .Row).NumberFormat = "@" 'AY é Coluna VBA
                    Range("AY" & .Row) = Range("AX" & .Row) 'AX é Coluna Fórmulas
                End If
            End With
        Next
        
    End Sub

     

    • Curtir 2
  4. Presumindo que a linha com o SUBTOTAL não exista e tenha que ser criada:

     

    Sub SUBTOTAL()
    
    Dim r   As Long
    Dim h   As Byte
    
    r = 14
    
    While Cells(r, 1) <> ""
        h = 1
            While Cells(r, 4) = Cells(r + h, 4)
                h = h + 1
            Wend
        Cells(r + h, 4).EntireRow.Insert
        Cells(r + h, 2) = "**SUBTOTAL"
        Cells(r + h, 9).Formula = "=SUBTOTAL(9,I" & r & ":I" & r + h - 1 & ")"
        If IsNumeric(Cells(r, 11).End(xlUp)) Then
            Cells(r + h, 11).Formula = "=K" & Cells(r + 1, 11).End(xlUp).Row & "-I" & r + h
                Else
            Cells(r + h, 11).Formula = "=I" & r + 1
        End If
        r = r + h + 1
    Wend
    
    End Sub

     

    • Curtir 1
  5. Boa tarde,

     

    Não sei se entendi bem, A LR verifica qual é a últinha linha da planilha para então testar em cada uma delas se atende a condição necessária para converter para negativo. Se retornasse um número menor (ex: apenas a quantidade de linhas com "Cod2" ela não iria terminar de converter até o final e o código iria parar antes da hora.

  6. @Zamboni_du

     

    Sub NEGATIVE()
    
    Dim RefDate As Long
    Dim LR      As Long
    Dim r       As Long
    
    RefDate = Range("J2")
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    
    For r = 2 To LR
        If Cells(r, "A") = RefDate And Cells(r, "B") = "Cod2" Then
            If Cells(r, "C") > 0 Then Cells(r, "C") = Cells(r, "C") * -1
            If Cells(r, "D") > 0 Then Cells(r, "D") = Cells(r, "D") * -1
            If Cells(r, "E") > 0 Then Cells(r, "E") = Cells(r, "E") * -1
        End If
    Next
    
    End Sub

     

  7. Clique com o botão direito do mouse no nome da aba (ex: Plan1), depois exibir código. Cole o script abaixo e feche a janela.

     

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim Cell As Range
        
        For Each Cell In Target
            With Cell
                If .Column = 183 Then 'Número da coluna GA
                    Range("GB" & .Row).NumberFormat = "@"
                    Range("GB" & .Row) = Range("GA" & .Row)
                End If
            End With
        Next
        
    End Sub

     

    Toda vez que alterar alguma coisa na coluna GA (ex: arrastando / inserindo fórmulas) o valor dela será colado como texto na coluna GB.

     

     

    Agora se quiser que isso funcione APENAS na célula GA5:

     

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Address(False, False) = "GA5" Then
            [GB5].NumberFormat = "@": [GB5] = [GA5]
        End If
    
    End Sub

     

  8. Bom dia,

     

    Ficaria assim (alterar o valor da variável folder para a pasta onde estão os arquivos Excel a serem lidos):

     

    Sub GET_SHEET_NAMES()
    
    Dim FSO     As Object
    Dim Folder  As String
    Dim File    As Object
    Dim wb      As Workbook
    Dim ws      As Worksheet
    Dim LR      As Long
    
    Set FSO = VBA.CreateObject("Scripting.FileSystemObject")
    Folder = "C:\Users\PC\Desktop"
    
    Application.DisplayAlerts = False
    
    For Each File In FSO.GetFolder(Folder).Files
        If InStr(1, LCase(File), ".xls") > 0 And InStr(1, LCase(File), "$") = 0 And File.Name <> ThisWorkbook.Name Then
            Set wb = Workbooks.Open(File, UpdateLinks:=False)
            With ThisWorkbook.ActiveSheet
                LR = .Cells(Rows.Count, 1).End(xlUp).Row + 1
                For Each ws In wb.Worksheets
                    .Cells(LR, 1) = wb.Name
                    .Cells(LR, .Cells(LR, Columns.Count).End(xlToLeft).Column + 1) = ws.Name
                Next
            End With
            wb.Close False
        End If
    Next
    
    End Sub

     

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!