With Workbooks(xxx) - Subscript out of Range?!

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hey guys,

I can't get my head round this, doesn't help that it's Friday afternoon and it's roasting outside...

Problem part is bolded.

It runs fine when I use it in the main sheet, but when I try using a WITH it ****s up..

Code:
Option Explicit
Dim lRow, i, j As Integer
Dim FileToOpen As String
Sub test()
    Application.ScreenUpdating = False

    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose the latest APO file", _
    FileFilter:="Excel Files *.xls (*.xls),")
    
    If FileToOpen = "" Then
        MsgBox "No file specified."
        Exit Sub
    Else
        Workbooks.Open Filename:=FileToOpen
    End If

    [U][B]With Workbooks(FileToOpen).Sheets("Sheet1")[/B][/U] 'OOR ERROR
        lRow = .Range("AL" & Rows.Count).End(xlUp).Row
        For i = 2 To lRow
            For j = 38 To 193
                If .Cells(i, j).Value <> 0 Then
                    If j = 38 Then
                        .Range("AK" & i).Value = 0
                        Exit For
                    Else
                        .Range("AK" & i).Value = Cells(1, j).Value
                        Exit For
                    End If
                Else
                    .Range("AK" & i).Value = 0
                End If
            Next j
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try like this

Rich (BB code):
Dim MyBook As WorkBook
 
If FileToOpen = "" Then
        MsgBox "No file specified."
        Exit Sub
    Else
        Set MyBook = Workbooks.Open(Filename:=FileToOpen)
    End If
 
    With MyBook.Sheets("Sheet1")

Hope that helps.
 
Upvote 0
This may not be the answer you want, but your code implies (I think!) the workbook FileToOpen will be (at that stage of excecution) the active workbook so have you tried changing your WITH statement to just?
Code:
With Sheets("Sheet1")
 
Upvote 0
Brilliant, thanks Jonmo.

Jack, good point, I didn't think about that. I'll add them both to my ever growing VBA reference folder ;-)
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top