|
-
Jun 10th, 2013, 11:19 AM
#1
Thread Starter
New Member
[RESOLVED] Importing text files using vba?
Hi!
I'm trying to import two text files using vba. I can import the first file, no problem, but when I try to import the second one, things get a little screwy. Here's what I have so far...
Code:
Sub example(wkbk As Workbook, qrsh, vfilepath As String, ufilepath As String)
With qrsh.QueryTables.Add(Connection:= _
"TEXT;" & vfilepath, _
Destination:=qrsh.Range("$A$2"))
.Name = "SampleTextFileName1"
.TextFilePlatform = 437
.TextFileStartRow = 2
.TextFileParseType = xlFixedWidth
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 9, 1, 9, 1, 9, 1, 9)
.TextFileFixedColumnWidths = Array(3, 9, 3, 2, 7, 2, 108, 10, 16, 10)
.Refresh BackgroundQuery:=False
End With
x = qrsh.Range("A2").End(xlDown).Row
With qrsh.QueryTables.Add(Connection:="TEXT;" & ufilepath, Destination:=qrsh.Range("$A" & x))
.Name = "SampleTextFileName2"
.TextFilePlatform = 437
.TextFileStartRow = 2
.TextFileParseType = xlFixedWidth
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 9, 1, 9, 1, 9, 1, 9)
.TextFileFixedColumnWidths = Array(3, 9, 3, 2, 7, 2, 108, 10, 16, 10)
.Refresh BackgroundQuery:=False
End With
When I import the first file, I get the 7 columns I want from A2, to the last row (normally somewhere in the 800s). Then when I import the second file in the same worksheet at the end of where the first file left off I get my seven columns and all the data, yet the data from the FIRST file gets moved over 7 columns. almost as if the second import is inserting 7 new colums? I'm not really sure if my description makes sense... If anyone has any ideas how to fix this, or a different way to import these files, please let me know! I'm a beginner at this and don't really know what I'm doing?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|