Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to use VBA to copy this code to "thisworkbook" objects of all opened workbooks ?


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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default How to use VBA to copy this code to "thisworkbook" objects of all

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to use VBA to copy this code to "thisworkbook" objects of all opened workbooks ?


Thank you for your code!

But there is a error says: "Application define error or Object define
error"

"With wb.VBProject.VBComponents("ThisWorkbook").CodeModu le"


could you give a help?


--
Amolin
------------------------------------------------------------------------
Amolin's Profile: http://www.excelforum.com/member.php...fo&userid=8321
View this thread: http://www.excelforum.com/showthread...hreadid=375868

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default How to use VBA to copy this code to "thisworkbook" objects of all

Greg Wilson shared this with us in microsoft.public.excel.programming:

This seems to work:


*snip* code containing VBComponents.CodeModule.InsertLines

And if you have a McAfee virusscanner, it will cheerfully erase any
code module that contains .InsertLines.

--
Amedee Van Gasse using XanaNews 1.17.4.1
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to use VBA to copy this code to "thisworkbook" objects of all opened workbooks ?


Thank you, I have Symantec Anti _Virus installed

--
Amoli
-----------------------------------------------------------------------
Amolin's Profile: http://www.excelforum.com/member.php...nfo&userid=832
View this thread: http://www.excelforum.com/showthread.php?threadid=37586



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default How to use VBA to copy this code to "thisworkbook" objects of all opened workbooks ?

Amolin shared this with us in microsoft.public.excel.programming:


Thank you, I have Symantec Anti _Virus installed.


Which is no guarantee. I experienced the problem with McAfee, but other
AV may do the same. Perhaps not now but who knows about future versions?

The problem is with .InsertLines (and also with .DeleteLines). Some
macro viruses use this to add their own code to other workbooks. So a
proactive AV may not know the virus yet, but it sees the .InsertLines
activity and it say, better safe than sorry.

If you have a standalone AV, you can perhaps disable this particular
scanning feature. With a managed or network scanner: don't know,
perhaps not.

In My Humble Opinion it is important that not only you know there can
be a problem, but also WHY there can be a problem. That way you can
work around it.

--
Amedee Van Gasse using XanaNews 1.17.4.1
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with Linking workbooks via "copy" and "paste link" Arkitek Excel Discussion (Misc queries) 0 December 19th 06 10:03 PM
Copying "ThisWorkbook" objects rpgun[_4_] Excel Programming 0 October 26th 04 12:35 PM
Copying "ThisWorkbook" objects rpgun[_3_] Excel Programming 1 October 25th 04 09:57 PM
Can you "duplicate" "copy" listboxes and code to multiple cells? HotRod Excel Programming 1 September 1st 04 05:03 PM


All times are GMT +1. The time now is 08:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"