Subscript out of Range when choosing a Workbook

MaxAttack102

When I tried to get excel to choose which workbook to use, it gives me an error for subscript out of range. I don't know whether I have to reference which folder it originates from. They are all in the same folder. I looked through other peoples solutions,but I don't have neither the same format nor task. The error is on the line where it assigns workbook numbers

Sub bringbookstogether()

Dim currentsheet As Worksheet
Set currentsheet = Application.ActiveSheet

Dim othersheets As Worksheet

Dim wbook As Workbook

Dim c As String

'assigns the number to start with

Dim a, b, d As Integer

a = 4
b = 6
d = 1

'assigns workbook numbers
If (d = 1) Then
    Set wbook = Workbooks("MaintPrep Sheet 1st")
    Else
    If (d = 2) Then
        Set wbook = Workbooks("MaintPrep Sheet 2nd")
        Else
        If (d = 3) Then
            Set wbook = Workbooks("MaintPrep Sheet 3rd")
        End If
    End If
End If

'End if it's done with all the workbooks

Do Until (d = 4)

'Looks for the sheet that has the same name

Do While (c = currentsheet.Name)

'Ends in row 99

Do While (b < 99)

'Ends in Column 52

Do While (a < 52)

currentsheet.Cells(b, a) = currentsheet.Cells(b, a) + Workbooks(d).Sheets(c).Cells(b, a)

a = a + 1
Loop

b = b + 1
Loop

Loop

d = d + 1
Loop

End Sub
Rik Sportel

First of all, it's better to use the full filename: Workbooks("MaintPrep Sheet 1st.xlsx") etc.

Second of all, this code will error as soon as one of the Workbooks you're trying to access is not currently opened. If a Workbook is not open, it doesn't exist within the current context and thus Excel will throw error 91.

To fix this, you could do:

Sub a()
Dim wb As Workbook

On Error Resume Next 'To avoid error 91
Set wb = Workbooks("MaintPrep Sheet 1st.xlsx")
On Error GoTo 0 'To avoid not seeing other errors.

If Not wb Is Nothing Then
    'Do stuff
    MsgBox "Opened!"
Else
    'Handle the fact that it's missing
    MsgBox "Not open!"
End If

'Alternatively, OPEN the workbook if you couldn't set it in the first place:
On Error Resume Next
Set wb = Workbooks("MaintPrep Sheet 1st.xlsx")
On Error GoTo 0

If wb Is Nothing Then
    Set wb = Workbooks.Open("C:\FullPath\MaintPrep Sheet 1st.xlsx")
    'Do stuff
End If

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Subscript out of range when referencing a worksheet in another workbook from a variable

From Dev

Subscript out of range when referencing a worksheet in another workbook from a variable

From Dev

VBA subscript out of range for workbook name

From Dev

Workbook_Open Subscript out of Range

From Dev

VBA Error: Runtime Error: 9 - Subscript out of range when copying a worksheet from another workbook

From Dev

Subscript out of range in VBA when running this code

From Dev

Subscript out of range error when referencing cells

From Dev

Subscript out of range when referencing to cell values

From Dev

VBA - Subscript out of range

From Dev

VBA - Subscript out of range

From Dev

Subscript out of range ,VBA

From Dev

VBA: Subscript out of range

From Dev

Referencing other workbook works for most but gets "subscript out of range" error for two machines

From Dev

Find cell address in another workbook with its value: "Run time error '9' Subscript out of range"

From Dev

"Subscript out of range" error when calling LBound() or UBound() on a VBA array

From Dev

Getting 'SubScript Out of Range' Error in VBA when calling class Method

From Dev

VBA Error '9' subscript Out of Range when copying worksheets

From Dev

"Subscript out of range" error when calling LBound() or UBound() on a VBA array

From Dev

"subscript out of range" error when trying to set secondary start page

From Dev

Subscript out of range when removing element from set

From Dev

Runtime Error Subscript out of range

From Dev

ReDim Preserve "Subscript Out of Range"

From Dev

error 9 subscript out of range

From Dev

Vector subscript out of range on iterator

From Dev

Subscript out of range, array variable

From Dev

sheet array subscript out of range

From Dev

Subscript out of range error for subroutine

From Dev

Error 9: Subscript out of range

From Dev

Subscript Out of Range Error in Code

Related Related

  1. 1

    Subscript out of range when referencing a worksheet in another workbook from a variable

  2. 2

    Subscript out of range when referencing a worksheet in another workbook from a variable

  3. 3

    VBA subscript out of range for workbook name

  4. 4

    Workbook_Open Subscript out of Range

  5. 5

    VBA Error: Runtime Error: 9 - Subscript out of range when copying a worksheet from another workbook

  6. 6

    Subscript out of range in VBA when running this code

  7. 7

    Subscript out of range error when referencing cells

  8. 8

    Subscript out of range when referencing to cell values

  9. 9

    VBA - Subscript out of range

  10. 10

    VBA - Subscript out of range

  11. 11

    Subscript out of range ,VBA

  12. 12

    VBA: Subscript out of range

  13. 13

    Referencing other workbook works for most but gets "subscript out of range" error for two machines

  14. 14

    Find cell address in another workbook with its value: "Run time error '9' Subscript out of range"

  15. 15

    "Subscript out of range" error when calling LBound() or UBound() on a VBA array

  16. 16

    Getting 'SubScript Out of Range' Error in VBA when calling class Method

  17. 17

    VBA Error '9' subscript Out of Range when copying worksheets

  18. 18

    "Subscript out of range" error when calling LBound() or UBound() on a VBA array

  19. 19

    "subscript out of range" error when trying to set secondary start page

  20. 20

    Subscript out of range when removing element from set

  21. 21

    Runtime Error Subscript out of range

  22. 22

    ReDim Preserve "Subscript Out of Range"

  23. 23

    error 9 subscript out of range

  24. 24

    Vector subscript out of range on iterator

  25. 25

    Subscript out of range, array variable

  26. 26

    sheet array subscript out of range

  27. 27

    Subscript out of range error for subroutine

  28. 28

    Error 9: Subscript out of range

  29. 29

    Subscript Out of Range Error in Code

HotTag

Archive