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
#24406
Bom dia pessoal,

Sou novo nessa área de VBA e meu sistema gera muitos arquivos em CSV, porém todos necessitam de formatação e para resolver isso, gero na 1a linha do arquivo os formatos das mascaras de cada coluna e com uma macro consigo formatar todas as colunas do jeito que eu defini na primeira linha do arquivo.

Até ai tudo bem, mas o que gostaria de fazer mesmo é abrir o arquivo CSV e rodar a macro automaticamente sem que o usuário necessite pressionar um botão ou uma tecla de atalho.

Seria algo assim: c:\macros\autoexec.xls "arquivo.csv"

Eu já vi uma planilha chamada AUTOOPEN.XLS usada no SAP B1 que parece fazer isso, mas não entendi e não consegui fazê-la funcionar.

Se puderem me ajudar eu agradeço.

Abraços.
#24450
Bom dia!!

Eu não entendi.

Você pretendi apenas abrir o arquivo CSV?
Código: Selecionar todos
Sub EuNaoEntendi()
Dim sCSVFullName As String
  sCSVFullName = Application.GetOpenFilename("CSV Files (*.csv), *.csv", , _
      "Abra seu arquivo CSV", , False)
' Sua  macro faz o resto

End Sub
Se puder explicar com mais detalhe.
Leia:
http://www.homeandlearn.org/open_a_text ... n_vba.html
https://peltiertech.com/simple-vba-open ... fied-data/
Att
#24463
Olá AlexandreVBA,

O que vc me passou funciona, porém eu preciso escolher qual arquivo eu quero abrir.

A ideia é eu informar o arquivo que eu quero abrir como um parâmetro e a macro abrir o arquivo passado por um comando.

Lembrando que o nome do arquivo CSV não é o mesmo sempre, ele varia.

O comando seria algo assim: C:\abrecsv.xlsm "arquivo.csv" ao abrir o abrecsv.xlsm a macro seria executada e abriria o arquivo informado na linha de comando.

Não sei se consegui me fazer entender.

Obrigado.
#24465
Boa tarde!

Eu não entendi.

Tente adaptar esse código.
Código: Selecionar todos
' Fonte: https://sites.google.com/a/madrocketscientist.com/jerrybeaucaires-excelassistant/merge-functions/csvs-to-1-sheet
Sub ImportCSVsWithReference()
'Author:    Jerry Beaucaire
'Date:      10/16/2010
'Summary:   Import all CSV files from a folder into a single sheet
'           adding a field in column A listing the CSV filenames

Dim wbCSV   As Workbook
Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.Sheets("Plan1")
Dim fPath   As String:      fPath = "C:\Users\aleVBA\Desktop\"    'path to CSV files, include the final \
Dim fCSV    As String

If MsgBox("Limpar os dados da guia antes de importar?", vbYesNo, "Limpar dados?") _
    = vbYes Then wsMstr.UsedRange.Clear

Application.ScreenUpdating = False  'speed up macro

fCSV = Dir(fPath & "*.csv")         'start the CSV file listing

    Do While Len(fCSV) > 0
      'open a CSV file
        Set wbCSV = Workbooks.Open(fPath & fCSV)
      'insert col A and add CSV name
        Columns(1).Insert xlShiftToRight
        Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
      'copy date into master sheet and close source file
        ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
        wbCSV.Close False
      'ready next CSV
        fCSV = Dir
    Loop
 
Application.ScreenUpdating = True
End Sub
Att
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