Iterate number only in certain row, within sets of rows

ExceL0ver

New Member
Joined
Apr 12, 2023
Messages
35
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I currently use the code below. I now have data in column H, so I have changed the range part to Range("A2:H3001"), as I want to iterate numbers in column H, too. However, this column contains different formulae in the rows, and I only want the script to iterate the number every 3rd row in each set of 8 rows. I think I would use "If i = 3 Then", but I'm not sure how I would apply this to only column H, as I still want the script to run as below with columns A to G.

VBA Code:
Sub IncrementRowNumbers()
  Dim i As Integer, j As Integer
  Dim replaceNum As Integer
  Dim myArray As Variant
  Dim myRange As Range
  Set myRange = Worksheets("All Data").Range("A2:G3001")
 
  myArray = myRange.Formula
  replaceNum = 6
 
  For i = 1 To UBound(myArray, 1)
    For j = 1 To UBound(myArray, 2)
      If j <> 4 And j <> 5 Then
        myArray(i, j) = Replace(myArray(i, j), "6", replaceNum + Int((i - 1) / 6))
      End If
    Next
  Next

  myRange.Formula = myArray
End Sub

Could anyone suggest how I would acheive this, please?

Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about?:

VBA Code:
    For i = 1 To UBound(myArray, 1)
        For j = 1 To UBound(myArray, 2)
          If j <> 4 And j <> 5 Then
            myArray(i, j) = Replace(myArray(i, j), "6", replaceNum + Int((i - 1) / 6))
          End If
        Next
        If i Mod 8 = 2 Then
            'Do something here
        End If
    Next

Or change the 2 to different numbers and see which one you need.
 
Upvote 0
How about?:

VBA Code:
    For i = 1 To UBound(myArray, 1)
        For j = 1 To UBound(myArray, 2)
          If j <> 4 And j <> 5 Then
            myArray(i, j) = Replace(myArray(i, j), "6", replaceNum + Int((i - 1) / 6))
          End If
        Next
        If i Mod 8 = 2 Then
            'Do something here
        End If
    Next

Or change the 2 to different numbers and see which one you need.
Sorry, I don't follow this. Could you explain?
 
Upvote 0
If you use

VBA Code:
Sub IncrementRowNumbers()
    Dim i As Integer, j As Integer
    Dim replaceNum As Integer
    Dim myArray As Variant
    Dim myRange As Range
    Set myRange = Worksheets("All Data").Range("A2:G3001")
  
    myArray = myRange.Formula
    replaceNum = 6
  
    For i = 1 To UBound(myArray, 1)
      For j = 1 To UBound(myArray, 2)
        If j <> 4 And j <> 5 Then
          myArray(i, j) = Replace(myArray(i, j), "6", replaceNum + Int((i - 1) / 6))
        End If
        If i Mod 8 = 2 Then
            myArray(i, j) = "new value " & i & " - " & j
        End If
      Next
    Next
  
    myRange.Formula = myArray
End Sub

You will get
Libro1.xlsm
ABCDEFGH
1
2
3new value 2 - 1new value 2 - 2new value 2 - 3new value 2 - 4new value 2 - 5new value 2 - 6new value 2 - 7
4
5
6
7
8
9
10
11new value 10 - 1new value 10 - 2new value 10 - 3new value 10 - 4new value 10 - 5new value 10 - 6new value 10 - 7
12
13
14
15
16
17
18
19new value 18 - 1new value 18 - 2new value 18 - 3new value 18 - 4new value 18 - 5new value 18 - 6new value 18 - 7
20
21
22
23
24
All Data


But if you use this:

VBA Code:
Sub IncrementRowNumbers()
    Dim i As Integer, j As Integer
    Dim replaceNum As Integer
    Dim myArray As Variant
    Dim myRange As Range
    Set myRange = Worksheets("All Data").Range("A2:G3001")
  
    myArray = myRange.Formula
    replaceNum = 6
  
    For i = 1 To UBound(myArray, 1)
      For j = 1 To UBound(myArray, 2)
        If j <> 4 And j <> 5 Then
          myArray(i, j) = Replace(myArray(i, j), "6", replaceNum + Int((i - 1) / 6))
        End If
        If i Mod 8 = 3 Then
            myArray(i, j) = "new value " & i & " - " & j
        End If
      Next
    Next
  
    myRange.Formula = myArray
End Sub

You will get:

Libro1.xlsm
ABCDEFG
1
2
3
4new value 3 - 1new value 3 - 2new value 3 - 3new value 3 - 4new value 3 - 5new value 3 - 6new value 3 - 7
5
6
7
8
9
10
11
12new value 11 - 1new value 11 - 2new value 11 - 3new value 11 - 4new value 11 - 5new value 11 - 6new value 11 - 7
13
14
15
16
17
18
19
20new value 19 - 1new value 19 - 2new value 19 - 3new value 19 - 4new value 19 - 5new value 19 - 6new value 19 - 7
21
22
23
24
All Data


The mod operator returns the remainder after number is divided by divisor. That is you divide i by 8 and get the reminder. So if you have you numbers and your divisor you will get:

NumberDivisorReminder
080
181
282
383
484
585
686
787
880
981
1082
1183
1284
1385
1486
1587
1680
1781
1882
1983
2084
2185
2286
2387
2480
2581
2682
2783
2884
2985
3086
3187
 
Last edited:
Upvote 0
How about?:

VBA Code:
    For i = 1 To UBound(myArray, 1)
        For j = 1 To UBound(myArray, 2)
          If j <> 4 And j <> 5 Then
            myArray(i, j) = Replace(myArray(i, j), "6", replaceNum + Int((i - 1) / 6))
          End If
        Next
        If i Mod 8 = 2 Then
            'Do something here
        End If
    Next

Or change the 2 to different numbers and see which one you need.

Thank you for taking the time to explain.

I have now got the If i Mod 8 = 2 Then part working for column H as a separate script but not as a whole with the original script.
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,129
Members
449,097
Latest member
mlckr

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