DONUT PROJECT - VBA - Excel - Importando arquivos CSV




Importar dados de arquivos CSVs é algo tão comum, que o MS Excel já tem um mecanismo próprio para fazer isso. Mas esta continua sendo uma das dúvidas mais comuns. Como fazer essa importação, através do VBA?

 Série eBook Donut Projects: 

DONUT PROJECT: VBA - Projetos e Códigos de Visual Basic for Applications (Visual Basic For Apllication)eBook - DONUT PROJECT 2024 - Volume 03 - Funções Financeiras - André Luiz Bernardes eBook - DONUT PROJECT 2024 - Volume 02 - Conectando Banco de Dados - André Luiz Bernardes eBook - DONUT PROJECT 2024 - Volume 01 - André Luiz Bernardes



Uma alternativa é a de ler o arquivo CSV como se fosse um arquivo texto, e em seguida carregar cada linha em uma matriz, e através de loops inserí-los no MS Excel.

'This sub only provides the sub ImportCSVfile with parameters

Sub InitiateImportCSVFile()
        Dim filePath As String
        Dim ImportToRow As Integer
        Dim StartColumn As Integer      

        Let filePath = "E:\Bernardes\Arquivo.csv"
        Let ImportToRow = 1 'the row where it will start printing
        Let StartColumn = 1 'the start column

        ImportCSVFile filePath, ImportToRow, StartColumn
End Sub

'  Este é o código que faz todo o trabalho:

Sub ImportCSVFile 
(ByVal filePath As String, ByVal ImportToRow As Integer, ByVal StartColumn As Integer) 

    Dim line As String
    Dim arrayOfElements
    Dim element As Variant

    Open filePath For Input As #1 ' Open file for input

        Do While Not EOF(1) ' Loop until end of file

            ImportToRow = ImportToRow + 1

            Line Input #1, line

            Let arrayOfElements = Split(line, ";") 'Split the line into the array.          

            ' Loop thorugh every element in the array and print to Excelfile

            For Each element In arrayOfElements

                Let Cells(ImportToRow, StartColumn).Value = element

                Let StartColumn = StartColumn + 1

            Next

        Loop

    Close #1 ' Close file.
End Sub




Conheça também:

DONUT PROJECT 2021 - VBA Function:  Como Rastrear o Google Maps (Coordenadas Geográficas) no VBA Excel?

DONUT PROJECT 2021 - VBA Function:  Crie Acrônimos a partir de Strings de Texto

DONUT PROJECT 2021 - VBA Function:  Convertendo uma Matrix num Vetor - Convert Matrix to a Vector

DONUT PROJECT 2021 - VBA Function:  Como tornar o Formulário Transparente no MS Excel?

DONUT PROJECT 2021 - VBA Function:  Faça Buscas no Google a Partir da Célula do MS Excel - Search Google From a Cell

DONUT PROJECT 2021 - VBA Function:  Decompondo um Nome nas Dimensões de uma Matriz

DONUT PROJECT 2021 - VBA Function: Extraindo o Último Sobrenome de um Nome Completo ou a Última Palavra de uma Frase

DONUT PROJECT 2021 - VBA Function:  Extraindo o Segundo Nome de um Nome Completo ou a Segunda Palavra de uma Frase

DONUT PROJECT 2021 - VBA Function: Extraindo o Primeiro Nome ou  a Primeira Palavra de uma Frase

Série Piece of Cake

Séries Donut

Leia também:

eBook: Série DONUT PROJECT 2015: Projetos e Códigos de Visual Basic for Applications - Autor: André Luiz Bernardes

eBook: Série Top 10 Funções: Top 10 Funções VBA para o Microsoft Excel - Autor: André Luiz Bernardes

eBook: Série Funções Poderosas: 13 Funções Poderosas no MS Excel - Autor: André Luiz Bernardes

eBook: Série Visual Basic For Application: Criando Logs de acesso: Dicas e Códigos de Visual Basic for Applications - Autor: André Luiz Bernardes

eBook: Série VBA Tips: Rastrei seus Dashboards, Scorecards, Reports, Relatórios, Planilhas e Aplicações - Dicas e Códigos - Autor: André Luiz Bernardes

eBook: Série Data Science: Big Data, Como? - Autor: André Luiz Bernardes

eBook: Série Smarter Analytic: 5 Previsões de Big Data - Autor: André Luiz Bernardes


Comente e compartilhe este artigo!

brazilsalesforceeffectiveness@gmail.com

Nenhum comentário:

Postar um comentário

diHITT - Notícias