Excel planilha vba variavel tipo retorna dados

Sáb, 13 de Agosto de 2011 16:14 Expedito Marcondes
Imprimir

Saberexcel - o site de quem precisa aprender macros microsoft excel vba

E
sses procedimentos do Aplicativo Microsoft Excel VBA(Visual Basic application), usando variaveis tipo (type), retornam valores especificos em determinada linhas e ou colunas células.

Private Type Registro
    Codigo As Single
    Combustivel As Integer
    ID As String * 12
    Caracteristicas As String * 20
    Posicao As Integer
End Type

Sub inserindo_dados_var_type()
    Dim Linha(3) As Registro 'vetor dinâmico
    Dim i, sbx As Integer
   
    Application.ScreenUpdating = False
   
    'Como atribuir dados, existem várias fórmas
    Linha(0).Codigo = 1
    Linha(0).Combustivel = 1
    Linha(0).ID = "42456"
    Linha(0).Caracteristicas = "Transporte Carga"
    Linha(0).Posicao = 1
    Linha(1).Codigo = 2
    Linha(1).Combustivel = 2
    Linha(1).ID = "42457"
    Linha(1).Caracteristicas = "Transporte Urbano"
    Linha(1).Posicao = 2
    Linha(2).Codigo = 3
    Linha(2).Combustivel = 3
    Linha(2).ID = "42458"
    Linha(2).Caracteristicas = "Transporte Urbano"
    Linha(2).Posicao = 3
   
    'Preechendo a planilha com os dados
    Range("A1").Value = "CODIGO"
    Range("B1").Value = "COMBUSTÍVEL"
    Range("C1").Value = "ID"
    Range("D1").Value = "CARACTERISTICAS"
    Range("E1").Value = "POSIÇÃO"
   
    'Formatando cabeçalho
    Range("A1:E1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
   
    Selection.Font.Bold = True
    Columns("A:E").ColumnWidth = 14.01
    'Columns("A:E").AutoFit
    Range("A2").Select
   
    'Preechendo a folha de planilha
    For i = 2 To UBound(Linha) + 1
        For sbx = 0 To 2
            Range("A" & i).Value = Linha(sbx).Codigo
            Range("B" & i).Value = Linha(sbx).Combustivel
            Range("C" & i).Value = Linha(sbx).ID
            Range("D" & i).Value = Linha(sbx).Caracteristicas
            Range("E" & i).Value = Linha(sbx).Posicao
            i = i + 1
        Next
    Next
   
    Application.ScreenUpdating = True
    MsgBox "Dados inseridos com Sucesso!", vbDefaultButton1, "Saberexcel - o site das macros"
End Sub

Sub limpar_teste()
Plan1.[A1:E100].ClearContents
End Sub



Aprenda tudo sobre planilhas do Aplicativo Microsoft Excel VBA(Visual Basic Application), sozinho, com baixo custo, praticando com os produtos didáticos Saberexcel




   Baixe o exemplo de planilha contendo os procedimento com as variáveis acima
Excel planilha vba variavel tipo como usar (13.4 KB)

Tags:
Última atualização em Sáb, 13 de Agosto de 2011 16:48