VBA - Subscript out of range/Switching between workbooks

prinzip

New Member
Joined
Feb 7, 2012
Messages
34
Hi, i tried to solve a problem appeared to me before and another came up.
I have set a UserForm with ComboBox for multiple selection items. I use 4-5 subroutines in a row so the UserForm user to select a specific value. Depended ComboBox in other words. The error i get is that of "Subscript out of range". I know it has to do with the Activation/Selection proper use of the workbooks/sheets/rows/columns, but the various changes I've done all lead me to wrong results.
Particularly, if in the second subroutine instead of .select i use .activate, the third subroutine gives wrong results. In what way you switch among the open workbooks?
The subroutines i use:
i get the error in the second subroutine in the wb1 .Sheets("En_mob").Activate part.

Code:
'First subroutine
Sub ActivateBelgium()
    Set wb1 = Workbooks.Open(Filename:="C:\Users\Zyklon\Desktop\1st\BE_ACTIVITIES_MOBILE_new.xls")
    Windows("BE_ACTIVITIES_MOBILE_new.xls").Activate
    Set wb2 = Workbooks.Open(Filename:="C:\Users\Zyklon\Desktop\1st\BE_CONTROL_VINTAGE_new.xls")
    Windows("BE_CONTROL_VINTAGE_new.xls").Activate
    'Workbooks.Open Filename:="C:\Users\Zyklon\Desktop\1st\BE_EMFACTORS.xls"
End Sub

'Second Subroutine
Sub ActivateHDB()

    With wb1
        .Sheets("En_mob").Activate
        Range("G:G").Select
    End With
    
    With wb2
        .Sheets("Penetr_MOB_RD").Activate
        Rows("4:22").Select
    End With

End Sub

'Third Subroutine
Sub Activate1990()

    With wb1
        Selection.Rows("4:11").Select
    End With
    
    With wb2
        Selection.Columns(4).Select
    End With
    
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Do you actually want to select particular ranges?

Or are you doing something else? eg copying/pasting

Is worksheet 'En_mob' in workbook wb1, 'BE_ACTIVITIES_MOBILE_new.xls'?
 
Upvote 0
Do you actually want to select particular ranges?

Or are you doing something else? eg copying/pasting

Is worksheet 'En_mob' in workbook wb1, 'BE_ACTIVITIES_MOBILE_new.xls'?

yes, through the UserForm the user choose ranges and finally he is lead to one cell.

wb1,wb2 are workbooks,
"En_mob" worksheet of "BE_ACTIVITIES_MOBILE_new.xls"
 
Upvote 0
Where have you declared wb1 and wb2, where is this code and when do you call it?

If 'En_mob' is in that worksheet there should be no subscript out of range on that code.

Have you checked the name(s)?
 
Upvote 0
Where have you declared wb1 and wb2, where is this code and when do you call it?

If 'En_mob' is in that worksheet there should be no subscript out of range on that code.

Have you checked the name(s)?

wb1, wb2 are declared in the first subroutine.
The UserForm code:

Code:
Private Sub cmdCancel_Click()

    Unload Me
    
End Sub

Private Sub cmdConfirm_Click()

    Select Case ComboBox1.ListIndex

        Case 0: Call ActivateAustria
        Case 1: ...
      
    End Select
    
    Select Case ComboBox2.ListIndex
   
        Case 0: Call ActivateHDB
        Case 1: ....
    End Select
    
        Select Case ComboBox3.ListIndex
 
        Case 0: Call Activate1990
        Case 1: ....

    End Select
    
End Sub
The problem, after fixing the Subscript out of range part, is between subroutine 2 and 3. The code gets confused and does not apply the changes to the appropriate workbook. So, how inside the subroutine you select one of the two workbooks?
 
Upvote 0
If you've declared wb1 and wb2 in a sub they won't be recognised in any other subs.
 
Upvote 0
If you've declared wb1 and wb2 in a sub they won't be recognised in any other subs.

They are declared in the module as workbooks with the classic way (option explicit etc), and inside the first subroutine it is stated which workbooks they are (in the example, wb1 is "BE_ACTIVITIES_MOBILE_new.xls" and wb2 is "BE_CONTROL_VINTAGE_new.xls"). I set them as wb1 and wb2 in the first subroutine because i want them treated the same way in the rest of the subroutines.
 
Upvote 0
So you've checked all the names involved, including for spaces/types etc?
 
Upvote 0
So you've checked all the names involved, including for spaces/types etc?

Fixed problems, when the Subscript out of range error appeared, it had to do with naming issue, which has gotten off my attention due to the large code and the wrong selection of data had to do with activation or not of the appropriate worksheet. The correct codes, instead of the ones in the first post:

Code:
'First subroutine 
Sub ActivateBelgium()     
Set wb1 = Workbooks.Open(Filename:="C:\Users\Zyklon\Desktop\1st\BE_ACTIVITIES_MOBILE_new.xls")     Windows("BE_ACTIVITIES_MOBILE_new.xls").Activate     
Set wb2 = Workbooks.Open(Filename:="C:\Users\Zyklon\Desktop\1st\BE_CONTROL_VINTAGE_new.xls")     Windows("BE_CONTROL_VINTAGE_new.xls").Activate     
End Sub  
'Second Subroutine 
Sub ActivateHDB()

[B] wb1.Activate [/B]   
 With wb1         
.Sheets("En_mob").Activate         
Range("G:G").Select     
End With     

[B]wb2.Activate[/B]     
With wb2         
.Sheets("Penetr_MOB_RD").Activate         
Rows("4:22").Select     
End With  
End Sub  
'Third Subroutine 
Sub Activate1990() 
[B]wb1.Activate [/B]    
With wb1         
Selection.Rows("4:11").Select     
End With     
[B]wb2.Activate [/B]    
With wb2         
Selection.Columns(4).Select     
End With      
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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