Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default relating to named range in vba

Hi I have found a variation on some code and it works fine when i set the
targ values below as numbers.

I cant get it to refer to the cells which are provided as named ranges.

You can see below my efforts in the code below.

Please can someone give me the correct code to make it work

Thanks for your help.
Rob

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Error

Dim icolour As Integer

Dim targ1 As Long
Dim targ2 As Long
Dim targ3 As Long
Dim targ4 As Long
Dim targ5 As Long
Dim targ6 As Long
Dim targ7 As Long
Dim targ8 As Long

targ1 = ''''
targ2 = ''''
targ3 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2a")).Va lue
targ4 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2b")).Va lue
targ5 = NaburnMLSSTrig3a
targ6 = NaburnMLSSTrig3b
targ7 = ''''
targ8 = ''''

'EXAMPLE FROM POST
'Range(ActiveWorkbook.Names("NaburnMLSSTrig1a")).V alue

If Not Intersect(Target, Range("Y:AB")) Is Nothing Then
Select Case Target
Case targ1 To targ2
icolour = 45
Case targ3 To targ4
icolour = 3
Case targ5 To targ6
icolour = 45
Case targ7 To targ8
icolour = 3
Case Else
ActiveCell.Interior.ColorIndex = xlColorIndexNone
End Select
Target.Interior.ColorIndex = icolour
End If



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default relating to named range in vba

I am not sure what your exact problem is, but all you targ variables are
dim'd as LONG, so the only thing they can hold is a whole number. If you
want to put in a string as your code, shows, type them as string or variant
(use variant if they could hold a string or a number).

--
Regards,
Tom Ogilvy

"Rob Hargreaves" wrote in message
...
Hi I have found a variation on some code and it works fine when i set the
targ values below as numbers.

I cant get it to refer to the cells which are provided as named ranges.

You can see below my efforts in the code below.

Please can someone give me the correct code to make it work

Thanks for your help.
Rob

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Error

Dim icolour As Integer

Dim targ1 As Long
Dim targ2 As Long
Dim targ3 As Long
Dim targ4 As Long
Dim targ5 As Long
Dim targ6 As Long
Dim targ7 As Long
Dim targ8 As Long

targ1 = ''''
targ2 = ''''
targ3 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2a")).Va lue
targ4 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2b")).Va lue
targ5 = NaburnMLSSTrig3a
targ6 = NaburnMLSSTrig3b
targ7 = ''''
targ8 = ''''

'EXAMPLE FROM POST
'Range(ActiveWorkbook.Names("NaburnMLSSTrig1a")).V alue

If Not Intersect(Target, Range("Y:AB")) Is Nothing Then
Select Case Target
Case targ1 To targ2
icolour = 45
Case targ3 To targ4
icolour = 3
Case targ5 To targ6
icolour = 45
Case targ7 To targ8
icolour = 3
Case Else
ActiveCell.Interior.ColorIndex = xlColorIndexNone
End Select
Target.Interior.ColorIndex = icolour
End If





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default relating to named range in vba

Rob,
Assuming NaburnMLSSTrig3a is a named range then:

targ5 = Range("NaburnMLSSTrig3a")

Similarly fot targ6 (and targ3/targ4 could be defined in same way).
should work.

Normal format is MyVar=Range("RangeName")

HTH

"Rob Hargreaves" wrote:

Hi I have found a variation on some code and it works fine when i set the
targ values below as numbers.

I cant get it to refer to the cells which are provided as named ranges.

You can see below my efforts in the code below.

Please can someone give me the correct code to make it work

Thanks for your help.
Rob

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Error

Dim icolour As Integer

Dim targ1 As Long
Dim targ2 As Long
Dim targ3 As Long
Dim targ4 As Long
Dim targ5 As Long
Dim targ6 As Long
Dim targ7 As Long
Dim targ8 As Long

targ1 = ''''
targ2 = ''''
targ3 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2a")).Va lue
targ4 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2b")).Va lue
targ5 = NaburnMLSSTrig3a
targ6 = NaburnMLSSTrig3b
targ7 = ''''
targ8 = ''''

'EXAMPLE FROM POST
'Range(ActiveWorkbook.Names("NaburnMLSSTrig1a")).V alue

If Not Intersect(Target, Range("Y:AB")) Is Nothing Then
Select Case Target
Case targ1 To targ2
icolour = 45
Case targ3 To targ4
icolour = 3
Case targ5 To targ6
icolour = 45
Case targ7 To targ8
icolour = 3
Case Else
ActiveCell.Interior.ColorIndex = xlColorIndexNone
End Select
Target.Interior.ColorIndex = icolour
End If




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default relating to named range in vba

If I understand this, 1 additional problem might be this...

If Target is blank, you want this:

Case targ7 To targ8
icolour = 3


But it will never get there because blanks are tested earlier.
Case targ1 To targ2
icolour = 45


I'm not sure what you want to do with Blanks, but here's another version:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim icolour
Select Case Target
Case vbEmpty
icolour = 45 ' Or 3 ??
Case Range("NaburnMLSSTrig2a") To Range("NaburnMLSSTrig2b")
icolour = 3
Case Range("NaburnMLSSTrig3a") To Range("NaburnMLSSTrig3b")
icolour = 45
Case Else
icolour = xlColorIndexNone
End Select
Target.Interior.ColorIndex = icolour
End Sub


--
Dana DeLouis
Win XP & Office 2003


"Rob Hargreaves" wrote in message
...
Hi I have found a variation on some code and it works fine when i set the
targ values below as numbers.

I cant get it to refer to the cells which are provided as named ranges.

You can see below my efforts in the code below.

Please can someone give me the correct code to make it work

Thanks for your help.
Rob

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Error

Dim icolour As Integer

Dim targ1 As Long
Dim targ2 As Long
Dim targ3 As Long
Dim targ4 As Long
Dim targ5 As Long
Dim targ6 As Long
Dim targ7 As Long
Dim targ8 As Long

targ1 = ''''
targ2 = ''''
targ3 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2a")).Va lue
targ4 = Range(ActiveWorkbook.Names("NaburnMLSSTrig2b")).Va lue
targ5 = NaburnMLSSTrig3a
targ6 = NaburnMLSSTrig3b
targ7 = ''''
targ8 = ''''

'EXAMPLE FROM POST
'Range(ActiveWorkbook.Names("NaburnMLSSTrig1a")).V alue

If Not Intersect(Target, Range("Y:AB")) Is Nothing Then
Select Case Target
Case targ1 To targ2
icolour = 45
Case targ3 To targ4
icolour = 3
Case targ5 To targ6
icolour = 45
Case targ7 To targ8
icolour = 3
Case Else
ActiveCell.Interior.ColorIndex = xlColorIndexNone
End Select
Target.Interior.ColorIndex = icolour
End If





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
Question relating to Named Ranges which exist in another workbook. Pank Excel Discussion (Misc queries) 2 February 5th 07 03:17 PM
Array as a "named range" - formula ok in cells, but error as "named range" tskogstrom Excel Discussion (Misc queries) 11 December 28th 06 04:44 PM
inserting a named range into new cells based on a named cell Peter S. Excel Discussion (Misc queries) 1 June 4th 06 03:53 AM
Problem with an "if" relating to a range Anthony Excel Worksheet Functions 7 July 10th 05 05:46 AM
A question relating to "for each cell_in_loop in range" David F. Schrader Excel Programming 3 March 2nd 05 04:38 PM


All times are GMT +1. The time now is 12:16 PM.

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"