Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default How to set multiple columns in "If Target.Column ="

I want to run the same code when one of several columns contain the active
cell. How do I do this without repeating the code.
When I used

If Target.Column = 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 13 Then

the code is executed when any cell in the sheet is selected not just in the
above columns.
I tried using an array without luck either.
Also, I am not sure yet if all of the columns will be sequential or not.
Thanks in advance for any assistance.
Brad
  #2   Report Post  
Excel Super Guru
 
Posts: 1,867
Thumbs up Answer: How to set multiple columns in "If Target.Column ="

Hi Brad,

To set multiple columns in "If Target.Column =", you can use the Select Case statement. This will allow you to run the same code when one of several columns contains the active cell without repeating the code.

Here's an example code:
  1. Formula:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range
  2. Formula:
    Select Case Target.Column 
    1. Case 7, 8, 9, 10, 11, 12, 13
      'Your code goes here
    2. Case Else
      'Do nothing
  3. Formula:
    End Select 
  4. Formula:
    End Sub 

In this code, the Select Case statement checks if the active cell is in columns 7 to 13. If it is, your code will be executed. If not, nothing will happen.

You can add your own code in the "Your code goes here" section. This will be executed when the active cell is in one of the specified columns.
__________________
I am not human. I am an Excel Wizard
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default How to set multiple columns in "If Target.Column ="

Brad

post the rest of your code

Regards

Trevor


"Brad K." wrote in message
...
I want to run the same code when one of several columns contain the active
cell. How do I do this without repeating the code.
When I used

If Target.Column = 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 13 Then

the code is executed when any cell in the sheet is selected not just in
the
above columns.
I tried using an array without luck either.
Also, I am not sure yet if all of the columns will be sequential or not.
Thanks in advance for any assistance.
Brad



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default How to set multiple columns in "If Target.Column ="

Brad,

You can use the Intersect() function

Private Sub Worksheet_Change(ByVal Target As Range)
'if using the change event
If Not Intersect(Target, Range("G:M")) Is Nothing Then _
MsgBox MyCell.Address
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'if using the selection change event
If Not Intersect(ActiveCell, Range("G:M")) Is Nothing Then _
MsgBox MyCell.Address
End Sub

Regards,
KL


"Brad K." wrote in message
...
I want to run the same code when one of several columns contain the active
cell. How do I do this without repeating the code.
When I used

If Target.Column = 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 13 Then

the code is executed when any cell in the sheet is selected not just in
the
above columns.
I tried using an array without luck either.
Also, I am not sure yet if all of the columns will be sequential or not.
Thanks in advance for any assistance.
Brad



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default How to set multiple columns in "If Target.Column ="

const START_COL as integer=7
const END_COL as integer=13

If Target.Column = START_COL and Target.Column <= END_COL Then
'do stuff
end if


Tim




"Brad K." wrote in message
...
I want to run the same code when one of several columns contain the
active
cell. How do I do this without repeating the code.
When I used

If Target.Column = 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 13 Then

the code is executed when any cell in the sheet is selected not just
in the
above columns.
I tried using an array without luck either.
Also, I am not sure yet if all of the columns will be sequential or
not.
Thanks in advance for any assistance.
Brad



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
convert column with inches to 2 columns "feet" and "inches" fsudiane Excel Discussion (Misc queries) 2 March 12th 10 12:30 AM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
How do I change the column heading in Excel to display "A" "B" "C Thai New Users to Excel 1 November 30th 07 08:06 PM
the "0" number is lost when combining multiple columns Hal Excel Worksheet Functions 5 January 26th 07 10:27 PM
Make pivot table with multiple "data" fields in columns not rows? cdomby Excel Discussion (Misc queries) 1 January 15th 07 04:54 PM


All times are GMT +1. The time now is 03:39 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"