MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 18th, 2002, 09:27 AM   #1
Richie(UK)
MrExcel MVP
 
Richie(UK)'s Avatar
 
Join Date: May 2002
Location: UK
Posts: 3,333
Default

Hi,

I'm relatively ne to VBA and am having difficulty with a routine containing defined ranges.

I have 'cut & pasted' a routine from the net for identifying which range contains the activecell. I then want a separate sub-routine to be called for each range. Within this sub-routine I want the activecell to be coloured and the other cells on the same row to be non-coloured.

The problem is that the VBE keeps telling me that the 'Range' variable isn't defined but I thought it was.

The relevant section of code is shown below. Any suggestions / comments would be gratefully received.

Option Explicit
'
' Function to determine if a cell is within a certain range
'
Function InRange(Range1 As Range, Range2 As Range) As Boolean
' returns True if Range1 is within Range2
Dim InterSectRange As Range
Set InterSectRange = Application.Intersect(Range1, Range2)
InRange = Not InterSectRange Is Nothing
Set InterSectRange = Nothing
End Function
'
' Detect change of active cell and action appropriate macro
'
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
'
TestInRange
'
End Sub
'
' Test whether cell is in defined range
'
Sub TestInRange()
'
Dim Q1to10 As Range, Q11to20 As Range
Dim Q21to30 As Range, Q31to40 As Range
Set Q1to10 = Range("B5", "E14")
Set Q11to20 = Range("B15", "E24")
Set Q21to30 = Range("B25", "E34")
Set Q31to40 = Range("B35", "E44")
'
If InRange(ActiveCell, Q1to10) Then
' code to handle when the active cell is within the right range
QBank1to10
End If
If InRange(ActiveCell, Q11to20) Then
QBank11to20
End If
If InRange(ActiveCell, Q21to30) Then
QBank21to30
End If
If InRange(ActiveCell, Q31to40) Then
QBank31to40
End If
End Sub
'
Sub QBank1to10()
'*************** Questions 1 to 10 ***************
'Question 1
If Target.Address = "$B$5" Then
Range("B5", "E5").Interior.ColorIndex = 0
Range("B5").Interior.ColorIndex = 8
Range("G5").Value = 1
Range("I5").Value = ""
Range("K5").Value = ""
Range("M5").Value = ""
End If
If Target.Address = "$C$5" Then
Range("B5", "E5").Interior.ColorIndex = 0
Range("C5").Interior.ColorIndex = 8
Range("G5").Value = ""
Range("I5").Value = 1
Range("K5").Value = ""
Range("M5").Value = ""
End If
If Target.Address = "$D$5" Then
Range("B5", "E5").Interior.ColorIndex = 0
Range("D5").Interior.ColorIndex = 8
Range("G5").Value = ""
Range("I5").Value = ""
Range("K5").Value = 1
Range("M5").Value = ""
End If
If Target.Address = "$E$5" Then
Range("B5", "E5").Interior.ColorIndex = 0
Range("E5").Interior.ColorIndex = 8
Range("G5").Value = ""
Range("I5").Value = ""
Range("K5").Value = ""
Range("M5").Value = 1
End If
[Then Questions 2 to 10 are similar to Q1]
End Sub
Richie(UK) is offline   Reply With Quote
Old May 18th, 2002, 09:51 AM   #2
mcleve
New Member
 
Join Date: May 2002
Posts: 25
Default

Im pretty new to VBA actually very new to VBA but I think that you forgot to add Quotation marks to:
Range("G5").Value = 1
Range("I5").Value = 1
Range("K5").Value = 1
Range("M5").Value = 1
change to:
Range("G5").Value = "1"
Range("I5").Value = "1"
Range("K5").Value = "1"
Range("M5").Value = "1"
Ithink the colorindex values need quotation marks also.
hope this helps Post and let me know

[ This Message was edited by: mcleve on 2002-05-18 09:15 ]
mcleve is offline   Reply With Quote
Old May 18th, 2002, 10:34 AM   #3
Richie(UK)
MrExcel MVP
 
Richie(UK)'s Avatar
 
Join Date: May 2002
Location: UK
Posts: 3,333
Default

Thanks for the input but that's not it. The 1 works fine on it's own, and the blank set of quotes are to clear the other cells.

The problem occurs further up the code at the 'Target.Address' line.
Richie(UK) is offline   Reply With Quote
Old May 18th, 2002, 11:18 AM   #4
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Just guessing here...

Set Q1to10 = Range("B5", "E14")
Set Q11to20 = Range("B15", "E24")
Set Q21to30 = Range("B25", "E34")
Set Q31to40 = Range("B35", "E44")

Should be...

Set Q1to10 = Range("B5:E14")
Set Q11to20 = Range("B15:E24")
Set Q21to30 = Range("B25:E34")
Set Q31to40 = Range("B35:E44")

