Exportar para XML apenas as células preenchidas

lboff

16-03-2019 02:49:32

Boa noite,

Utilizo uma Macro VBA para gerar um arquivo XML para cada linha do excel.
Porém, preciso que para células que estão vazias, a Macro VBA desconsidere e não inclua no arquivo.
Segue abaixo.

Código: Selecionar todos

Sub ExportToXml()

      
    Dim linha As Long, coluna As Long, colunas As Long
    linha = 2
    colunas = ActiveSheet.UsedRange.Columns.Count
    
    
    With ActiveSheet
        Do While Not IsEmpty(.Cells(linha, 1))
        
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    
            
    Set a = fs.CreateTextFile(Cells(linha, 1).Value & ".xml", True)
    
    
    'cria as primeiras linhas
    a.WriteLine ("<?xml file>")
    a.WriteLine ("<IncluirRelatorioFinanceiro>")
         
        coluna = 1
        a.Write (Chr(9) & _
        "<NumeroDocumento>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</NumeroDocumento>" & Chr(13))
            
        a.WriteLine
        coluna = 2
        a.Write (Chr(9) & _
        "<NomeTomadorServico>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</NomeTomadorServico>" & Chr(13))
             
       a.WriteLine
        coluna = 3
        a.Write (Chr(9) & _
        "<Cidade>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</Cidade>" & Chr(13))
                
        a.WriteLine
        coluna = 4
        a.Write (Chr(9) & _
        "<DataInicio>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</DataInicio>" & Chr(13))
                
        
        a.WriteLine
        coluna = 5
        a.Write (Chr(9) & _
        "<DataConclusao>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</DataConclusao>" & Chr(13))
                
           
        a.WriteLine
        coluna = 6
        a.Write (Chr(9) & _
        "<Valor>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</Valor>" & Chr(13))
    
   

            linha = linha + 1
            
    'finaliza o arquivo
    a.Close


        Loop
    End With
 
    
 
    MsgBox "Exportação concluída!"
End Sub

Wagner Morel

18-03-2019 14:38:12

Você pode anexar aqui no fórum seu arquivo compactado com .ZIP?

lboff

18-03-2019 22:22:53

Segue abaixo arquivos.

Reinaldo

19-03-2019 18:25:13

veja se atende

Código: Selecionar todos

Sub ExportToXml()
    Dim linha As Long, coluna As Long, colunas As Long
    linha = 2
    colunas = ActiveSheet.UsedRange.Columns.Count
With ActiveSheet
    Do While Not IsEmpty(.Cells(linha, 1))
    
Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.CreateTextFile(Cells(linha, 1).Value & ".xml", True)

'cria as primeiras linhas
a.WriteLine ("<?xml file>")
a.WriteLine ("<IncluirRelatorioFinanceiro>")
     
    coluna = 1
    If Len(RTrim(.Cells(linha, coluna).Value)) > 0 Then
        a.Write (Chr(9) & _
        "<NumeroDocumento>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</NumeroDocumento>" & Chr(13))
        a.WriteLine
    End If
    coluna = 2
    If Len(RTrim(.Cells(linha, coluna).Value)) > 0 Then
        a.Write (Chr(9) & _
        "<NomeTomadorServico>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</NomeTomadorServico>" & Chr(13))
        a.WriteLine
     End If
    coluna = 3
    If Len(RTrim(.Cells(linha, coluna).Value)) > 0 Then
        a.Write (Chr(9) & _
        "<Cidade>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</Cidade>" & Chr(13))
    a.WriteLine
     End If
    coluna = 4
    If Len(RTrim(.Cells(linha, coluna).Value)) > 0 Then
        a.Write (Chr(9) & _
        "<DataInicio>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</DataInicio>" & Chr(13))
    a.WriteLine
      End If
    coluna = 5
    If Len(RTrim(.Cells(linha, coluna).Value)) > 0 Then
        a.Write (Chr(9) & _
        "<DataConclusao>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</DataConclusao>" & Chr(13))
    a.WriteLine
End If
    coluna = 6
    If Len(RTrim(.Cells(linha, coluna).Value)) > 0 Then
        a.Write (Chr(9) & _
        "<Valor>" & _
        RTrim(.Cells(linha, coluna).Value) & _
        "</Valor>" & Chr(13))
    End If

        linha = linha + 1
        
    'finaliza o arquivo
    a.Close
    Loop
End With
    MsgBox "Exportação concluída!"
End Sub

lboff

19-03-2019 18:49:46

Funcionou, muito obrigada!