I'm on the hunt for a solution to this problem.
I have a large number of .csv files, and I'm looking for a way to import them into Excel, as separate sheets in single workbook
Now I've asked in two other forums and after some help was left with two un-finished codes.
first is 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
The VBA seems to work and asks you to choose a folder when you hit OK nothing imports???
The second is all 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
This one seems to hang and times out with the error code
error "System Events got an error: AppleEvent timed out." number -1712
I've been at this for almost a week and while it's fun. (I liked getting to know applescript especially), could really use a hand trying to figure out what is wrong with either or both!!