Tom
Tom Schreiner is offline   Reply With Quote
Old May 18th, 2002, 12:36 PM   #5
Nimrod
MrExcel MVP
 
Join Date: Apr 2002
Location: Vancouver BC , Canada
Posts: 6,259
Default

Hi Mcleve:
Just a little tip on the use of quotes around numbers. The quotes are only used if you want to have the number evaluated as text and not a number.
__________________

<MARQUEE>...........Never be afraid to try something new. Remember, amateurs built the ark, professionals built the Titanic...............The easiest thing to find is fault, don't be easy !.. --Anonymous--...</marquee>
Nimrod is offline   Reply With Quote
Old May 18th, 2002, 01:03 PM   #6
Richie(UK)
MrExcel MVP
 
Richie(UK)'s Avatar
 
Join Date: May 2002
Location: UK
Posts: 3,333
Default

Hi TsTom,

Afraid that didn't do it either. Any other ideas? (The problem area is the 'Target.Address' reference)
Richie(UK) is offline   Reply With Quote
Old May 18th, 2002, 01:32 PM   #7
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Target.Address is defined inside a procedure.
The scope of this variable is then limited to the procedure Worksheet_SelectionChange.
You will need to define a variable at the module level and assign the target range to this variable. You can then use this new variable in any procedure located within this module.
See '##################

Example:



Option Explicit
Dim SaveTarget as Range '##################
' Function to determine if a cell is within a certain range
'
Function InRange(Range1 As Range, Range2 As Range) As Boolean
' returns True if Range1 is within Range2
Dim InterSectRange As Range
Set InterSectRange = Application.Intersect(Range1, Range2)
InRange = Not InterSectRange Is Nothing
Set InterSectRange = Nothing
End Function
'
' Detect change of active cell and action appropriate macro
'
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
SaveTarget = Target '#######################
TestInRange
'
End Sub
'
' Test whether cell is in defined range
'
Sub TestInRange()
'
Dim Q1to10 As Range, Q11to20 As Range
Dim Q21to30 As Range, Q31to40 As Range
Set Q1to10 = Range("B5", "E14")
Set Q11to20 = Range("B15", "E24")
Set Q21to30 = Range("B25", "E34")
Set Q31to40 = Range("B35", "E44")
'
If InRange(ActiveCell, Q1to10) Then
' code to handle when the active cell is within the right range
QBank1to10
End If
If InRange(ActiveCell, Q11to20) Then
QBank11to20
End If
If InRange(ActiveCell, Q21to30) Then
QBank21to30
End If
If InRange(ActiveCell, Q31to40) Then
QBank31to40
End If
End Sub
'
Sub QBank1to10()
'*************** Questions 1 to 10 ***************
'Question 1
'##################
If SaveTarget.Address = "$B$5" Then
Range("B5", "E5").Interior.ColorIndex = 0
Range("B5").Interior.ColorIndex = 8
Range("G5").Value = 1
Range("I5").Value = ""
Range("K5").Value = ""
Range("M5").Value = ""
End If



Tom

Tom Schreiner is offline   Reply With Quote
Old May 18th, 2002, 01:35 PM   #8
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

If
If Target.Address = "$B$5" Then
is causing difficulty try changing it to:

If Not Application.Intersect(Target, [b5]) Is Nothing Then

__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 18th, 2002, 01:50 PM   #9
Richie(UK)
MrExcel MVP
 
Richie(UK)'s Avatar
 
Join Date: May 2002
Location: UK
Posts: 3,333
Default

Tom,

Top Man! That did the trick. Thanks for your help.

Richie
Richie(UK) is offline   Reply With Quote
Old May 18th, 2002, 01:50 PM   #10
Nimrod
MrExcel MVP
 
Join Date: Apr 2002
Location: Vancouver BC , Canada
Posts: 6,259
Default

Hi Guys:
I'm looking at the code and see alot of redundancy instead of using the power of variables... Anyway does this code do what you trying to achieve ?
If not maybe with a bit more detail I can modify this code and help ?

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

'make sure active cell between column B & E
If Target.Column > 1 And Target.Column < 5 And IsNumeric(Target.Value) Then
         RowNum = Target.Row
         ColNum = Target.Column
         
        ' Clear cells B-E  of color
        Range(Cells(RowNum, 2), Cells(RowNum, 5)).Interior.ColorIndex = 0
        
        ' Clear Values in G,I,K,M
        Range("G" & RowNum).Value = ""
        Range("I" & RowNum).Value = ""
        Range("K" & RowNum).Value = ""
        Range("M" & RowNum).Value = ""
        
        ' Set color in one cell
        Cells(RowNum, ColNum).Interior.ColorIndex = 8
        
        'Set offset cell value to 1
        Cells(RowNum, ColNum + 6).Value = 1
End If
End Sub
Nimrod is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 11:50 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes