Loop problems

Beginner1200

New Member
Joined
Jan 18, 2023
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
I am whacking to get the code working but only semi. It seems it only goto A6 and not through until A100... I have tried a lot to get in to work!
but nothing will dot as i want it to do. I hope i can get some help to fix my issue :)

A1 = 100
A2 = 200
A3 = 300
A4 = 400
A5 = 500
A6 = 600
A7.... A100


VBA Code:
Sub SelectingEitherLine1OrLine2()

    Dim cell As Range

    For Each cell In Range("A6:A100")
       
        If cell.Value <> 300 Then GoTo Line2 Else: GoTo Line1
Line2:
        cell.Offset(1).EntireRow.Insert
        cell.Offset(1) = 300
        Exit For
    Next cell
Line1:
End Sub
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Is the Line1: supposed to be on the outside of the for-next loop?
 
Upvote 0
Yes stronger! :)

I have tried it... same condition.

For Each cell In Range("A6:A100")
If cell.Value <> 300 Then GoTo Line2 Else: GoTo Line1
when i execute this, then it goes to Line "line2" And i dont want this to happend is should go to "line1" It seems it stays at cell A6. As it should goto A8 = 300

But if I indstead of For Each cell In Range("A6:A100") change A6 to A8 then the code is working fine.

I think when I am looking at the forest I dont see the threes :)
 
Upvote 0
Because you have a "exit for" line which suspends the code as soon as you go to line2, try removing that
Hi
I have tried this it doesnt work.

When instead of using ("A6:A100") just use ("A6") in my code its working perfekt. I need to loop through the whole range of cells, finding ex number 100 Then its working fine, but is type ex 200 then the code is not doing what i want it to... but when i type (A7) and 200 then its working fine...

A6 = 100
A7 = 200
A8 = 300
A9 = 400
 
Upvote 0
When inserting rows, it is generally best to work from the bottom up.
Try this with a copy of your worksheet.

VBA Code:
Sub Test()
  Dim i As Long
  
  For i = 100 To 6 Step -1
    If Len(Range("A" & i).Value) > 0 And Range("A" & i).Value <> 300 Then
      Rows(i + 1).Insert
      Range("A" & i + 1).Value = 300
    End If
  Next i
End Sub
 
Upvote 0
When inserting rows, it is generally best to work from the bottom up.
Try this with a copy of your worksheet.

VBA Code:
Sub Test()
  Dim i As Long
 
  For i = 100 To 6 Step -1
    If Len(Range("A" & i).Value) > 0 And Range("A" & i).Value <> 300 Then
      Rows(i + 1).Insert
      Range("A" & i + 1).Value = 300
    End If
  Next i
End Sub
Hi Peter
Thanks for your sugestion. I can see your point to work up from the bottom. I have tried your code (But it doesnt exactly do what i want it to do) . I am working with it :)
Much appriciate your input :)
 
Upvote 0
Cheers, good luck! (y)
If you end up needing more help, post back with more exact details of your objective.
 
Upvote 0
Cheers, good luck! (y)
If you end up needing more help, post back with more exact details of your objective.
Hi Peter

I am struggling a bit to get it to work :unsure:

My first code is working! but only almost (You can see it here below)
I want from A6 to A100 to look up if ex. 118 is present! And if not (<>) then to go to "Line2:" else goto "line1" and end sub.
My code only works example are typing "A18" in! If I example do "A6:A50" My intension is to loop through the whole range and not only one cell.

Your code which you send to me, are giving sense in some way... but I think I am missing some out.

My English is not the best for explaining in this subject :)

VBA Code:
Sub SelectingEitherLine1OrLine2()

    Dim cell As Range

    For Each cell In Range("A18")
       
        If cell.value <> 118 Then GoTo Line2 Else: GoTo Line1
Line2:
        cell.Offset(1).EntireRow.Insert
        cell.Offset(1) = 118
        Exit For
    Next cell
Line1:
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,588
Members
449,174
Latest member
chandan4057

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