PC Review


Reply
Thread Tools Rate Thread

Auto-calculate after a cell is filled in - Macro is looping!!!

 
 
=?Utf-8?B?cmFwaGllbDIwNjM=?=
Guest
Posts: n/a
 
      6th Sep 2007
Hi

I'm still struggling with the below as the macro appears to be contantly
looping. I've set it up so if any of the input cells are used it will trigger
the calculations to be performed on the corresponding cell in the same row.

However, excel just freezes and I have to abort the macro.... any ideas?


Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Range(Target.Address), Range("A:EE")) _
Is Nothing Then

Dim r As Long
r = Target.Row

If Cells(r, "B").Value <> "" Or _
Cells(r, "C").Value <> "" Or _
Cells(r, "E").Value <> "" Then

' The below equation is the original sumif formula I was using in the cell
' =SUMIF($U$4:$BL$4,$A$2,U5:BL5)

' the below perform sumif's on the same range
Cells(r, "I").FormulaR1C1 =
"=SUMIF(R4C21:R4C64,R2C1,RC[12]:RC[55])"
Cells(r, "J").FormulaR1C1 =
"=SUMIF(R4C21:R4C64,R3C1,RC[11]:RC[54])"
Cells(r, "K").FormulaR1C1 =
"=SUMIF(R4C21:R4C64,R4C1,RC[10]:RC[53])"

' this totals the sumif's
Cells(r, "L").FormulaR1C1 =
"=SUM(RC[-3]:RC[-1])+SUM(RC[5]:RC[7])"

' These take the result of the above sumif and multiply it by a unit price
Cells(r, "M").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-5]"
Cells(r, "N").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-6]"
Cells(r, "O").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-7]"

' This gives a grand total of the above three sub-totals
Cells(r, "P").FormulaR1C1 = "=SUM(RC[-3]:RC[-1])"
End If
End If
End Sub


 
Reply With Quote
 
 
 
 
Jim Rech
Guest
Posts: n/a
 
      6th Sep 2007
Worksheet_Change is called when you make an entry in a sheet, either
manually or by macro. Since your macro is changing the sheet it is in
effect calling itself (again and again).

Whenever you change a sheet change in sub Worksheet_Change put
Application.EnableEvents=False at the beginning of the sub and then set it
to True at the end.

--
Jim
"raphiel2063" <(E-Mail Removed)> wrote in message
news:A3126C9B-A0F4-459A-81AA-(E-Mail Removed)...
| Hi
|
| I'm still struggling with the below as the macro appears to be contantly
| looping. I've set it up so if any of the input cells are used it will
trigger
| the calculations to be performed on the corresponding cell in the same
row.
|
| However, excel just freezes and I have to abort the macro.... any ideas?
|
|
| Private Sub Worksheet_Change(ByVal Target As Range)
|
| If Not Intersect(Range(Target.Address), Range("A:EE")) _
| Is Nothing Then
|
| Dim r As Long
| r = Target.Row
|
| If Cells(r, "B").Value <> "" Or _
| Cells(r, "C").Value <> "" Or _
| Cells(r, "E").Value <> "" Then
|
| ' The below equation is the original sumif formula I was using in the
cell
| ' =SUMIF($U$4:$BL$4,$A$2,U5:BL5)
|
| ' the below perform sumif's on the same range
| Cells(r, "I").FormulaR1C1 =
| "=SUMIF(R4C21:R4C64,R2C1,RC[12]:RC[55])"
| Cells(r, "J").FormulaR1C1 =
| "=SUMIF(R4C21:R4C64,R3C1,RC[11]:RC[54])"
| Cells(r, "K").FormulaR1C1 =
| "=SUMIF(R4C21:R4C64,R4C1,RC[10]:RC[53])"
|
| ' this totals the sumif's
| Cells(r, "L").FormulaR1C1 =
| "=SUM(RC[-3]:RC[-1])+SUM(RC[5]:RC[7])"
|
| ' These take the result of the above sumif and multiply it by a unit
price
| Cells(r, "M").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-5]"
| Cells(r, "N").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-6]"
| Cells(r, "O").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-7]"
|
| ' This gives a grand total of the above three sub-totals
| Cells(r, "P").FormulaR1C1 = "=SUM(RC[-3]:RC[-1])"
| End If
| End If
| End Sub
|
|


 
Reply With Quote
 
