VBA: Run-time Error '9': Subscript out of range

TheCobbler

New Member
Joined
Aug 21, 2021
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hi - Getting the above error when I try to use CORfname that I have as a global variable from a different module which captures a path. (Public CORfname As Variant)
Can see that CORfname in this module is finding the right path in the immediate window so not sure what the issue is. Any ideas? Cheers, Cobb

Workbooks(CORfname).Activate: This line highlights when debugging.

VBA Code:
Option Explicit

Sub L_Shop_Check()

Workbooks(CORfname).Activate
Sheets("To Order").Activate

'Deletes Blank Rows

    On Error Resume Next
    Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
    
    Dim SkuRng As Range
    Dim SkuCell As Range
    
'Inserts Sku Preface

    Range("B1").EntireColumn.Insert

    Set SkuRng = Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)

        For Each SkuCell In SkuRng

            If Not IsEmpty(SkuCell) Then

                SkuCell.Offset(, -1) = "_" & SkuCell

            End If
        Next

    Cells(1, 2) = "Merged SKU"
    
    
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You do not include a path when referring to a workbook by name in the Workbooks collection.
 
Upvote 0
Ok I understand. Is the best way to proceed to extract the Workbook name and then reference that? Or is there a way to activate the Workbook based on path? Thanks
 
Upvote 0
It somewhat depends on how you get the filename to start with. It may be easier to obtain the path and filename separately to start with, or it may be easier to extract the filename later. It may also be easier, if the code opens the workbook, to store a reference to that Workbook object in another variable.
 
Upvote 0
Cool. I'm currently getting the name when a user chooses a file, utilising the following code in a separate module if that sheds any light.

VBA Code:
Public CORfname As Variant

Sub B_Check_Sales()

'Allows selection of the Checkout Report

    CORfname = Application.GetOpenFilename(FileFilter:="Excel Workbooks,*.*", Title:="Open the Checkout Report...", MultiSelect:=False)
    
        If CORfname <> False Then
            Workbooks.Open Filename:=CORfname

        End If
End Sub
 
Upvote 0
In that case, I'd use a Workbook object:

Code:
Public SourceWorkbook as Workbook

and then amend that code to use:

Code:
Set SourceWorkbook = Workbooks.Open(Filename:=CORfname)

You can then simply refer to SourceWorkbook whenever you need that workbook as an object.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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