9 Replies Latest reply: Sep 14, 2017 2:48 PM by Stefan Wühl

# Double Loop

Is it possible to do a double loop in qlik? one inside the other?

• ###### Re: Double Loop

You are talking about e.g. a FOR ... NEXT loop in the script?

An example for a nested loop can be found here:

For each..next ‒ Qlik Sense

• ###### Re: Double Loop

yes it is possible

Foe Ex;

For i = 0 to 10

For j = 0 to 5

Some code

Next

Next

• ###### Re: Double Loop

I'm trying to do the loop to get the name of tabs in a sheet so I can load all of the tabs in an excel sheet and the way the titles for the tabs are set up is 'MMM-YYYY'

• ###### Re: Double Loop

Ok, so how does your current code look like? Do you also have a sample Excel file to work with?

• ###### Re: Double Loop

Sheet names are in this formats MMM-YYYY is so it comes same name when you apply loop on the sheets.

• ###### Re: Double Loop

Set ErrorMode = 0;

NoConcatenate
TableName:
*
From x
(ooxml, embedded labels, table is Current)
Where Num(MonthYear)<>Null();

NoConcatenate
MaxMonthYear:
Max(Date) As TNMaxMY
Resident TableName;

Let vTNMaxMY=Peek('TNMaxMY');

Let vTNMaxMon=Num(Month(\$(vTNMaxMY)));

Let vTNMaxYear=Num(Year(\$(vTNMaxMY)));

Let vTNMonPrev=\$(vTNMaxMon)-1;

// First for loop
For Year=Start Year to \$(vTNMaxYear);

Let vYear=\$(Year);

//For Loop

For num=1 to 12

Let vMY=Date(MakeDate(\$(vYear),\$(num)),'MMM-YYYY');

Concatenate(TableName)
*
From x
(ooxml, embedded labels, table is [\$(vMY)])
Where Num(MonthYear)<>Null();

Next

Next

Drop Table MaxMonthYear;

If ScriptErrorCount > 0 then

End IF;

// ErrorMode set back to 1
Set ErrorMode = 1;

// Load DataRefresh.QVD to have previously loaded data and used Exit Script as part of error handling

Concatenate(TableName)
*
From x(qvd)
Where [TableName]='TableName' and Not Exists(Key);

This ended up to be the code that worked for me to get all the tabs in the excel file for whatever year and month and to be able to have missing months.

• ###### Re: Double Loop

"This ended up to be the code that worked for me to get all the tabs in the excel file for whatever year and month and to be able to have missing months."