=?Utf-8?B?cmFwaGllbDIwNjM=?=
Guest
Posts: n/a
 
      6th Sep 2007
Worked a charm, cheers.

"Jim Rech" wrote:

> Worksheet_Change is called when you make an entry in a sheet, either
> manually or by macro. Since your macro is changing the sheet it is in
> effect calling itself (again and again).
>
> Whenever you change a sheet change in sub Worksheet_Change put
> Application.EnableEvents=False at the beginning of the sub and then set it
> to True at the end.
>
> --
> Jim
> "raphiel2063" <(E-Mail Removed)> wrote in message
> news:A3126C9B-A0F4-459A-81AA-(E-Mail Removed)...
> | Hi
> |
> | I'm still struggling with the below as the macro appears to be contantly
> | looping. I've set it up so if any of the input cells are used it will
> trigger
> | the calculations to be performed on the corresponding cell in the same
> row.
> |
> | However, excel just freezes and I have to abort the macro.... any ideas?
> |
> |
> | Private Sub Worksheet_Change(ByVal Target As Range)
> |
> | If Not Intersect(Range(Target.Address), Range("A:EE")) _
> | Is Nothing Then
> |
> | Dim r As Long
> | r = Target.Row
> |
> | If Cells(r, "B").Value <> "" Or _
> | Cells(r, "C").Value <> "" Or _
> | Cells(r, "E").Value <> "" Then
> |
> | ' The below equation is the original sumif formula I was using in the
> cell
> | ' =SUMIF($U$4:$BL$4,$A$2,U5:BL5)
> |
> | ' the below perform sumif's on the same range
> | Cells(r, "I").FormulaR1C1 =
> | "=SUMIF(R4C21:R4C64,R2C1,RC[12]:RC[55])"
> | Cells(r, "J").FormulaR1C1 =
> | "=SUMIF(R4C21:R4C64,R3C1,RC[11]:RC[54])"
> | Cells(r, "K").FormulaR1C1 =
> | "=SUMIF(R4C21:R4C64,R4C1,RC[10]:RC[53])"
> |
> | ' this totals the sumif's
> | Cells(r, "L").FormulaR1C1 =
> | "=SUM(RC[-3]:RC[-1])+SUM(RC[5]:RC[7])"
> |
> | ' These take the result of the above sumif and multiply it by a unit
> price
> | Cells(r, "M").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-5]"
> | Cells(r, "N").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-6]"
> | Cells(r, "O").FormulaR1C1 = "=(RC[-4]+RC[4])*RC[-7]"
> |
> | ' This gives a grand total of the above three sub-totals
> | Cells(r, "P").FormulaR1C1 = "=SUM(RC[-3]:RC[-1])"
> | End If
> | 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
Auto Insert the present time when a cell is filled with a data, Dinanath Microsoft Excel New Users 2 8th Mar 2009 08:28 PM
Auto-calculate after a cell is filled in - macro shouldn't be loop =?Utf-8?B?cmFwaGllbDIwNjM=?= Microsoft Excel Programming 5 6th Sep 2007 04:30 PM
Calculate value in a text filled cell =?Utf-8?B?RC0t?= Microsoft Excel Worksheet Functions 6 12th Jul 2007 02:05 AM
Run Macro when cell is filled =?Utf-8?B?SmFja1I=?= Microsoft Excel Misc 4 21st Mar 2006 11:26 PM
How do I auto fill long col. filling blanks with last filled cell =?Utf-8?B?TUJCZWdpbm5lcg==?= Microsoft Excel Misc 3 15th Jan 2006 12:44 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:00 PM.