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:
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

  1. It looks to me like you want to go through the range A6:A100 and if the cell is not 118 then insert a row below and put 118 in that new row.
    Is that correct?

  2. I assume that if any cells are blank that you do not want to insert a new row for those ones.
    Is that correct?

  3. So, if your original data was like the first sheet below then you would want the code to make the sheet look like the second sheet below.
    Is that correct?
Blue cells are the original data, green cells are the cells to be inserted by the code
Beginner1200.xlsm
A
6100
7118
8155
93
10118
11118
12118
1320
1465
Sheet2


Beginner1200.xlsm
A
6100
7118
8118
9155
10118
113
12118
13118
14118
15118
1620
17118
1865
19118
Sheet2


If the above is not what you want, then please post two small mini sheets like this with "before" and "after" layouts
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

  1. It looks to me like you want to go through the range A6:A100 and if the cell is not 118 then insert a row below and put 118 in that new row.
    Is that correct?

  2. I assume that if any cells are blank that you do not want to insert a new row for those ones.
    Is that correct?

  3. So, if your original data was like the first sheet below then you would want the code to make the sheet look like the second sheet below.
    Is that correct?
Blue cells are the original data, green cells are the cells to be inserted by the code
Beginner1200.xlsm
A
6100
7118
8155
93
10118
11118
12118
1320
1465
Sheet2


Beginner1200.xlsm
A
6100
7118
8118
9155
10118
113
12118
13118
14118
15118
1620
17118
1865
19118
Sheet2


If the above is not what you want, then please post two small mini sheets like this with "before" and "after" layouts
Hi Peter

You´re correct on 1 & 2 - please see below.
Hope I got i right ?

Lars

Original before

Original .png

My code

My code to sheet.png


My code .png
 
Upvote 0
VBA Code:
Sub SelectingEitherLine1OrLine2()

    Dim cell As Range

    For Each cell In Range("A9")
        
        If cell.value <> 300 Then GoTo Line2 Else: GoTo Line1
Line2:
        cell.Offset(1).EntireRow.Insert
        cell.Offset(1) = 301
        Exit For
    Next cell
Line1:
End Sub
 
Upvote 0
You´re correct on 1 & 2 -
In that case use the code from post #7 but use 118 instead of 300

VBA Code:
Sub Test2()
  Dim i As Long
  
  For i = 100 To 6 Step -1
    If Len(Range("A" & i).Value) > 0 And Range("A" & i).Value <> 118 Then
      Rows(i + 1).Insert
      Range("A" & i + 1).Value = 118
    End If
  Next i
End Sub

Before

Beginner1200.xlsm
A
6100
7118
8155
93
10118
11118
12118
1320
1465
15
16
17
18
19
20
Sheet3


After

Beginner1200.xlsm
A
6100
7118
8118
9155
10118
113
12118
13118
14118
15118
1620
17118
1865
19118
20
Sheet3
 
Upvote 0
Hi Peter (update)

I figured it out in my own way! Thanks a lot for your input! You gave me an different approach to look on it. :)


VBA Code:
Sub SelectingEitherLine1OrLineDone()

    Dim foundRng As Range
    Set foundRng = Range("A6:A14").Find("705")
    
    If foundRng Is Nothing Then GoTo Line2 Else: GoTo Line1
    
Line2:
        Range("A6").Offset(1).EntireRow.Insert
        Range("A6").Offset(1) = 705
Line1:
End Sub
 
Upvote 0
Solution
Glad you have something that you want. The only thing is that the code seems to have nothing to do with the thread title (Loop problems) as there is no looping at all involved, nor what you described earlier
I need to loop through the whole range of cells, finding ex number 100

I would also comment that using coding like GoTo Line1 is very old-fashioned and there are usually much easier ways to do the same thing.
For example, this will do exactly the same thing as your code from post 15 but in a much more streamlined way.

VBA Code:
Sub Example1()
    Dim foundRng As Range
    Set foundRng = Range("A6:A14").Find("705")
    
    If foundRng Is Nothing Then
        Range("A6").Offset(1).EntireRow.Insert
        Range("A6").Offset(1) = 705
    End If
End Sub

If you were going to put any code after Line1: in your code then it would be like this.

VBA Code:
Sub Example2()
    Dim foundRng As Range
    Set foundRng = Range("A6:A14").Find("705")
    
    If foundRng Is Nothing Then
      Range("A6").Offset(1).EntireRow.Insert
      Range("A6").Offset(1) = 705
    Else
      'Do here what would have followed after Line1: in your other code
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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