Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to use the following command
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Application.Intersect(Target, Range("A51,A100,A149")).
But instead of a target range A51,A100,A149 I want to put the command in each of the cells and perform the function associated with the particular cell. However I do not seem to be able to put this function into the workbook 3 times as the name ambiguous error is flagged because I am using the same name, if I append the title the function does not work.
I also need to know how to register variables as Rw and returnvalue are not able to be used.
This is an example of what I want to doPrivate Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Application.Intersect(Target, Range("A51")) Is Nothing Then
If Target.Value > 0 Then
.Unprotect
Application.ScreenUpdating = False
For Rw = 51 To 99
Cells(Rw, 1).EntireRow.Hidden = False
Next
Application.ScreenUpdating = True
.Protect
End With
End SubPrivate Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Application.Intersect(Target, Range("A100")) Is Nothing Then
If Target.Value > 0 Then
.Unprotect
Application.ScreenUpdating = False
For Rw = 100 To 148
Cells(Rw, 1).EntireRow.Hidden = False
Next
Application.ScreenUpdating = True
.Protect
End With
End Sub

First off, let's clear up the terminology.
What you are doing is not called a Command or a Function. It's called a Worksheet Change macro.
OK, now onto your problems...
It looks like you are trying to perform an action based on certain target cells, specifically A51,A100,A149.
Instead of
If Not Application.Intersect(Target, Range("A100")) Is Nothing
try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$51" Then
'Your A51 code goes here
End If
If Target.Address = "$A$100" Then
'Your A100 code goes here
End If
If Target.Address = "$A$149" Then
'Your A149 code goes here
End If
End Sub
I'm not sure what you mean when you say "Rw and returnvalue are not able to be used."What error do you get?
The only thing I can think of based on the limited information provided is that you have Option Explicit set someplace which would force you to declare all variables with a Dim statement before you can use them.
Give us some more details and we'll see what we can do.

You were quite right Option Explicit was there. Something else you've taught me.
The Worksheet_Change(ByVal Target As Range)also works for me.
Thank you very much for all your help excellent advice as always.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |