【问题标题】:Importing multiple csv into excel for mac using VBA /applescript使用 VBA /applescript 将多个 csv 导入 excel for mac
【发布时间】:2020-07-02 03:26:17
【问题描述】:

我正在寻找解决这个问题的方法。

我有大量 .csv 文件,我正在寻找一种将它们导入 Excel 的方法,作为单个工作簿中的单独工作表

现在我在另外两个论坛上问过了,在一些帮助后留下了两个未完成的代码。

首先是VBA

Sub test()
Dim MacReturn As String
Dim FilePaths As Variant
MacReturn = MacScript(ScriptString)

FilePaths = Split(MacReturn, ",")

If UBound(FilePaths) = -1 Then
    MsgBox "chosen folder had no CSV files"
Else
    If FilePaths(0) = "false" Then
        MsgBox "cancel button pressed"
    Else
        MsgBox (UBound(FilePaths) + 1) & " CSV files in folder."
    End If
End If
End Sub

Function ScriptString() As String
ScriptString = "tell application ""Finder"""

ScriptString = ScriptString & vbCr & "    try"
ScriptString = ScriptString & vbCr & "        set CSVfiles to (files of (choose folder) whose    name extension = ""csv"")"
ScriptString = ScriptString & vbCr & "        set allPaths to {}"
ScriptString = ScriptString & vbCr & "        repeat with oneFile in CSVfiles"
ScriptString = ScriptString & vbCr & "            set OnePath to """""
ScriptString = ScriptString & vbCr & "            repeat until (name of oneFile = """")"
ScriptString = ScriptString & vbCr & "                set OnePath to (get name of (oneFile)) & "":"" & OnePath"
ScriptString = ScriptString & vbCr & "                set oneFile to (container of oneFile)"
ScriptString = ScriptString & vbCr & "            end repeat"
ScriptString = ScriptString & vbCr & "            copy (text 1 thru -2 of OnePath) to end of   allPaths"
ScriptString = ScriptString & vbCr & "        end repeat"
ScriptString = ScriptString & vbCr & "activate application ""Microsoft Excel"""
ScriptString = ScriptString & vbCr & "        return allPaths"
ScriptString = ScriptString & vbCr & "    on error"
ScriptString = ScriptString & vbCr & "activate application ""Microsoft Excel"""
ScriptString = ScriptString & vbCr & "        return false"
ScriptString = ScriptString & vbCr & "    end try"
ScriptString = ScriptString & vbCr & "end tell"

End Function

VBA 似乎可以正常工作,并在您点击 OK 时要求您选择一个文件夹,什么都没有导入???

第二个都是applescript

tell application "System Events"
set CSVfiles to path of (files of (choose folder) whose name extension = "csv")
end tell

tell application "Microsoft Excel"
activate
open workbook workbook file name "HD:Users:<my name>:Documents:Office:SOLAR:converted:Daily solar Generation.xlsx"
set targetSheet to worksheet 1 of workbook (name of active workbook)
repeat with thisFile in CSVfiles

    open text file filename thisFile data type delimited

    tell active workbook
        open workbook workbook file name "HD:Users:<myname>:Documents:Office:SOLAR:converted:Daily solar Generation .xlsx"
        set sourceBookName to its name
        set sourceSheet to sheet 1
    end tell
    copy worksheet sourceSheet after targetSheet
    close workbook sourceBookName
end repeat
end tell

这个似乎挂起并显示错误代码超时

错误“系统事件出错:AppleEvent 超时。”编号-1712

我已经在这里待了将近一个星期,虽然它很有趣。 (我特别喜欢了解applescript),真的可以用手来找出其中一个或两个有什么问题!

【问题讨论】:

  • 如果您将范围缩小到似乎发生错误的几行,您可能更有可能得到响应;您可以使用该信息更新您的回复。
  • 这是问题的一部分。 VBA 进入“选择文件夹”框,单击确定没有导入。 Applescript 只是挂起,然后是错误代码。所以发布了很多,但谢谢,从现在开始我会尽量保持简短@StevenW

标签: excel vba macos applescript


【解决方案1】:

Applescript 挂在choose folder,因为它位于System Events tell 块中,但系统事件无法弄清楚如何处理它。将选择的文件夹移出tell块,它应该可以工作。

set theFolder to choose folder
tell application "System Events"
    set CSVfiles to path of (files of theFolder whose name extension = "csv")
end tell

【讨论】:

  • Microsoft Excel 出现错误:无法继续打开工作簿。亮点open workbook workbook file name "HD:Users:my name:Documents:Office:SOLAR:converted:Daily solar Generation.xlsx"@Darrick
  • 我现在不在 Mac 上,但我相信 workbook file name 仅对当前打开的文件有效。试试open (alias "path:to:file")
猜你喜欢
  • 2018-07-14
  • 1970-01-01
  • 2019-04-27
  • 2015-02-23
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
相关资源
最近更新 更多