Thread: [Solved] Run Macro on Cell Lost Focus
View Single Post
 
Old 04-03-2017, 04:31 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Word doesn't have the kind of change events Excel has, so your options are limited. Although Word does have a WindowSelectionChange event, instantiating it requires the creation of an event handler and class module for the code and, even then, would only work in your situation if you were select something in a table after typing whatever you want to the macro to work on. Still, if you're interested in going down that path, see:
http://wordmvp.com/FAQs/MacrosVBA/In...tSavePrint.htm
http://wordmvp.com/FAQs/MacrosVBA/AppClassEvents.htm

The 'event' macro might then be coded along the lines of:
Code:
Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
Application.ScreenUpdating = False
If Sel.Information(wdWithInTable) <> True Then Exit Sub
Dim tCell As Word.Cell, tRange As Range
For Each tCell In Selection.Cells
  Set tRange = tCell.Range
  tRange.End = tRange.End - 1
  With tRange
    If IsNumeric(.Text) Then
      .Text = FormatCurrency(Expression:=.Text)
    End If
  End With
Next tCell
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote