Excel for Mac,用户选择,将CSV导入特定工作表

时间:2015-04-11 15:59:44

标签: excel macos vba csv excel-vba-mac

我在构建一个允许用户选择CSV然后将其导入特定工作表的宏时遇到了一些麻烦。

我找到this script,它将用户选择的文件导入到新工作簿中,并尝试根据我的需要对其进行修改 - 但到目前为止没有运气..

到目前为止,这是我的脚本:

    Sub Select_File_Or_Files_Mac()
    Dim MyPath As String
    Dim MyScript As String
    Dim MyFiles As String
    Dim MySplit As Variant
    Dim N As Long
    Dim NR As Long
    Dim Fname As String
    Dim mybook As Excel.Workbook
    Dim mysheet As Excel.Worksheet

    On Error Resume Next
    MyPath = MacScript("return (path to documents folder) as String")
    'Or use MyPath = "Macintosh HD:Users:YourUserName:Desktop:TestFolder:"

    MyScript = "set applescript's text item delimiters to (ASCII character 10) " & vbNewLine & _
            "set theFiles to (choose file of type " & _
          " (""public.comma-separated-values-text"") " & _
            "with prompt ""Please select a file or files"" default location alias """ & _
            MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
            "set applescript's text item delimiters to """" " & vbNewLine & _
            "return theFiles"

    MyFiles = MacScript(MyScript)
    On Error GoTo 0

    If MyFiles <> "" Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    MySplit = Split(MyFiles, Chr(10))
        For N = LBound(MySplit) To UBound(MySplit)

            'Get file name only and test if it is open
            Fname = Right(MySplit(N), Len(MySplit(N)) - InStrRev(MySplit(N), _
            Application.PathSeparator, , 1))

                On Error Resume Next
                Set mysheet = ThisWorkbook.Sheets("Rapport")
                On Error GoTo 0
             Next

Worksheets("Rapport").Activate

NR = mysheet.Range("A" & Rows.Count).End(xlUp).Row + 1

With mysheet.QueryTables.Add(Connection:="TEXT;" & Fname, _
        Destination:=mysheet.Range("A" & NR))
        .Name = "CSV" & Worksheets.Count + 1
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlMacintosh
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
End With

              End If

End Sub

注意:应该插入CSV的表格称为“Rapport”atm。

现在我正在使用Apple脚本来运行CSV并替换“;”与“,” - 所以任何实现这一点的方式都将受到高度赞赏!

谢谢!

1 个答案:

答案 0 :(得分:0)

我只需打开文件,然后使用Excel的TextToColumns函数将字段分隔为列:

'Open the text file with semicolon delimiters
Workbooks.Open Filename:=Fname

'Split the fields separated by semicolons out into columns
Columns("A:A").TextToColumns Destination:=Range("A1"), _
                             DataType:=xlDelimited, _
                             TextQualifier:=xlDoubleQuote, _
                             ConsecutiveDelimiter:=False, _
                             Tab:=False, _
                             Semicolon:=True, _
                             Comma:=False, _
                             Space:=False, _
                             Other:=False