If loop embedded in "for each" prematurely ends.

HansDK

New Member
Joined
Oct 19, 2021
Messages
7
Office Version
  1. 2019
Platform
  1. Windows
Hi all.

I have a for each loop which looks like this:

VBA Code:
sub monthnumber()
Worksheets("ws1").Range("K4:AS4").Clear
Worksheets("ws1").Range("K8:AS8").Clear
Range("K4").Value = Month(Worksheets("ws1").Range("M2"))
m0 = Month(Worksheets("ws1").Range("M2"))

For Each m In rng
        If m.Value <> 1 Then
            m.Offset(-3, 1).Value = m.Offset(-3, 0).Value
        ElseIf m.Value = 1 Then
           m.Offset(-3, 0).Value = m.Value + m0
           m0 = m0 + 1
        End If
Next m
end sub

The purpose of the code is to copy a number in a cell in a row and insert it in the following row-cell. so for instance if cell A4 has a value, that same value should be inserted in to B4, C4 and so on. In the event of a change of month (lets say a change from month number 5 to 6), the month-number goes up by 1 and the copy/pasting should continue. The trigger for the month-change is that the day-value (m) becomes 1.

However, whenever the month changes, the loop stops and nothing more happens. Any help would be greatly appreciated. :)
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I'm assuming that the code shown is not your actual code, you haven't set anything to rng so you're going to get a runtime error before the loop even starts.

If you want us to debug an error in your code then we need to see that code, not a snippet that closely resembles part of it. The problem is most likely going to be in the part that you haven't shown us.
 
Upvote 0

Forum statistics

Threads
1,215,553
Messages
6,125,483
Members
449,233
Latest member
Deardevil

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