Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
Avatar do usuário
Por AfonsoMira
Posts Avatar
#53896
Boas.

Existe algum código onde por exemplo insiro o número 2,6 numa célula e ao correr a macro ele arredonda o numero para 3, mas caso esteja 2,4 ele teria que arredondar para 2,5 e caso escreve 2,5 não arredonda.

Existe alguma for de fazer isto sem recorrer a macro?

Obrigado.
Por babdallas
#53897
Se quiser na mesma célula que digitou, só com macro. Em outra célula daria para fazer com fórmula. O que prefere?
Avatar do usuário
Por AfonsoMira
Posts Avatar
#53901
Na mesma célula.

A minha ideia era criar uma macro com um Select Case.

Não sei se é boa ideia ou se existe algo mais fácil.
Por babdallas
#53906
Veja se atende.
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lng2Digitos      As Long
    
    If Not Application.Intersect(Target, wshTeste.Range("C3:F8")) Is Nothing And _
        VBA.IsNumeric(Target.Value2) Then
        lng2Digitos = (Target.Value2 - VBA.Int(Target.Value2)) * 100
        
        Application.EnableEvents = False
        If lng2Digitos > 50 Then
            Target.Value2 = VBA.Round(Target.Value2)
        Else
            Target.Value2 = VBA.Int(Target.Value2) + 0.5
        End If
        Application.EnableEvents = True
    End If
End Sub
Você não está autorizado a ver ou baixar esse anexo.
Por babdallas
#53907
Acho que assim fica melhor.
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, wshTeste.Range("C3:F8")) Is Nothing And _
        VBA.IsNumeric(Target.Value2) Then
        
        Application.EnableEvents = False
        
        If VBA.Round(Target.Value2) = VBA.Int(Target.Value2) + 1 Then
            Target.Value2 = VBA.Round(Target.Value2)
        Else
            Target.Value2 = VBA.Int(Target.Value2) + 0.5
        End If
        Application.EnableEvents = True
    End If
End Sub
Você não está autorizado a ver ou baixar esse anexo.
Avatar do usuário
Por AfonsoMira
Posts Avatar
#53914
Sim é isso mas quando eu insiro o número 2 ele arredonda para 2,5 e eu queria que se o número já fosse inteiro não arredondava.

Existe maneira de fazer isso? Penso que com um IF dê.
Por babdallas
#53915
Veja agora
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, wshTeste.Range("C3:F8")) Is Nothing And _
        VBA.IsNumeric(Target.Value2) Then
        
        Application.EnableEvents = False
        
        If Target.Value2 <> VBA.Int(Target.Value2) Then
            If VBA.Round(Target.Value2) = VBA.Int(Target.Value2) + 1 Then
                Target.Value2 = VBA.Round(Target.Value2)
            Else
                Target.Value2 = VBA.Int(Target.Value2) + 0.5
            End If
            Application.EnableEvents = True
        End If
    End If
End Sub
Você não está autorizado a ver ou baixar esse anexo.
Avatar do usuário
Por AfonsoMira
Posts Avatar
#53916
Agora não está a executar a macro apenas deixa estar os números que estou a inserir.
Por babdallas
#53918
Coloquei um If a mais na resposta anterior e esqueci de alterar a posição do Application.EnableEvents = True.

Veja agora:
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, wshTeste.Range("C3:F8")) Is Nothing And _
        VBA.IsNumeric(Target.Value2) Then
        
        Application.EnableEvents = False
        
        If Target.Value2 <> VBA.Int(Target.Value2) Then
            If VBA.Round(Target.Value2) = VBA.Int(Target.Value2) + 1 Then
                Target.Value2 = VBA.Round(Target.Value2)
            Else
                Target.Value2 = VBA.Int(Target.Value2) + 0.5
            End If
        End If
        Application.EnableEvents = True
    End If
End Sub
Você não está autorizado a ver ou baixar esse anexo.
Avatar do usuário
Por AfonsoMira
Posts Avatar
#53934
É mesmo isto!
Obrigado pela ajuda! :D
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord