This seems to work:
Sub InsertCode()
Dim wb As Workbook
Dim code As String, checkline As String
Dim Ln As Long
checkline = "Workbook_SheetSelectionChange"
code = "Private Sub Workbook_SheetSelectionChange" & _
"(ByVal Sh As Object, ByVal Target As Range)" & vbCr & _
"Application.SendKeys ""{F9}""" & vbCr & _
"End Sub"
On Error Resume Next 'In case project is protected
For Each wb In Workbooks
With wb.VBProject.VBComponents("ThisWorkbook").CodeModu le
Ln = .CountOfLines
'Don't add code if already exists
If Not .Find(checkline, 1, 1, Ln, 1) Then _
..InsertLines Ln + 1, code
End With
Next
On Error GoTo 0
End Sub
Regards,
Greg
"Amolin" wrote:
How to use VBA to copy this code from one opened workbook to all opened
workbooks ? I need this code under "Thisworkbook" object of each opened
workbook. Thank you!
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal
Target As Range)
Application.SendKeys "{F9}"
End Sub
--
Amolin
------------------------------------------------------------------------
Amolin's Profile: http://www.excelforum.com/member.php...fo&userid=8321
View this thread: http://www.excelforum.com/showthread...hreadid=375868