NaturalTvven
08-12-2014, 09:56 AM
I have a VBA code that activates two different sheets in the same xlsx workbook. Or, at least I think it should. But the code below does not switch active sheets. The debug return text is always either 'Sheet1' repeated or 'Sheet5' repeated. Can someone help me understand why? This is running in powerpoint, by the way.
'open excel spreadsheet
Set xlApp = New Excel.Application
xlApp.Workbooks.Open xltbl, True, True
xlApp.Workbooks(1).Worksheets(xlssheet).Activate
q = ActiveSheet.UsedRange.Rows.Count
For r = 2 To q
xlApp.Workbooks(1).Worksheets("Sheet1").Activate
Debug.Print ActiveSheet.Name
'Change table
xlApp.Workbooks(1).Worksheets("Sheet5").Activate
Debug.Print ActiveSheet.Name
'open excel spreadsheet
Set xlApp = New Excel.Application
xlApp.Workbooks.Open xltbl, True, True
xlApp.Workbooks(1).Worksheets(xlssheet).Activate
q = ActiveSheet.UsedRange.Rows.Count
For r = 2 To q
xlApp.Workbooks(1).Worksheets("Sheet1").Activate
Debug.Print ActiveSheet.Name
'Change table
xlApp.Workbooks(1).Worksheets("Sheet5").Activate
Debug.Print ActiveSheet.Name