注册 登录
编程论坛 VB6论坛

路径是变量的问题,新手求教啊

南国嘉树 发布于 2015-09-25 21:32, 521 次点击
我要把txt中数据导入到excel,代码是在excel中录制的宏指令。txt文件的路径是变量A(),其中A(i)是txt文件的完整路径,类似C:\Users\THINK\Desktop\数据.txt这种。
代码中With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\THINK\Desktop\数据.txt", Destination:=Range("Sheet1!$A$1"))要怎么改才可以?

程序代码:
Private Sub Macro(A() As String)
Dim n As Integer
n = Val(Text1.Text)

Dim i As Integer

For i = 1 To n

   Debug.Print A(i)

Sheets("Sheet" & i).Select
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\THINK\Desktop\数据.txt", Destination:=Range("$A$1"))
        .Name = "1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 936
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

Next

End Sub
3 回复
#2
风吹过b2015-09-26 10:07
"TEXT;" & a(i), Destination:=Range("$A$1"))
这样行吗?
会用 字串符连接命令吗?
#3
南国嘉树2015-09-30 09:01
回复 2楼 风吹过b
解决了。
#4
南国嘉树2015-09-30 09:01
回复 2楼 风吹过b
谢谢版主
1