Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

for each loop

ResolvedVersion 2016

For each loop

Can someone tell me why 'j' in my code below returns as "empty"?

Public Sub CreateChart()

Dim chartDataSheets() As Long
Dim numberCells As Integer
Dim arraySize As Integer
Dim i As Integer
Dim j As Variant
Dim chartData As Range
Dim Inp As String


With Application

.ScreenUpdating = False

Let Inp = ThisWorkbook.Sheets("CreateChart").Cells(3, 2).Value

Workbooks.Open (Inp)

numberCells = WorksheetFunction.CountA(ThisWorkbook.Worksheets("CreateChart").Columns(4)) + 1

arraySize = (numberCells - 2)

ReDim chartDataSheets(arraySize)

For i = 3 To numberCells

chartDataSheets((i - 2)) = ThisWorkbook.Worksheets("CreateChart").Cells(i, 4).Value

Next i

'MsgBox chartDataSheets(1)
'MsgBox chartDataSheets(2)

For Each j In chartDataSheets

ActiveWorkbook.Worksheets(j).Activate

chartData = ActiveWorkbook.Worksheets(j).UsedRange.Select

Next j

End With

End Sub

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Moving between split pane sections in a spreadsheet

If you have used the split panes feature in your worksheet, use the following keyboard shortcut keys to move quickly and effortlessly between paned sections:

F6 - Move to the next pane
Shift + F6 - Move to the previous pane

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.