I am writing an AppleScript for Excel and I can not seem to get rid of this pesky loop no matter what I do.
tell application "Microsoft Excel"
activate
select range "A1"
set FoundResult to false
try
set FoundCell to (find range "A:A" what "")
set FoundResult to true
delete entire row of FoundCell
end try
repeat while FoundResult is true
set FoundResult to false
try
set FoundCell to (find range "A:A" what "")
set FoundResult to true
delete entire row of FoundCell
end try
end repeat
end tell
There is probably a better way to do this. However, the code below should work: I search for the first non-empty row, and if that row has an index of 1 then there was just empty rows below, and the pruning of empty rows in between the area is finished, so I then exit the repeat loop.
There is probably a better way, using areas or something, it may even be written a better technique in Excel Script Reference.
tell application "Microsoft Excel"
activate
select range "A1"
set FoundResult to false
set FoundCell to (find range "A:A" what "")
set FoundResult to true
delete range (entire row of FoundCell) shift shift up
repeat while FoundResult is true
set FoundResult to false
try
set FoundCell to (find range "A:A" what "")
set first_Empty to first row index of FoundCell
delete entire row of FoundCell
set FoundProbe to (find range "A:A" what "*" after FoundCell)
set first_full to first row index of FoundProbe
if first_full < first_Empty then
set FoundResult to false
end if
on error
set FoundResult to false
end try
end repeat
end tell
Edit
The code isn’t ideal, I could have added delete range within the loop, and shift shift up (default behaviour), but it doesn’t seem necessary, so I leave it as it is, or up to any user to polish it.
Sorry I missed the biggest part of my issue. Sorry!
The Excel sheet I have is going to have rows with data and rows without data. I am trying to remove all of the empty rows in between the rows and stop at the end.
The word “Short” will always be in the last row.
If it is so, that “short” is to be found in the last row, then I surmise that “short” is a unique value, and then you can just add the line below after the repeat loop:
I have got to be missing something so obvious it is not even funny here.
Here is the plan I have empty row in an Excel sheet, the last row contains the word “short”
I need it to cycle through and clear out all of the empty rows and stop when it gets to “short”
tell application "Microsoft Excel"
activate
select range "A1"
set FoundResult to false
set FoundCell to (find range "A:A" what "")
set FoundResult to true
delete range (entire row of FoundCell) shift shift up
repeat while FoundResult is true
set FoundCell to (find range "A:A" what "Short")
set FoundResult to false
try
set FoundCell to (find range "A:A" what "")
set first_Empty to first row index of FoundCell
delete entire row of FoundCell
set FoundProbe to (find range "A:A" what "*" after FoundCell)
set first_full to first row index of FoundProbe
if first_full < first_Empty then
set FoundResult to false
end if
on error
set FoundResult to false
end try
end repeat
end tell
I tried that and it cleans one row and stops which is better than the loop I was stuck in but…
tell application "Microsoft Excel"
activate
select range "A1"
set FoundResult to false
set FoundCell to (find range "A:A" what "")
set FoundResult to true
delete range (entire row of FoundCell) shift shift up
repeat while FoundResult is true
set FoundResult to false
try
set FoundCell to (find range "A:A" what "")
set first_Empty to first row index of FoundCell
delete entire row of FoundCell
set FoundProbe to (find range "A:A" what "*" after FoundCell)
set first_full to first row index of FoundProbe
if first_full < first_Empty then
set FoundResult to false
end if
on error
set FoundResult to false
end try
end repeat
set FoundCell to (find range "A:A" what "Short")
end tell