Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Numbers reference cell keep formatting

Hi - I'm looking for a way to keep the formatting of a reference cell when it is applied to another cell. For example, if cell A1 has a background color, and the font is bold, then if I use the formula =A1 in another cell it should also be that background color / bold.


In excel you can do this with a VBA script something like this:

Private Sub Worksheet_Activate()
'UpdatebyExtendoffice20101024
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Or Target.Value = "" Then Exit Sub
    Application.EnableEvents = False
    If Not Intersect(Target, Range("A1")) Is Nothing Then              'Range("A1") the reference cell
        Target.Copy
        ActiveWorkbook.Sheets("Sheet2").Range("B1").PasteSpecial xlPasteAllUsingSourceTheme
                    'Range("B1")the cell linked to reference cell,ActiveWorkbook.Sheets("Sheet2")the sheet which contains linked cell
        Application.CutCopyMode = False
        Target.Select
    End If
    Application.EnableEvents = True
End Sub


Can I do something similar using AppleScript in numbers? I'm not a programmer at all so would need a specific formula to cut/paste- thanks!


iMac 27″ 5K, macOS 10.15

Posted on Jun 21, 2022 11:31 AM

Reply
Question marked as Best reply

Posted on Jun 21, 2022 7:51 PM

A more or less direct equivalent to your VBA is something like this in AppleScript.


tell application "Numbers"
	tell front document's active sheet
		tell (first table whose class of selection range is range)
			set tblName to its name
			my copyStyle("A2", tblName)
			delay 1 -- can vary; gives interface time to catch up
			my pasteStyle("B2", tblName)
		end tell
	end tell
end tell

to copyStyle(cellName, tblName)
	tell application "Numbers"
		tell front document's active sheet
			tell table tblName to set selection range to range cellName
			tell application "Numbers" to activate
			tell application "System Events" to keystroke "c" using {option down, command down}
		end tell
	end tell
end copyStyle

to pasteStyle(cellName, tblName)
	tell application "Numbers"
		tell front document's active sheet
			tell table tblName to set selection range to range cellName
			tell application "Numbers" to activate
			tell application "System Events" to keystroke "v" using {option down, command down}
		end tell
	end tell
end pasteStyle



  1. Copy-paste into Script Editor (in Applications > Utility)
  2. The first time make sure Script Editor is listed and selected at System Preferences > Security & Privacy > Privacy > Accessibility.
  3. Change the cell references to match your setup
  4. Click somewhere in the table, and the click the triangle 'run' button in Script Editor.



Note that this simply mimics the Copy Style and Paste Style in the Format menu. These are similar to the "paintbrush" in Excel. You can also add their equivalents to the Toolbar. So I tend to just use that easy-to-access built-in functionality, rather than a script.


SG





SG

Similar questions

2 replies
Question marked as Best reply

Jun 21, 2022 7:51 PM in response to larzalere

A more or less direct equivalent to your VBA is something like this in AppleScript.


tell application "Numbers"
	tell front document's active sheet
		tell (first table whose class of selection range is range)
			set tblName to its name
			my copyStyle("A2", tblName)
			delay 1 -- can vary; gives interface time to catch up
			my pasteStyle("B2", tblName)
		end tell
	end tell
end tell

to copyStyle(cellName, tblName)
	tell application "Numbers"
		tell front document's active sheet
			tell table tblName to set selection range to range cellName
			tell application "Numbers" to activate
			tell application "System Events" to keystroke "c" using {option down, command down}
		end tell
	end tell
end copyStyle

to pasteStyle(cellName, tblName)
	tell application "Numbers"
		tell front document's active sheet
			tell table tblName to set selection range to range cellName
			tell application "Numbers" to activate
			tell application "System Events" to keystroke "v" using {option down, command down}
		end tell
	end tell
end pasteStyle



  1. Copy-paste into Script Editor (in Applications > Utility)
  2. The first time make sure Script Editor is listed and selected at System Preferences > Security & Privacy > Privacy > Accessibility.
  3. Change the cell references to match your setup
  4. Click somewhere in the table, and the click the triangle 'run' button in Script Editor.



Note that this simply mimics the Copy Style and Paste Style in the Format menu. These are similar to the "paintbrush" in Excel. You can also add their equivalents to the Toolbar. So I tend to just use that easy-to-access built-in functionality, rather than a script.


SG





SG

Numbers reference cell keep formatting

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