2 Loops - how to reset 2nd counter J to initial value for next 1st counter i

Viewed 34

Objective- TO modify the code for automatically resetting 2nd counter "j" to initial value once all iteration are completed for next value of 1st counter "i". I Have 2 Arrays. A & B are List of stock scrips. A is compared with B. B has nearly 200 items. Numbers of items in A may range between 20 - 100. Counter i is used for A array. Counter j is used for B array. For Ist item of array A, counter j runs from initial value (say 13 i.e. row number) up to 200th item. I am looking to reset counter j to an initial value (say 13 i.e. linked to row number) for the next item in A array. When cell 'B9 & D9'(in 'Match F' sheet) are 9 & 135 and If list of Array B (i.e. D13:D in 'Match F' sheet) is having 135 items, then for next loop j counter should initiate to 13(i.e. starting row number of range D13:D).

I seek help. How to initialize 2nd loop value. If this value is greater than maximum value, I want it to reset to initial value.

Final Output is collected in 'List' sheet. This gets wrong results, as j counter keeps running, and initial items are overlooked by code for next i counter.

Sorting Symbols

Complete Code is in my original question, raised to fix errors for copying and pasting a selected range to a new sheet with help of a Loop inside another Loop. Pl. refer the link below - Original Post

line 45 to 60. of original code which i modified for counter i & j are as follows -

function matchnsort() {
  
  if(cellAVal === ""){                                                       // final Check Scrip-1 values copied to desired cell for 1st run 
  
       
    Logger.log(rowB);
  }
  else{                                                                       
    for (i=cellAVal; i<rows_lenA; i++){                                      // Loop-1 for Scirp-1 
      // set the number i on the sheet  into the cell 'Match F!B16'    
      var dataOutRang1  = rank.getRange(i,col1) ;
      var dataOut1      = dataOutRang1.getDisplayValue() ;
      scrip1.setValue(dataOut1);
      
      Logger.log(rows_lenB) ;
      for (j=cellBVal; j<rows_lenB; j++){                                    // Loop-2 for Scirp-2 
        if(j > lenB || cellBVal === "" ){
          j= lenB ; 
          Logger.log(cellBVal); 
        }
        else{
          j=cellBVal ;
          Logger.log(cellBVal);  
        }  
          // set the number j on the sheet  into the cell 'Coint!H4'
          var dataOutRang2 = pf_sheet.getRange(j,col2) ;
          var dataOut2 = dataOutRang2.getValue() ;                             // *** Array Values for appending in 'List' Sheet
          Logger.log(dataOut2) 
          Logger.log(j) ;
          
          scrip2.setValue(dataOut2)    
         
          cellB.setValue(j);                                              // Record last j value - to used in next cycle
          Logger.log(j);
          }
          
      cellA.setValue(i);                                                  // Record last i value - to used in next cycle 
      }
      
    }        
  }
}

Pl refer original post for detailed code. Pl. explore spreadsheet and code and ask for more clarification if you require.

Debug report

0 Answers
Related