Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Max Max is offline
external usenet poster
 
Posts: 390
Default Code in certain sheets

Hello
I have a file with 33 sheets and a VB code (below) in "This Workbook". The
problem is that I don't want the first and last sheets obey this code. How
can I fix this problem?
This is code:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Application.Intersect(Target, Range("A7:A68")) Is Nothing Then
If IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target = Left(Format(Target.Value, "0000"), 2) & ":" & _
Right(Format(Target.Value, "0000"), 2)
Application.EnableEvents = True
End If
End If
If Not Application.Intersect(Target, Range("G7:G64")) Is Nothing Then
If IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target = Left(Format(Target.Value, "0000"), 0) & "P." & _
Right(Format(Target.Value, "0000"), 2)
Application.EnableEvents = True
End If
End If
End Sub

Thanks in advance.
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Code in certain sheets

You could use the name of the sheets:

if lcase(sh.name) = lcase("firstsheet") _
or lcase(sh.name) = lcase("lastsheet") then
exit sub
end if

Or you could use the .index property

if sh.index = 1 _
or sh.index = sh.parent.sheets.count then
exit sub
end if

This adds some basic checks:

Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Sh.Index = 1 _
Or Sh.Index = Sh.Parent.Sheets.Count Then
Exit Sub
End If

If Target.Cells.Count 1 Then
Exit Sub
End If

If Not Application.Intersect(Target, Sh.Range("A7:A68")) Is Nothing Then
If IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target = Left(Format(Target.Value, "0000"), 2) & ":" & _
Right(Format(Target.Value, "0000"), 2)
Application.EnableEvents = True
End If
Else
If Not Application.Intersect(Target, Sh.Range("G7:G64")) Is Nothing Then
If IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target = Left(Format(Target.Value, "0000"), 0) & "P." & _
Right(Format(Target.Value, "0000"), 2)
Application.EnableEvents = True
End If
End If
End If
End Sub


MAX wrote:

Hello
I have a file with 33 sheets and a VB code (below) in "This Workbook". The
problem is that I don't want the first and last sheets obey this code. How
can I fix this problem?
This is code:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Application.Intersect(Target, Range("A7:A68")) Is Nothing Then
If IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target = Left(Format(Target.Value, "0000"), 2) & ":" & _
Right(Format(Target.Value, "0000"), 2)
Application.EnableEvents = True
End If
End If
If Not Application.Intersect(Target, Range("G7:G64")) Is Nothing Then
If IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target = Left(Format(Target.Value, "0000"), 0) & "P." & _
Right(Format(Target.Value, "0000"), 2)
Application.EnableEvents = True
End If
End If
End Sub

Thanks in advance.


--

Dave Peterson
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
sum across range of sheets for a cost code monkeydoom Excel Worksheet Functions 1 August 25th 09 03:29 PM
Code to hide sheets Tia[_3_] Excel Worksheet Functions 0 July 8th 08 11:07 AM
VBA Code to Add the same header from my first sheet into 20 sheets [email protected] Excel Worksheet Functions 4 December 15th 06 02:13 AM
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Excel Worksheet Functions 6 March 29th 06 12:43 PM
Formating Sheets made in Code Jenn Excel Discussion (Misc queries) 1 August 22nd 05 05:19 PM


All times are GMT +1. The time now is 02:45 AM.

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

About Us

"It's about Microsoft Excel"