Jump to content

Preenchimento de células via função


heldercorreia
 Share

Recommended Posts

Viva malta, venho pedir a vossa opinião/sugestões quanto ao seguinte:

Necessito de preencher células no excel, de tal forma que não seja em posições fixas.

Assim sendo, engendrei a seguinte função, a qual recebe em argumento a posição inicial da sequencia de células a serem preenchidas.

Private Function dados_motor(p, n) 'Escreve na folha de cálculo os dados relativos ao
    Sheet2.Range(p).Select         'motor a contar da posição recebida em argumento
    Sheet2.Range(p).Value = "Motor " & n
    ActiveCell.Offset(1, 0).Select
    Call supersub("UrM (V)", 0, 2, 2, 0)
    ActiveCell.Offset(1, 0).Select
    Call supersub("SrM (VA)", 0, 2, 2, 0)
    ActiveCell.Offset(1, 0).Select
    Call supersub(ChrW(981) & "rM (º)", 0, 2, 2, 2)
    ActiveCell.Offset(1, 0).Select
    Call supersub("ILR/IrM", 0, 2, 2, 0)
    ActiveCell.Offset(1, 0).Select
    Call supersub("RM/XM", 0, 2, 0, 0)
    Sheet2.Range(p).Select
    ActiveCell.Offset(1, 1).Select
    ActiveCell.Value = "400"
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = "20000"
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = "0.15"
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = "8"
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = "0.3"
End Function

Pronto.. como podem ver não me parece lá grande solução, vá funciona mas acho que é um bocado mau do ponto de vista de programação.

Alguma alternativa/sugestões adicionais?

Ah, falta indicar o que é a função Supersub que tantas vezes chamo:

Private Sub supersub(b, c, d, e, f) 'Coloca certos caracteres em expoente ou índice
    ActiveCell.Value = b
    With ActiveCell.Characters(Start:=1, Length:=1).Font
    .Superscript = issuper(c)
    .Subscript = issub(c)
    End With
    With ActiveCell.Characters(Start:=2, Length:=1).Font
    .Superscript = issuper(d)
    .Subscript = issub(d)
    End With
    With ActiveCell.Characters(Start:=3, Length:=1).Font
    .Superscript = issuper(e)
    .Subscript = issub(e)
    End With
    With ActiveCell.Characters(Start:=4, Length:=1).Font
    .Superscript = issuper(f)
    .Subscript = issub(f)
    End With
End Sub

E ainda: 

Public Function issub(f) As Boolean 'Retorna 1 se caracter for índice
    If f = 2 Then
        issub = True
    Else
        issub = False
    End If
End Function

Private Function issuper(f) As Boolean 'Retorna 1 se caracter for expoente
    If f = 1 Then
        issuper = True
    Else
        issuper = False
    End If
End Function

Isto foi também uma maneira que arranjei de poder controlar caracter a caracter se é um expoente ou um índice, superior à linha, ou inferior.

Peço que perdoem a extenção do texto, aceito também sugestões quanto a como apresentar melhor o meu problema para a próxima  ?

Link to comment
Share on other sites

Muito boa ideia, e resolvia o meu problema se

Cells(LIN(p), COL(p)).Value = valor

funcionasse.. ou seja, recebo p em argumento e conseguia tirar de p as coordenadas.

Já experimentei: Row() e Column() mas em ambiente excel funciona, em vba não..

Obtenho o erro: Compile error: sub or function not defined

Link to comment
Share on other sites

Resolvido com

    Cells(Range(p).Row, Range(p).Column).Value = valor

De facto não podia ser como estava a pensar visto que a variável p é passada em argumento como STRING! ora uma string não tem como propriedades linha e coluna..

Sabe bem quando a resposta aparece ^^ obrigado p'la dica.

Agora... outra coisita, alguma sugestão em como formatar o texto de uma célula caracter a caracter ?

Necessito de escrever fórmulas matemáticas tipo: Un2

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.