PC Review


Reply
Thread Tools Rate Thread

Can the last two if statements be combined together?

 
 
Brad
Guest
Posts: n/a
 
      1st Apr 2009
If the user change c4, i want the value of c39 to change or
If the user change e4, i want the value of e39 to change

Is there a better way??

Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Range("c4", "e4"), Target) Is Nothing Then
Application.EnableAutoComplete = True
Else
Application.EnableAutoComplete = False
End If
If Intersect(Range("c4"), Target) Is Nothing Then
Application.EnableAutoComplete = True
Else
Range("c39").Value = Range("c4").Value
End If
If Intersect(Range("e4"), Target) Is Nothing Then
Application.EnableAutoComplete = True
Else
Range("e39").Value = Range("e4").Value
End If

End Sub

 
Reply With Quote
 
 
 
 
joel
Guest
Posts: n/a
 
      1st Apr 2009
This will work. It uses binary arithmetic.

Sub Worksheet_SelectionChange(ByVal Target As Range)
Results = 0
if not Intersect(Range("c4"), Target) is nothing then
Results = Results + 1
end if
if not Intersect(Range("E4"), Target) is nothing then
Results = Results + 2
end if

select Case Results
case 0:
Application.EnableAutoComplete = True

case 1:
Application.EnableAutoComplete = True
Range("c39").Value = Range("c4").Value

case 2:
Application.EnableAutoComplete = True
Range("e39").Value = Range("e4").Value

case 3:
Application.EnableAutoComplete = False
Range("c39").Value = Range("c4").Value
Range("e39").Value = Range("e4").Value
end select
end sub


"Brad" wrote:

> If the user change c4, i want the value of c39 to change or
> If the user change e4, i want the value of e39 to change
>
> Is there a better way??
>
> Sub Worksheet_SelectionChange(ByVal Target As Range)
> If Intersect(Range("c4", "e4"), Target) Is Nothing Then
> Application.EnableAutoComplete = True
> Else
> Application.EnableAutoComplete = False
> End If
> If Intersect(Range("c4"), Target) Is Nothing Then
> Application.EnableAutoComplete = True
> Else
> Range("c39").Value = Range("c4").Value
> End If
> If Intersect(Range("e4"), Target) Is Nothing Then
> Application.EnableAutoComplete = True
> Else
> Range("e39").Value = Range("e4").Value
> End If
>
> End Sub
>

 
Reply With Quote
 
joel
Guest
Posts: n/a
 
      1st Apr 2009
Brad: I forgot to mention one item. if somebody copies a range of cells and
pastes it on over both cells, both cells will change.

"joel" wrote:

> This will work. It uses binary arithmetic.
>
> Sub Worksheet_SelectionChange(ByVal Target As Range)
> Results = 0
> if not Intersect(Range("c4"), Target) is nothing then
> Results = Results + 1
> end if
> if not Intersect(Range("E4"), Target) is nothing then
> Results = Results + 2
> end if
>
> select Case Results
> case 0:
> Application.EnableAutoComplete = True
>
> case 1:
> Application.EnableAutoComplete = True
> Range("c39").Value = Range("c4").Value
>
> case 2:
> Application.EnableAutoComplete = True
> Range("e39").Value = Range("e4").Value
>
> case 3:
> Application.EnableAutoComplete = False
> Range("c39").Value = Range("c4").Value
> Range("e39").Value = Range("e4").Value
> end select
> end sub
>
>
> "Brad" wrote:
>
> > If the user change c4, i want the value of c39 to change or
> > If the user change e4, i want the value of e39 to change
> >
> > Is there a better way??
> >
> > Sub Worksheet_SelectionChange(ByVal Target As Range)
> > If Intersect(Range("c4", "e4"), Target) Is Nothing Then
> > Application.EnableAutoComplete = True
> > Else
> > Application.EnableAutoComplete = False
> > End If
> > If Intersect(Range("c4"), Target) Is Nothing Then
> > Application.EnableAutoComplete = True
> > Else
> > Range("c39").Value = Range("c4").Value
> > End If
> > If Intersect(Range("e4"), Target) Is Nothing Then
> > Application.EnableAutoComplete = True
> > Else
> > Range("e39").Value = Range("e4").Value
> > End If
> >
> > End Sub
> >

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Apr 2009
If I read what you are doing correctly, you should be able to use this code
in place of what you posted...

Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("C4,E4"), Target) Is Nothing Then
Application.EnableAutoComplete = False
Cells(39, Target.Column).Value = Target.Value
Application.EnableAutoComplete = True
End If
End Sub

Notice that I changed the Range you used in the Intersect function call from
("C4","E4") to ("C4,E4")... the range you used included the cells C4, D4 and
E4 whereas your subsequent code seemed to indicate that you don't care about
Target being D4.

--
Rick (MVP - Excel)


