The Applescript below works fine without any issues in Excel 2011. The script opens an Excel file, deletes a column and removes any "," or ";" from the Excel file and then saves it as a CSV file. The issue I'm running into in Excel 2016 is the last piece to save it as a CSV file after the manipulation. Nothing is saved and I don't get any errors.
tell application "Microsoft Excel"
activate
open theWorkbookFile #open the xls file
set theWorksheetname to name of worksheet 1 of active workbook
set theWorksheet to worksheet 1 of active workbook
activate object theWorksheet
tell application "System Events" to set visible of process "Microsoft Excel" to false
#Remove the first column
tell theWorksheet
delete range column 1 shift shift to left
try
##remove any "," and ";" from the product description and replace it with a " " instead.
replace (range "B:B" of worksheet "Sheet1") what "," replacement " "
replace (range "B:B" of worksheet "Sheet1") what ";" replacement " "
end try
end tell
#Set the temp csv file to the name of of the Excel sheet followed by -TMP.csv
set theFile to theWorksheetname & "-TMP" & ".csv" as text
save as theWorksheet filename theFile file format CSV file format with overwrite #save the file in csv format and overwrite if it exist
close active workbook saving no #close the csv file without prompting user to save again
end tell
I came across the same issue and managed to save as CSV kind of "manually", using AppleScript to do the user interaction that the user should do.
My code is below...
#here you choose the name to your CSV file
set fileName to "ClearD -" & (current date) as string
#here you should have your original .XLSX file name and address to open it further
set theDoc to ((path to desktop folder as text) & "ClearD-Data.xlsx") as string
tell application "Microsoft Excel"
activate
open file theDoc
delay 1
tell application "System Events"
keystroke "s" using {command down, shift down}
delay 0.3
tell front window of (first application process whose frontmost is true)
click pop up button 2 of sheet 1
delay 0.3
#we are doing this to select CSV in the dropdown menu
repeat 3 times
keystroke "c"
end repeat
key code 36 # hit return
delay 1
repeat 50 times # this will delete whatever the current file name is and put fileName instead
key code 123 using {shift down}
end repeat
delay 1
#this will type your file name
keystroke fileName
delay 1
click button "Save" of sheet 1
-- set uiElems to entire contents # I use this command only to see what is out there for me to use and troubleshoot
end tell
end tell
#now we are all good so we will close excel
close active workbook saving no
end tell
Related
My CSV file:
Product Code,Product Description,Net Weight,POR Number,BBE Info (DD/MM/YY)
0001450,Californian Whole Almonds,22.68kg,POR17195,21/11/19
Excel file, when importing the CSV file.
Question
I'd like to place the POR Number column to the left of Product Code. But when I refresh the data, it goes back to it's original place.
How can I load the CSV file into excel and choose which column loads up where? Without adjusting the CSV structure.
Here is a desired output when I refresh the CSV data:
Read the file line by line and use split to split the columns and then output the data where you need.
Use application.OnTime to run the code every minute.
Make NextRun a global date variable
sub split_csv()
File = FreeFile()
Open "csv.csv" For Input As #File
i = 2
While Not EOF(File)
Line Input #File, csvLine
cols = split(csvLine, ",")
range("A" & i).value = cols(1) ' and so on...
i = i+1
Wend
NextRun = Now + timevalue("00:01:00")
Application.OnTime EarliestTime:=NextRun, Procedure:="split_csv", Schedule:=True
end sub
To stop the code from running you have to use Application.OnTime EarliestTime:=NextRun, Procedure:="split_csv", Schedule:=False, I'll advice you to add that to workbook_close or if you forget to turn off the function it will open the workbook again and keep going.
you can use the integrated feature Get & Transform (Excel 2016) or earlier Version with the MS Power Query Add-in.
Go to Data > New Query > From File > From CSV
Select your csv file. Click Import.
A preview of the csv data will be shown. Click Edit.
Go to Home > Transform > Use First Row As Headers
Move the column you want with drog & drap over the column header
you may remove some columns with right click on the column header
Give it a try. The UI is very intuitive and you don't have to write any code for most transforming tasks.
I have a problem with a select file window popping up while updating a link to an excel file. I believe it's caused by the destination file being saved at the moment I refresh the link.
I have 2 computers. I open file "A" on the first one and file "B" on the other.
File "A" runs this macro:
application.enableevents = false
Do until(I set loop for couple minutes )
Range("a1").value = 1
save
range("a1).value = 2
save
Loop
File B runs this macro:
range("b2").value = (link to File A, cell "a1")
z = 1
do until (runs for couple minutes)
Cells(z,1).value = range("b2").value
z = z+1
loop
So basically the first file continuously changes the value of cell "a1" between 1 and 2, and saves each time, while the second one constantly refreshes the link and records the linked cell value.
It works for a few rounds, then a select file window pops up.
Exact formula for the cell containing link:
"='[" & BAZAO.Name & "]" & "ZMIANY'" & "!a1"
And in excel cell it looks like that:
„='[BWP 215.xlsm]ZMIANY'!a1”
The line of code that refreshes the link:
BAZA.UpdateLink (BAZAZ.Range("C3").Value)
BAZA - name of FILE B
BAZAZ.range("c3").value - contains address and name of file A
Range("b2").value = "='[" & BAZAO.Name & "]" & "ZMIANY'" & "!a1"
„='[BWP 215.xlsm]ZMIANY'!a1”
Can anyone tell me how to suppress this select file window, or if there is any other workaround?
I think it's happening because when opening the excel file, excel creates it's copy with generated name. When changes are being saved and the original file replaced, excel deletes original file first, and then puts a temporary file in its place changing name to the original one.
I guess the select file window pops up when my macros hit between that moments.
Is this the cause?
I think I have found a workaround.
Instead of using a cell to track file avaliability I have made the code to create file "inuse.txt" in the "file A" directory whenever its in use.
Then the "file B" just checks if that file is present through dir command
I am currently using this Applescript I found that searches for a file name and returns the file path in a text doc. This works fine for finding 1 or 2 files, but I would like to find 500 files that are spread over hundreds of folders. My ideal script would use data from an excel spreadsheet or csv, perform a search, find the file and make a copy of it in a designated folder on my desktop. Any help is appreciated.
Here is the script I found:
tell application "System Events"
activate
set thePattern to text returned of (display dialog "Search for" default answer "")
end tell
if thePattern = "" then return
try
set foundFiles to do shell script "mdfind -name " & quoted form of thePattern & " | /usr/bin/egrep -i " & quoted form of thePattern & "[^/]*/?$ | /usr/bin/grep -vi " & quoted form of thePattern & ".*" & quoted form of thePattern
on error
set foundFiles to "Nothing Returned"
end try
if foundFiles = "" then set foundFiles to "Nothing Returned"
tell application "TextEdit"
activate
delay 0.5
try
set theDoc to document 1
get text of theDoc
if result is not "" then
make new document
set theDoc to result
end if
on error
make new document
set theDoc to result
end try
set text of theDoc to foundFiles
end tell
You need to read the data from the text file, then turn it into a return or linefeed delimited list and do a repeat over the items of this list. Then turn each item (which is actually a line) into e.g. a tab delimited list and again do a (nested) repeat loop over the items of this list. If you know that e.g. item 3 is the file path, you can set a variable to item 3 of the line as text and use this variable in your shell script.
I think you need to show that you understand the concept of repeat loops by posting your own attempt of implementing this. If you do, I'll be happy to come back and help you with the next step.
Kind regards,
Mark
I am using Vbscript to write some data into excel then i am saving this excel object as txt file. My problem here is after saving as text file some rows contains quotes ". Below is my code can some body help me recording this.
My Output text file is:
"Rules*V*ZBEA*892**0010*10*IBM-01**"
30,000.00*01/08/2012*21/08/2012****0000013556*01***2600
"Scale value* *********"
problem here is 1st and 3rd row starts and ends with quotes (" ").
code is
Dim objXL1,name
Set objXL1 = CreateObject("Excel.Application")
objXL1.Workbooks.Add
objXL1.Cells(1 ,1) = "Rules*V*ZBEA*892**0010*10*IBM-01** "
objXL1.Cells(2,1) = "30,000.00*01/08/2012*21/08/2012****0000013556*01***2600"
objXL1.Cells(3 ,1) = "Scale value* *********"
name = objXL1.GetSaveAsFilename(,"Text(MS-DOS)(*.txt),*.txt")
objXL1.ActiveWorkbook.SaveAs name ,21 ,,21
objXL1.ActiveWorkbook.Close 0
objXL1.quit
And here once more issue is I am using SaveAs method for getting file name. When execution comes to this line the file save dialog box hiding behind the main IE window is there any way to get this save dialog box in focus?
I assume this is because of the whitespace. In your code there is a trailing whitespace in the frist line.
"Rules*V*ZBEA*892**0010*10*IBM-01** "
-----------------------------------^-
If you would export multiple cells you would need to encapsualte those cells to recognize where a cell value begins and ends.
I have about 400 excel files. I want to insert a column just before the first column in the existing file and then insert the name of the file into each row of that column.
I know a little bit of Applescript and based on that I wrote this script so that I can drop some files onto the script and it will execute the script on each one of those files.
I was wondering if someone could help me in completing the "TO DO" lines. Upon execution this script gives me dialogue boxes with the path of files that I drop on top. But the excel application throws an error dialogue box which says" Not enough memory". I tried this with only 2 excel files so it wasn't the number of files that caused the error.
Can someone please give me a hand with completing the TODO lines and give me an ide as to why Im getting the error. Thanks
property numFiles : 0
on open excelFiles
set fileNames to ""
tell application "Finder"
repeat with eachFile in excelFiles
--open document file eachFile
--tell application "Microsoft Excel"
--increment count
--save name of each file
set fileNames to fileNames & return & (POSIX path of eachFile)
--TO DO insert a column
--TO DO insert text in each column to the name of eachFile
--end tell
end repeat
display dialog fileNames
--display dialog "Ouch that hurt " & return & "You dropped " & (count excelFiles) & "files on me"
end tell
end open
on addFilePath(eachFile)
set fileNames to fileNames & (POSIX path of eachFile)
end addFilePath
Thanks a lot
I don't understand everything --> insert the name of the file into each row of that column | TO DO insert text in each column to the name of eachFile.
Here is the script, Updated :
on open excelFiles
set numFiles to count excelFiles
repeat with eachFile in excelFiles -- open each file in Excel
tell application "Microsoft Excel"
set tBook to open workbook workbook file name (eachFile as string)
set tName to name of tBook
insert into range column 1 of active sheet -- insert column
set lastCell to last cell of used range of active sheet -- get last cell from the used range
set value of range ("A1:A" & first row index of lastCell) of active sheet to tName --set first column's values to the file name
close tBook saving yes
end tell
end repeat
display dialog numFiles
end open
Edit : I forgot the error :
Not enough memory : This weird error seems to be : you call a handler without using my or tell me to) in a tell block application.
Use my like this : set x to my addFilePath(eachFile)
Also, a tell application "Microsoft Excel" block in the application Finder block is not recommended, this can cause unexpected errors.