导入多个文件.blst(看起来像.csv)到excel

时间:2019-03-31 05:04:17

标签: excel vba

我想将多个.blst文件导入一个工作表,并希望将.blst文件水平放置在工作表中。哪个文件将分开放置大约23列,例如第一个文件放在A1-W1列和第二个X1-AT1继续... n文件。但是我的代码无法打开它们。

下面的代码是转换为字母函数

Function ConvertToLetter(iCol As Integer) As String
   Dim iAlpha As Integer
   Dim iRemainder As Integer
   iAlpha = Int(iCol / 27)
   iRemainder = iCol - (iAlpha * 26)
   If iAlpha > 0 Then
      ConvertToLetter = Chr(iAlpha + 64)
   End If
   If iRemainder > 0 Then
      ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
   End If
   Debug.Print ConvertToLetter & 1
End Function

下面的代码是导入向导功能

Function import_wizard(xFileName, xAddress) As String

   With ActiveSheet.QueryTables.Add("TEXT;" & xFileName, Range(xAddress))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 936
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = ";"
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With


End Function

下面的代码是浏览和导入多文件.blst的按钮

Private Sub browseXML_Click()

    Dim xFileName As Variant
    Dim xAddress As String
    Dim countFile As Integer


    On Error GoTo ErrHandler

    xFileName = Application.GetOpenFilename(FileFilter:="blst Files,*.*", Title:="Select file", MultiSelect:=True)

    If IsArray(xFileName) Then
            'Msg = vbNewLine
        For i = LBound(xFileName) To UBound(xFileName)
            Msg = Msg & xFileName(i) & vbCrLf
            countFile = i + 23

            xAddress = ConvertToLetter(countFile) & "1"
            SplitterMark.TextBox1.Value = Msg
            Call import_wizard(xFileName, xAddress)

        'Debug.Print "X = " & xAddress
        Next i

    Else
        MsgBox "No files were selected."
        GoTo ExitHandler
    End If

ExitHandler:
ErrHandler:
End Sub

当我厌倦评论' Call import_wizard(xFileName, xAddress)时,代码可以选择多个文件并可以在UI上显示,但是厌倦删除注释Call import_wizard(xFileName, xAddress)可以选择多个文件,但仅显示select中的最后一个文件,而不能显示打开文件。我不确定它是否不会ActiveSheet.QueryTables.Add("TEXT;" & xFileName, Range(xAddress))。有人可以建议吗?谢谢

1 个答案:

答案 0 :(得分:0)

您需要“(i)”来选择此代码行中的每个文件:

Call import_wizard(xFileName(i), ...

您不需要“ ConvertToLetter”功能即可将列号转换为地址。
我建议像这样计算下一个导入列:

...
Dim NextColumn As Long
For i = LBound(xFileName) To UBound(xFileName)
    NextColumn = (i - 1) * 23 + 1
    Call import_wizard(xFileName(i), NextColumn)
Next i
...

然后,您的导入例程必须像这样开始:

Function import_wizard(ByVal xFileName as String, NextColumn as Long) As String
   With ActiveSheet.QueryTables.Add("TEXT;" & xFileName, ActiveSheet.Cells(1, NextColumn))
       ...