"Brad" <(E-Mail Removed)> wrote in message
news:B2398E46-7EC3-4AB1-9F00-(E-Mail Removed)...
> If the user change c4, i want the value of c39 to change or
> If the user change e4, i want the value of e39 to change
>
> Is there a better way??
>
> Sub Worksheet_SelectionChange(ByVal Target As Range)
> If Intersect(Range("c4", "e4"), Target) Is Nothing Then
> Application.EnableAutoComplete = True
> Else
> Application.EnableAutoComplete = False
> End If
> If Intersect(Range("c4"), Target) Is Nothing Then
> Application.EnableAutoComplete = True
> Else
> Range("c39").Value = Range("c4").Value
> End If
> If Intersect(Range("e4"), Target) Is Nothing Then
> Application.EnableAutoComplete = True
> Else
> Range("e39").Value = Range("e4").Value
> End If
>
> End Sub
>


 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      1st Apr 2009

For case 1 and 2 should not the application.EnableAutoComplete be False?

"joel" wrote:

> This will work. It uses binary arithmetic.
>
> Sub Worksheet_SelectionChange(ByVal Target As Range)
> Results = 0
> if not Intersect(Range("c4"), Target) is nothing then
> Results = Results + 1
> end if
> if not Intersect(Range("E4"), Target) is nothing then
> Results = Results + 2
> end if
>
> select Case Results
> case 0:
> Application.EnableAutoComplete = True
>
> case 1:
> Application.EnableAutoComplete = True
> Range("c39").Value = Range("c4").Value
>
> case 2:
> Application.EnableAutoComplete = True
> Range("e39").Value = Range("e4").Value
>
> case 3:
> Application.EnableAutoComplete = False
> Range("c39").Value = Range("c4").Value
> Range("e39").Value = Range("e4").Value
> end select
> end sub
>
>
> "Brad" wrote:
>
> > If the user change c4, i want the value of c39 to change or
> > If the user change e4, i want the value of e39 to change
> >
> > Is there a better way??
> >
> > Sub Worksheet_SelectionChange(ByVal Target As Range)
> > If Intersect(Range("c4", "e4"), Target) Is Nothing Then
> > Application.EnableAutoComplete = True
> > Else
> > Application.EnableAutoComplete = False
> > End If
> > If Intersect(Range("c4"), Target) Is Nothing Then
> > Application.EnableAutoComplete = True
> > Else
> > Range("c39").Value = Range("c4").Value
> > End If
> > If Intersect(Range("e4"), Target) Is Nothing Then
> > Application.EnableAutoComplete = True
> > Else
> > Range("e39").Value = Range("e4").Value
> > End If
> >
> > End Sub
> >

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Apr 2009
> Cells(39, Target.Column).Value = Target.Value

You could also use this line instead of the one above (they will both work
the same)...

Target.Offset(35).Value = Target.Value

--
Rick (MVP - Excel)


"Rick Rothstein" <(E-Mail Removed)> wrote in message
news:OYu$(E-Mail Removed)...
> If I read what you are doing correctly, you should be able to use this
> code in place of what you posted...
>
> Sub Worksheet_SelectionChange(ByVal Target As Range)
> If Not Intersect(Range("C4,E4"), Target) Is Nothing Then
> Application.EnableAutoComplete = False
> Cells(39, Target.Column).Value = Target.Value
> Application.EnableAutoComplete = True
> End If
> End Sub
>
> Notice that I changed the Range you used in the Intersect function call
> from ("C4","E4") to ("C4,E4")... the range you used included the cells C4,
> D4 and E4 whereas your subsequent code seemed to indicate that you don't
> care about Target being D4.
>
> --
> Rick (MVP - Excel)
>
>
> "Brad" <(E-Mail Removed)> wrote in message
> news:B2398E46-7EC3-4AB1-9F00-(E-Mail Removed)...
>> If the user change c4, i want the value of c39 to change or
>> If the user change e4, i want the value of e39 to change
>>
>> Is there a better way??
>>
>> Sub Worksheet_SelectionChange(ByVal Target As Range)
>> If Intersect(Range("c4", "e4"), Target) Is Nothing Then
>> Application.EnableAutoComplete = True
>> Else
>> Application.EnableAutoComplete = False
>> End If
>> If Intersect(Range("c4"), Target) Is Nothing Then
>> Application.EnableAutoComplete = True
>> Else
>> Range("c39").Value = Range("c4").Value
>> End If
>> If Intersect(Range("e4"), Target) Is Nothing Then
>> Application.EnableAutoComplete = True
>> Else
>> Range("e39").Value = Range("e4").Value
>> End If
>>
>> End Sub
>>

>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF, and, or statements combined lite Microsoft Excel Programming 1 7th Aug 2009 10:31 PM
IF STATEMENTS COMBINED WITH VLOOKUPS peggyL Microsoft Excel Worksheet Functions 2 1st Jul 2008 09:10 PM
Use of Combined IF and AND statements ConfusedNHouston Microsoft Excel Misc 2 11th Mar 2008 06:30 PM
Combined Statements =?Utf-8?B?RG9u?= Microsoft Excel Misc 1 29th May 2007 11:24 PM
Using Min & Max in combined SQL statements Llyllyll Microsoft Access Queries 4 3rd Nov 2004 07:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:56 PM.