1

I am creating a function that reads column titles from two excel files and then place those column titles in checkboxes so the user can check which columns he will work with. It works when I do it with one file but when I modify it to work with two files I get the "run time error 9: Subscript out of range" and highlights the line => Set wks2 = ActiveWorkbook.Worksheets(SheetName2).

Even with this error message still works for the first file but it does not work with the second file. Can anybody help me to find the reason of this error message? Thank you very much in advance.

 Function CallFunction(SheetName1 As Variant, SheetName2 As Variant) As Long
' This is a function used to retrieve column titles and place them as checkboxes in a listBox

 Dim jTitles(200) As String
 Dim sTitles(200) As String
 Dim titless As Integer
 Dim titlesj As Integer
 Dim wks1 As Worksheet
 Dim wks2 As Worksheet
 Dim Item(200) As String

 SPathName = Range("F18").Value
 SFilename = Range("F19").Value

 JPathName = Range("F22").Value
 JFilename = Range("F23").Value

 Workbooks.Open Filename:=SPathName & "\" & SFilename
 Workbooks.Open Filename:=JPathName & "\" & JFilename

 Set wks1 = ActiveWorkbook.Worksheets(SheetName1)

 For j = 1 To 199
     If Trim(wks1.Cells(4, j).Value) = "" Then
        titlesj = j - 1
        Exit For
    End If
        jTitles(j - 1) = wks1.Cells(4, j).Value
 Next

 j = 1

 ' Add column titles from files into the listbox as checkboxes
 For j = 0 To titlesj
    Sheet1.ListBox1.AddItem jTitles(j)
    Sheet1.ListBox3.AddItem jTitles(j)
 Next

 Set wks2 = ActiveWorkbook.Worksheets(SheetName2)  ' <=== HERE POPS THE ERROR MESSAGE

 For s = 1 To 199
    If Trim(wks2.Cells(1, s).Value) = "" Then
        titless = s - 1
        Exit For
    End If
        sTitles(s - 1) = wks2.Cells(1, j).Value
 Next

 s = 1

 For s = 0 To titless
    Sheet1.ListBox2.AddItem sTitles(s)
    Sheet1.ListBox4.AddItem sTitles(s)
 Next

    Workbooks(JFilename).Close
    ' Workbooks(SFilename).Close

End Function
Community
  • 1
  • 1
Bart g
  • 587
  • 2
  • 13
  • 28
  • 1
    It seems that you are activating the workbook that does not contain the worksheet called SheetName2. – causita Jul 10 '14 at 00:34

1 Answers1

1

Subscript out of Range error arises in these circumstances when the specified sheetname does not exist in that workbooks Worksheets collection.

I notice you have two open workbooks specified by:

Workbooks.Open Filename:=SPathName & "\" & SFilename
Workbooks.Open Filename:=JPathName & "\" & JFilename

However, both of your worksheet assignments refer only to the ActiveWorkbook.

The cause of the error is certainly that SheetName2 does not exist in the ActiveWorkbok (which is specified by JFilename)

Especially when working with multiple books or worksheets, it is always preferable to avoid using Activate/Select methods-- otherwise you need to keep track of which workbook/worksheet/etc. is "Active", and that makes for spaghetti code and lots of unnecessary calls to the .Activate method.

I know that SheetName1 exists in JFilename, and I am assuming that SheetName2 exist in the workbook SFileName.

Instead, define two Workbook variables:

Dim wb1 as Workbook
Dim wb2 as Workbook

Assign the results of the Workbooks.Open method to these workbooks:

Set wb2 = Workbooks.Open(Filename:=SPathName & "\" & SFilename)
Set wb1 = Workbooks.Open(Filename:=JPathName & "\" & JFilename)

Now, wb1 is the "Active" workbook, so with the worksheet assignments:

Set wks1 = wb1.Worksheets(SheetName1)

And later for the Sheetname2:

Set wks2 = wb2.Worksheets(Sheetname2)

Otherwise, there is a typo in your worksheet names or the string parameters you're sending to this function. Doublecheck/debug that the value of SheetName2 is correct and that it exists.

Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130