Convert Excel Macro to Applescript to work with Numbers

I have an excel spreadsheet that uses macros. I am hoping to convert that spreadsheet to Numbers but I am not familiar with Applescript.


Is it possible to convert macros code to Applescript?


If so can anyone help convert the following codes:

1.

Sub Clear_Total_Plays()

' Create a loop to check each Total Play cell

' and reset their values to zero

Dim i As Integer

For i = 2 To 28

Cells(i, 9).Select

ActiveCell.Value = 0

Next i

Range("a1").Select

End Sub


2.

Sub Reset_Plays1()

' Reset_Plays1 Macro

' Resets Play Counter to zero.

Range("Table3[[#All],[Column2]]").Select

ActiveCell.FormulaR1C1 = "0"

Range("a1").Select

End Sub


3.

Sub Add_1_Play()

'

' Add_1_Play Macro

' Adds 1 Play to the Play counter.

'



'

' Create a loop to check each Total Play cell

' and increment their value by one if a player

' was in the current play



'Create an index to use in a for loop

Dim i As Integer

Dim j As Integer

'Create an array to store whether a player

'has had multiple plays in a row

Dim InARow(2 To 28) As Boolean

For i = 2 To 28

Cells(i, 9).Select

CurrentTotal = ActiveCell.Value

'If the boolean value is true

If Cells(i, 11) = True Then

'then increment the Total Plays by one

ActiveCell.Value = CurrentTotal + 1

InARow(i) = True

Else

'otherwise leave Total Plays alone

ActiveCell.Value = CurrentTotal

End If

Next i

For j = 2 To 28

Cells(j, 8).Select

'If player has played in the last play

If InARow(j) = True Then

'Increment the corresponding cell in the

'In a Row Column by 1

InARowValue = ActiveCell.Value

ActiveCell.Value = InARowValue + 1

Else

'Reset cell to zero and reset

'InARow boolean array index to false

Cells(j, 8).Select

ActiveCell.Value = 0

InARow(j) = False

End If

Next j

' Increment Total Plays by One

Range("Table3[[#All],[Column2]]").Select

PlayCount = ActiveCell.Value

ActiveCell.FormulaR1C1 = PlayCount + 1

Range("a1").Select

End Sub


Thank you

MacBook Pro (Retina, Mid 2012)

Posted on Oct 16, 2016 10:12 AM

Reply
12 replies

Oct 17, 2016 7:10 PM in response to SGIII

Thanks for the reply.


Here is a picture of the excel spreadsheet. The Numbers spreadsheet is set up identically.


User uploaded file

code 1 from above is tied to clear total play button. When pressed the total plays column is reset to 0.


Code 2 from above is tied to reset play count button. When pressed the number in C13 is reset to 0.


Code 3 from above is tied to the snap button. When pressed the code searches through column K. If the value in column K is "true" it adds one to total plays column and the in a row column. If the value in column K is "false" it resets the value in the in a row column back to 0.


Column K is the text version of the checkboxes in column E.

Oct 18, 2016 8:21 AM in response to CoachMac3

Incrementing the values via a script similar to the VBA should be straightforward.


However, Numbers has no "buttons" to which scripts can be attached and activated by clicking.


The scripts would have to be activated by a menu pick and/or keyboard shortcut.


Is there a reason you aren't using formulas to do the incrementing? Are there any formulas in the table?


SG

Oct 18, 2016 8:41 PM in response to SGIII

"Is there a reason you aren't using formulas to do the incrementing?"


Hi SG,


This is similar to a recent request on much the same topic (and possibly from the same person).


The purpose of the table is to track the number of consecutive plays and total number of plays of a football game during which each player was on the field.

The checkboxes in column E are checked when a player enters the field and unchecked when that player leaves the field.


The small block of cells B2-C5 counts the number of checked boxes, and is likely assigned a conditional formatting rule that colours the cell fill when too few or too many players are on the field. Two of C3, C4 and C5 will be determined by COUNTIF or COUNTA, the third by an addition statement or a subtraction statement.


The play count in C13 is incremented by one each time the Snap button is clicked. It is reset to zero only when the Reset Play count button in column M is clicked.


The cells in column H are incremented by one each time the Snap button is clicked IF the checkbox on that row is checked. They are reset to zero if the associated checkbox is not checked.


The cells in column I are incremented by one each time the Snap button is clicked IF the checkbox on that row is checked. They remain at the current value IF the checkbox is unchecked. They are reset to zero only when the Clear Total Plays button is clicked.



Formulas have no memory—they act on the current state of the document, and without regard to the previous state, so they cannot offer an option of "leave as is" which would require a circular reference.


MS Excel Macros and Applescripts do have memory—both can get a value, store it, then use that value n determining the 'new' value for a cell. That memory is required in the tasks to be done here, so a formula does not seem to offer the possibility of a solution. It requires a script.


Regards,

Barry

Oct 19, 2016 8:09 AM in response to Barry

Thanks for stating the issue with formulas. I am the poster of the other post. Figured as I am more familiar with excel I would see if I could find a way to make it work with excel and then see whether or not macros could be converted to applescript. It's unfortunate Applescript and numbers don't allow buttons, as the excel spreadsheet with macros does not work on an iPad. Guess I will keep searching for a solution that works best for the situation.


Thanks for the help.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Convert Excel Macro to Applescript to work with Numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.