macro to Import csv file into an excel non active worksheet

前端 未结 2 899
你的背包
你的背包 2021-02-01 10:46

I have a macro enabled excel workbook that contains several named worksheets. One of the worksheets is named \"panel\" and a second worksheet is named \"data\". The sheet named

相关标签:
2条回答
  • 2021-02-01 11:08

    For Excel on Mac, it seems the QueryTable object does not support the properties "PreserveFormatting" and "RefreshPeriod" and will give you a runtime error if you try and set them.

    Also, Application.FileDialog does not work with Mac either, but that is covered in other posts.

    For Mac:

    Sub load_csv()
    Dim fStr As String
    
    fStr = "Macintosh HD:Users:anthony:Documents:example.csv" 'Keeping file String simple for example.
    
    With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _
    "TEXT;" & fStr, Destination:=Range("$A$1"))
        .Name = "CAPTURE"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        '.PreserveFormatting = True  **commented out for Mac
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        '.RefreshPeriod = 0  **commented out for Mac
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    
    End With
    End Sub
    
    0 讨论(0)
  • 2021-02-01 11:22

    Is this what you are trying?

    Sub load_csv()
        Dim fStr As String
    
        With Application.FileDialog(msoFileDialogFilePicker)
            .Show
            If .SelectedItems.Count = 0 Then
                MsgBox "Cancel Selected"
                Exit Sub
            End If
            'fStr is the file path and name of the file you selected.
            fStr = .SelectedItems(1)
        End With
    
        With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _
        "TEXT;" & fStr, Destination:=Range("$A$1"))
            .Name = "CAPTURE"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    
        End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题