QueryTable

jmvanhilten

New Member
Joined
Feb 25, 2010
Messages
2
Hi,

I have some code to read in several textfiles in specific sheets in a workbook. The problem is weird.
-First, If the name of the textfiles is hardcoded everything works fine.
-Second, if the name of the textfiles is read out fo the excell-sheet, it sometimes works and sometimes it doesn't.

Code:
Public Sub FixedWidthImport()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ThePath As String
Dim TheName As String
Dim Stat As String
 
' read several cells for .txt file location and name
Worksheets("MacroVariables").Activate
ThePath = ActiveSheet.Range("$C$11")
TheName = ActiveSheet.Range("$C$12")
 
'call sub to read file
Stat = "Static01"
Call Static01(ThePath, TheName, Stat)
 
End Sub
 
Public Sub Static01(ThePath, TheName, Stat)
 
Dim Name1 As String
Dim Name2 As String
 
 Worksheets("Static01").Activate
 ActiveSheet.Columns("A:I").Select
 Selection.ClearContents
 
Name1 = ThePath & "\Output\" & TheName & "_" & Stat & ".out"
Name2 = TheName & "_" & Stat & ""
 
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & Name1, Destination:=ActiveSheet.Range("$A$3"))
        .Name = Name2
        .PreserveFormatting = True
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .TextFilePlatform = 437
        .TextFileStartRow = 11
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileFixedColumnWidths = Array(5, 5, 10, 10, 10, 10, 10, 10)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=True
     End With
 
    Worksheets("MacroVariables").Range("F6:R6").Copy
    ActiveSheet.Range("A1:M1").Select
    ActiveSheet.Paste
 
 End Sub

Anyone any idea?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Welcome to the Board.

In what way doesn't it work (sometimes)?

Why the $ signs in?

ThePath = ActiveSheet.Range("$C$11")
TheName = ActiveSheet.Range("$C$12")

Try:

ThePath = ActiveSheet.Range("C11").Value
TheName = ActiveSheet.Range("C12").Value
 
Upvote 0
I get a runtime error '1004', which says excell cannot access the text file. I'm 100% sure the file exists and there are no mistypes in the name.
If I debug it jumps to .Refresh BackgroundQuery. So, the wholen errormessage doesn't say so much.

Funny thing is that the code worked 2 or 3 times, and suddenly it does not work anymore.

When I hardcode Name1 and Name2 in the code, everything works fine.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top