Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Which is the best method to use

I am trying to write a code that does the following:

If the value in Range(B3) or Range(B4) is changed perform some calculations
in the worksheet.

But everytime I try to run the code, when it gets to:
..Range("I36") = c.Offset(0, 1).Value
it jumps back to the beginning again which I assume is because the value in
that cell is changed and the Sub is running itself again which is going to
end up being as infinite loop. How can I write a code that does what I want.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
End If

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Which is the best method to use

temporarily disable the events...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
application.enableevents = false
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
application.enableevents = true
End If

--
HTH...

Jim Thomlinson


"Ayo" wrote:

I am trying to write a code that does the following:

If the value in Range(B3) or Range(B4) is changed perform some calculations
in the worksheet.

But everytime I try to run the code, when it gets to:
.Range("I36") = c.Offset(0, 1).Value
it jumps back to the beginning again which I assume is because the value in
that cell is changed and the Sub is running itself again which is going to
end up being as infinite loop. How can I write a code that does what I want.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
End If

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Which is the best method to use

Thanks so much. Worked great.

"Jim Thomlinson" wrote:

temporarily disable the events...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
application.enableevents = false
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
application.enableevents = true
End If

--
HTH...

Jim Thomlinson


"Ayo" wrote:

I am trying to write a code that does the following:

If the value in Range(B3) or Range(B4) is changed perform some calculations
in the worksheet.

But everytime I try to run the code, when it gets to:
.Range("I36") = c.Offset(0, 1).Value
it jumps back to the beginning again which I assume is because the value in
that cell is changed and the Sub is running itself again which is going to
end up being as infinite loop. How can I write a code that does what I want.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
End If

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Which is the best method to use

Spoke too soon. It just worked the first time I tried it. Now it doesn't do
anything. The values are not changing; they just remain the same when I
change Range("B3"). Any ideas?

"Jim Thomlinson" wrote:

temporarily disable the events...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
application.enableevents = false
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
application.enableevents = true
End If

--
HTH...

Jim Thomlinson


"Ayo" wrote:

I am trying to write a code that does the following:

If the value in Range(B3) or Range(B4) is changed perform some calculations
in the worksheet.

But everytime I try to run the code, when it gets to:
.Range("I36") = c.Offset(0, 1).Value
it jumps back to the beginning again which I assume is because the value in
that cell is changed and the Sub is running itself again which is going to
end up being as infinite loop. How can I write a code that does what I want.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
End If

End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Which is the best method to use

Never mind Jim, looks like its working again. Thanks for all your help.

"Jim Thomlinson" wrote:

temporarily disable the events...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
application.enableevents = false
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
application.enableevents = true
End If

--
HTH...

Jim Thomlinson


"Ayo" wrote:

I am trying to write a code that does the following:

If the value in Range(B3) or Range(B4) is changed perform some calculations
in the worksheet.

But everytime I try to run the code, when it gets to:
.Range("I36") = c.Offset(0, 1).Value
it jumps back to the beginning again which I assume is because the value in
that cell is changed and the Sub is running itself again which is going to
end up being as infinite loop. How can I write a code that does what I want.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address() = "$B$3" Then
With ActiveSheet
For Each c In Worksheets("Goals").Range("C3:C54").Cells
If c.Value = .Range("B4") And c.Offset(0, -1).Value = .Range("B3")
Then
.Range("I36") = c.Offset(0, 1).Value
.Range("I37") = c.Offset(0, 3).Value
.Range("I38") = c.Offset(0, 5).Value
.Range("I39") = c.Offset(0, 7).Value
Exit For
End If
Next c
End With
End If

End Sub



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
Method "Method 'Open' of object 'Workbooks' failed [email protected] Excel Programming 2 June 2nd 10 05:28 PM
Please post this thread a correct full method, method about Nast Runsome New Users to Excel 8 February 25th 08 03:29 PM
Please post this thread a complete correct method, method about te Nast Runsome New Users to Excel 0 February 23rd 08 09:42 PM
GetObject method not work after Call Shell Method ben Excel Programming 8 February 21st 06 03:45 PM
Why QUIT method doesn't work after COPY method? surotkin Excel Programming 3 October 26th 05 04:32 PM


All times are GMT +1. The time now is 11:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"