Excel Stops Responding when running this simple code

Ritzyy

New Member
Joined
Jan 28, 2013
Messages
29
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Dim row As Range
Dim cell As Range

myvar = "A1:A" & ThisWorkbook.Sheets("Sheet3").Range("A5") - ThisWorkbook.Sheets("Sheet3").Range("A3")
For Each row In Range(myvar).Rows
For Each cell In row.Cells
cell = 1
Next cell
Next row

End Sub




I can't work out what I've done wrong... sometimes I get an error wiht the myvar line.. other times it runs fine but breaks excel.

all it's trying to do is insert 1 in 94 rows. (ThisWorkbook.Sheets("Sheet3").Range("A5") - ThisWorkbook.Sheets("Sheet3").Range("A3") = 94)
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi Ritzyy,

Try turning off events so it doesn't call itself recursively each time it sets a cell to 1:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Dim row As Range
Dim cell As Range

Application.EnableEvents = False
myvar = "A1:A" & ThisWorkbook.Sheets("Sheet3").Range("A5") - ThisWorkbook.Sheets("Sheet3").Range("A3")
For Each row In Range(myvar).Rows
For Each cell In row.Cells
cell = 1
Next cell
Next row
Application.EnableEvents = True

End Sub
 
Upvote 0
Hi again Ritzyy,

Just a suggestion. Here is a little simpler way of accomplishing the same thing:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   Dim WS3        As Worksheet
   Dim iRow       As Long
   Dim LastRow    As Long
   
   Set WS3 = ThisWorkbook.Sheets("Sheet3")
   
   LastRow = WS3.Range("A5") - WS3.Range("A3")
   
   Application.EnableEvents = False
   For iRow = 1 To LastRow
      Cells(iRow, "A") = 2
   Next iRow
   Application.EnableEvents = True
   
End Sub

Damon
 
Upvote 0
Hi again Ritzyy,

Just a suggestion. Here is a little simpler way of accomplishing the same thing:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   Dim WS3        As Worksheet
   Dim iRow       As Long
   Dim LastRow    As Long
   
   Set WS3 = ThisWorkbook.Sheets("Sheet3")
   
   LastRow = WS3.Range("A5") - WS3.Range("A3")
   
   Application.EnableEvents = False
   For iRow = 1 To LastRow
      Cells(iRow, "A") = 2
   Next iRow
   Application.EnableEvents = True
   
End Sub

Damon

You're a genius mate. Thanks!
I'm still relatively new to this whole excel macro thing.

With events and stuff, I just had worksheet selection change as an example but i've been meaning to make it so when another worksheet in the same workbook changes a certain cell value this will update itself. Is there a way to have an event condition for this worksheet which relies on eg. A5 from Sheet2 changing value?

Thanks again man
 
Upvote 0
Event condition relies on e.g. A5.

Hi again Ritzyy,

Yes, this is what the Target parameter is for. Target is the range that was changed and caused the Worksheet Change event to fire. For example, if you want your previous event to fire only if A5 changes value the code would be

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   Dim WS3        As Worksheet
   Dim iRow       As Long
   Dim LastRow    As Long

   If Target.Address <> "$A$5" Then Exit Sub
   
   Set WS3 = ThisWorkbook.Sheets("Sheet3")
   
   LastRow = WS3.Range("A5") - WS3.Range("A3")
   
   Application.EnableEvents = False
   For iRow = 1 To LastRow
      Cells(iRow, "A") = 1
   Next iRow
   Application.EnableEvents = True
   
End Sub

Of course this code would turn right around and set the value of A5 to 1, which probably isn't what you want. Also you mentioned A5 in Sheet2. I'm assuming this code resides in Sheet2's code module--otherwise it wouldn't fire Sheet2's Change event. If you want a worksheet Change event to fire no matter what sheet is changed you must place it in the ThisWorkbook code module, and in your code you should check which sheet the change occurred to as well as which cell.

I hope this helps.

Damon

You're a genius mate. Thanks!
I'm still relatively new to this whole excel macro thing.

With events and stuff, I just had worksheet selection change as an example but i've been meaning to make it so when another worksheet in the same workbook changes a certain cell value this will update itself. Is there a way to have an event condition for this worksheet which relies on eg. A5 from Sheet2 changing value?

Thanks again man
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top