How to run loop for only one task

mtbthepro

Board Regular
Joined
Feb 22, 2017
Messages
91
I have this code, which inserts "Department 75" in a cell if criteria is met but the problem is that the macro is applied to all lines that meet the criteria which is if rows contain "Source Total" insert row another row and insert value "Department 75". Is there a way that this macro only applies to just the one/first line that meets criteria.

Code:
       For n = nlast To 1 Step -1                             If Sht.Cells(n, "C").Value = "Source Total==>" Then
                             Sht.Cells(n, "A").EntireRow.Offset(1).Insert
                             Sht.Cells(n, "A").Offset(1).Value = "Department 75"
                        End If
                             Next n
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hello,

just put exit sub statement after your action:

Code:
 For n = nlast To 1 Step -1                             If Sht.Cells(n, "C").Value = "Source Total==>" Then
                             Sht.Cells(n, "A").EntireRow.Offset(1).Insert
                             Sht.Cells(n, "A").Offset(1).Value = "Department 75"
                             Exit Sub
                        End If
                             Next n
 
Upvote 0
Hello,

just put exit sub statement after your action:

Code:
 For n = nlast To 1 Step -1                             If Sht.Cells(n, "C").Value = "Source Total==>" Then
                             Sht.Cells(n, "A").EntireRow.Offset(1).Insert
                             Sht.Cells(n, "A").Offset(1).Value = "Department 75"
                             Exit Sub
                        End If
                             Next n
It didn't work, it is still inserting "Department 75" IN OTHER PLACES
 
Upvote 0
not possible :) there must be something what triggers subroutine again ... do you have another part of code when cell value changed, or something else like that?
 
Upvote 0
not possible :) there must be something what triggers subroutine again ... do you have another part of code when cell value changed, or something else like that?
Lol yes, you are right. Something was triggering it. It works perfect. Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,134
Messages
6,123,237
Members
449,093
Latest member
Vincent Khandagale

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