2 Replies Latest reply: Jan 8, 2014 12:14 PM by Koushik Vutha RSS

    Loop sheets and Excel workbooks

    Koushik Vutha

      Hi All

       

      I am trying to loop sheets and Excel workbooks. The data structure in all the sheets in all the workbooks is the same but each workbook has different number of sheets. I tried to use the below code and it works when excel workbooks has same number of sheets but doesn't work if the number of sheets in each workbook are not the same

       

      Ex:  When Q_Excel1.xlsx has 4 sheets and Q_Excel2.xlsx has 5 sheets below code doesnt work but when Q_Excel1.xlsx has 4 sheets and Q_Excel2.xlsx has has 4 sheets it works.

       

       

       

      ODBC CONNECT TO [Excel Files;DBQ=C:\Desktop\Excel1.xlsx];

        tables:

        SQLTables;

        DISCONNECT;

        let var=NoOfRows('tables');

       

      Data:

      LOAD

           A,

           B,

           C,

           D,

      FROM

      [X:\Desktop\Q_*.xlsx]

      (ooxml, no labels, header is 17 lines, table is '$(sheetName)');

       

        FOR j = 0 to $(var)-1

      let sheetName=subfield(peek('TABLE_NAME', j,'tables'),'$',1);

       

      NEXT j

      DROP Table tables;

       

      Regards

      Koushik