code_junkie

Making Excel functions affect 'other' cells

Nov 11th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. Public Function Bar(r As Range) As Integer
  2. If r.Value = 2 Then
  3. Bar = 0
  4. Else
  5. Bar = 128
  6. End If
  7. End Function
  8.  
  9. def safeDivide(numerator, cellRange):
  10. if not isinstance(cellRange, CellRange):
  11. raise ValueError('denominator must be a cell range')
  12. denominator = cellRange.Value
  13. if denominator == 0:
  14. cell = cellRange.TopLeft
  15. cell.BackColor = Color.Red
  16. cell.Offset(1, 0).Value = 'Tried to divide by zero'
  17. return 0
  18. return numerator / denominator
  19.  
  20. Option Explicit
  21.  
  22. Declare Function SetTimer Lib "user32" (ByVal HWnd As Long, _
  23. ByVal IDEvent As Long, ByVal mSec As Long, _
  24. ByVal CallFunc As Long) As Long
  25.  
  26. Declare Function KillTimer Lib "user32" (ByVal HWnd As Long, _
  27. ByVal timerId As Long) As Long
  28.  
  29. Private timerId As Long
  30.  
  31. Private wb As Workbook
  32. Private rangeName As String
  33. Private blnFinished As Boolean
  34.  
  35. Public Sub RunTimer()
  36.  
  37. timerId = SetTimer(0, 0, 10, AddressOf Woohoo)
  38.  
  39.  
  40. End Sub
  41.  
  42.  
  43. Public Sub Woohoo()
  44.  
  45. Dim i As Integer
  46.  
  47. ' For i = 0 To ThisWorkbook.Names.Count - 1
  48. ' ThisWorkbook.Names(i).Delete
  49. ' Next
  50.  
  51. ThisWorkbook.Worksheets("Sheet1").Range("D8").Value = "Woohoo"
  52.  
  53. KillTimer 0, timerId
  54.  
  55. End Sub
  56.  
  57. Dim busy As Boolean
  58. Private Sub Worksheet_Change(ByVal Target As Range)
  59. If busy Then Exit Sub
  60. busy = True
  61. If Target.Row <= 10 And Target.Column <= 10 Then
  62. With Target.Offset(0, 1)
  63. If IsNumeric(Target) Then
  64. .Value = Target * 4
  65. .Interior.Color = RGB(212, 212, 255)
  66. Else
  67. .Value = Empty
  68. .Interior.ColorIndex = xlColorIndexNone
  69. End If
  70. End With
  71. End If
  72. busy = False
  73. End Sub
Add Comment
Please, Sign In to add comment