Problem on double loop VBA

9thOctober

New Member
Joined
Nov 21, 2022
Messages
1
Office Version
  1. 2021
Hello i have this code

VBA Code:
For X = 9 To 36
    For Y = 8 To 36 step 14
            
If WS1.Cells(X, 1) = "TOT" Then X = X + 2
    
SUM = WorksheetFunction.SumIfs(Prova1, Prova2, WS1.Cells(X, 1), Date, WS1.Cells(Y, 2), Prova3, Prova4)
                  
WS1.Cells(X, 2) = SUM

    Next Y
Next X

But it doesnt work, the selection should be

Code:
x=9  Y=8
x=10    Y=8
x=11    Y=8
x=12    Y=8
x=13    Y=8
x=14    Y=8
x=15    Y=8
x=16    Y=8
x=17    Y=8
x=18    Y=8
x=19    Y=8
x=20    Y=8 (it skips)
x=23    Y=22
x=24    Y=22
x=25    Y=22
x=26    Y=22
x=27    Y=22
x=28    Y=22
x=29    Y=22
x=30    Y=22
x=31    Y=22
x=32    Y=22
x=33    Y=22
x=34    Y=22
x=35    Y=22

But with that code it is always Y = 22, the values of Y can be fixed are always 8,22,36 and so on so basically adding 14 on the values before

Im trying to fix a value on Y based on X

It must skip from X 20 to X 23, from X 34 to 37 and so on always +14 till forever
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Didn't study your code because I'm pretty sure that you cannot alter the start, step or end of a loop once it has been initialized. You will probably have to resort to using Do While x =,>,< or whatever operator you want to use. Or you can use Do Until. Either of these would allow you to base decisions on a modified value of X.
If using a Do loop, step through the procedure at least the first time (perhaps on a small data set) to ensure it ends when you expect it to.
 
Upvote 0
Hi,

this is what I think you may want. Test it on a sheet as it will only display the numbers in Columns B and C from Row 9 to the last entry in Column A:

VBA Code:
Public Sub MrE_1222760_1614516()
' https://www.mrexcel.com/board/threads/problem-on-double-loop-vba.1222760/
' Created: 20221121
' By:      HaHoBe

Dim lngCounter            As Long       'Looper
Dim lngItem               As Long       'Value to hold for number of cells
Dim lngHelp               As Long       'next change for looper and Item
Dim lngWrite              As Long       'may change for writing to cells like looper or continuously
Dim blnWithoutBlanks      As Boolean    'switch between continous(wiithout bkanks to normal looper
Dim ws1                   As Worksheet  'short for worlking sheet

lngItem = 8
lngWrite = 9
lngHelp = 20
blnWithoutBlanks = False

Set ws1 = ActiveSheet
With ws1
  For lngCounter = 9 To .Cells(.Rows.Count, "A").End(xlUp).Row
    If blnWithoutBlanks Then
      lngWrite = lngWrite + 1
    Else
      lngWrite = lngCounter
    End If
  ' commented as I do not know what to use the formula for
  '  Sum = WorksheetFunction.SumIfs(Prova1, Prova2, ws1.Cells(lngCounter, 1), Date, ws1.Cells(lngItem, 2), Prova3, Prova4)
    
    .Cells(lngWrite, 2) = lngCounter
    .Cells(lngWrite, 3) = lngItem
    If lngCounter = lngHelp Then
      lngCounter = lngCounter + 2
      lngHelp = lngCounter + 12
      lngItem = lngItem + 14
    End If
  Next lngCounter
End With
Set ws1 = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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