LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
al al is offline
external usenet poster
 
Posts: 363
Default Add VBA code to multiple WorkSheets

The following code works for one worksheet but when I try to use it for
multiple worksheets, I get the error message: Excel has created errors,
Windows will shut the program down. A log file has been created. Must I do
something special when adding code to multiple worksheets? Thanks in advance.

Sub addvba()
Dim s As String
Dim DEPT2 As String
Dim dept As Integer

Application.ScreenUpdating = False

For dept = 9 To 10

Select Case dept
Case 9
DEPT2 = "ALL"
Case 10
DEPT2 = "OTHER"
End Select


Call addactivate(DEPT2)

Next dept
End Sub


Sub addactivate(DEPT2 As String)
Dim StartLine As Long
Dim s As String
Dim LINENUM As Integer

s = ActiveWorkbook.Worksheets(DEPT2).CodeName

With ActiveWorkbook.VBProject.VBComponents(s).CodeModul e
StartLine = .CreateEventProc("Activate", "Worksheet")

LINENUM = .CountOfLines
Debug.Print LINENUM
.InsertLines LINENUM - 1, _
"If lastAddress < """" Then Range(lastAddress).Select" & Chr(13)

LINENUM = 6
.InsertLines LINENUM, _
"Private Sub Worksheet_SelectionChange(ByVal Target As Range)" & Chr(13)
& _
"lastAddress = Target.Address" & Chr(13) & _
"End Sub"

End With

End Sub


 
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
Code for multiple worksheets Paul S Excel Discussion (Misc queries) 1 June 17th 08 12:50 PM
Delete Blank Rows Code - Multiple Worksheets - Multiple Documents BenS Excel Discussion (Misc queries) 3 June 29th 07 12:20 AM
Need help on code - Macro on multiple worksheets J@Y Excel Discussion (Misc queries) 2 February 2nd 07 08:05 PM
Run code for multiple worksheets in a workbook & Column Formatting Shaun[_3_] Excel Programming 1 January 7th 04 08:35 PM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM


All times are GMT +1. The time now is 04:31 PM.

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"