Opening *.txt by code with varible path

  • Thread starter Thread starter carloshernandezy
  • Start date Start date
C

carloshernandezy

Hello to all,


I´m triying to open a *.txt file in a variable path and with a
variable name.


C:\.....\year\weekXX\

After opening the select dialog box I select the *.txt file and convert
with comma delimiters.



-year- is the value of a cell in worksheet
-weekXX- is the value of a cell in worksheet


Thanks for your help
 
Take a look at GetOpenFilename in VBA help.

--
HTH

Bob Phillips

Hello to all,


I´m triying to open a *.txt file in a variable path and with a
variable name.


C:\.....\year\weekXX\

After opening the select dialog box I select the *.txt file and convert
with comma delimiters.



-year- is the value of a cell in worksheet
-weekXX- is the value of a cell in worksheet


Thanks for your help
 
Hello Bob,
I´ve been triying a code like this and it returns an error "13" in
line >>>>


Sub TXT_OPEN()

Dim varDir, varYear, varWeek As String
Dim ws As Workbook
Dim RutaArchivo As String
Set ws = ActiveWorkbook

Application.DisplayAlerts = False

varDir = "C:\....\VB\"
varYear = Range("L8")
varWeek = Range("L7")
RutaArchivo = varYear & " \ " & varWeek


archivoAAbrir = Application.GetOpenFilename("Archivos de texto (*.txt),
*.txt")


If archivoAAbrir <> False Then


ChDir " C:\....\VB\ "
Workbooks.OpenText Filename:=archivoAAbrir, Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:=
_
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=True, Space:=False, Other:=True, OtherChar:="|",
FieldInfo:= _
Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1),
Array(5, 1), Array(6, 1), Array(7 _
, 1), Array(8, 1))
End If


End Sub
 
You can't use "C:\.....\

you have to put in the path explicitly.

--
Regards,
Tom Ogilvy

Hello Bob,
I´ve been triying a code like this and it returns an error "13" in
line >>>>


Sub TXT_OPEN()

Dim varDir, varYear, varWeek As String
Dim ws As Workbook
Dim RutaArchivo As String
Set ws = ActiveWorkbook

Application.DisplayAlerts = False

varDir = "C:\....\VB\"
varYear = Range("L8")
varWeek = Range("L7")
RutaArchivo = varYear & " \ " & varWeek


archivoAAbrir = Application.GetOpenFilename("Archivos de texto (*.txt),
*.txt")


If archivoAAbrir <> False Then


ChDir " C:\....\VB\ "
Workbooks.OpenText Filename:=archivoAAbrir, Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:=
_
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=True, Space:=False, Other:=True, OtherChar:="|",
FieldInfo:= _
Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1),
Array(5, 1), Array(6, 1), Array(7 _
, 1), Array(8, 1))
End If


End Sub
 
Tom I Know it´s only an example, it is complete in the original code.

Thanks for your advice.

Regards,
 
so this is your problem line?

Unless you just omitted and added double quotes arbitrarily as examples,
then


ChDir "C:\....\VB\" & varYear & " \ " & varWeek

but since you did this

RutaArchivo = varYear & " \ " & varWeek


Why not just do

ChDir "C:\....\VB\" & RutaArchivo



--
Regards,
Tom Ogilvy



Tom I Know it´s only an example, it is complete in the original code.

Thanks for your advice.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top