Notifications
Clear all

COMO IMPORTAR DADOS DO MESMO DIRETÓRIO DA PASTA

3 Posts
3 Usuários
0 Reactions
1,282 Visualizações
(@jamesjim)
Posts: 0
New Member
Topic starter
 

Olá!

Quem puder me ajudar. Estou a dias tentando colocar numa macro, que o arquivo em csv que ela está puxando via importação de dados de texto deixe de ser de uma origem fixa e passe ser de onde a planilha ativa estiver salva. Isto vai salvar a minha vida pois compartilharei esta planilha principal na empresa e darei a orientação aos funcionários que rodem a macro quando tiverem salvo as bases que serão importadas, no mesmo local onde está a pasta de trabalho

Na macro está mais ou menos assim:

Sub IMPORTA_BASES()
'
' IMPORTA_BASES Macro
'

'
Sheets("BASE 360").Select
Columns("K:R").Select
Selection.ClearContents
Range("K1").Select
ActiveSheet.Unprotect
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:VALIDADOR VENDASVIVO360.csv", Destination:=Range("$K$1"))
.Name = "VIVO360"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

Eu não quero que o seguinte caminho seja fixo: "C:VALIDADOR VENDAS", quero que a macro carregue o arquivo de onde está a pasta principal

Desde já agradeço

Thiago

 
Postado : 20/11/2017 9:51 pm
(@wzxnet7)
Posts: 0
New Member
 

Bom dia.
O método thisworkbook.path captura o local onde a pasta de trabalho está salva.
Vc pode ajustar isso na sua rotina e depois só acrescentar o nome do arquivo a ser importado.

 
Postado : 21/11/2017 1:34 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Bom dia!!

Seria isso?

Sub IMPORTA_BASES()
'
' IMPORTA_BASES Macro
'

' ===== AleVBA =====
Dim strName As String
strName = ThisWorkbook.Path & "" 'favor editar se necessário
' ===== AleVBA =====

Sheets("BASE 360").Select
Columns("K:R").Select
Selection.ClearContents
Range("K1").Select
ActiveSheet.Unprotect
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;strNameVIVO360.csv", Destination:=Range("$K$1")) 'Editado por AleVBA
    .Name = "VIVO360"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 1252
    .TextFileStartRow = 2
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = True
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
End Sub

Att

 
Postado : 21/11/2017 6:20 am