Subscript Error when using Split

CrispyAsian

Board Regular
Joined
Sep 22, 2017
Messages
64
Hey all,

I'm working on this code but everytime I run it, I get a subscript error on the .Value lines. I don't know what I'm doing wrong.

Code:
Private Sub ComboBox2_Change()


Range("F3:PO3").Clear


Dim SDates() As String
Dim CDates As String
Dim c As Range


Select Case ComboBox2
    Case Is = "AF CTSAC"
        For Each c In Worksheets("Sheet2").Range("B2:AZ2")
            If Not c Is Nothing Then
            CDates = Cells(2, c.Column).Value
            SDates = Split(CDates, " - ")
            Worksheets("Sheet1").Cells(21, 1).Value = SDates(0) 'These two lines are where I get the Subscript out of range error
            Worksheets("Sheet1").Cells(21, 2).Value = SDates(1)

            Else
            End If
        Next c
        
End Select


End Sub

I have the Select Case in there because I am going to be adding more option to what ComboBox2 could be equal to, but I want to get this part fixed before I do so. Anybody see what I'm not?
 
EQV seems interesting but wondered where you'd ever use IMP, should have deferred to "not worth effort researching them"! Thanks again
 
Upvote 0

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
That was my fault through an earlier change. Taking Rick's suggestion into account, try:
Code:
Private Sub ComboBox2_Change()

Dim SDates() As String
Dim y   As Long
Dim x   As Long

Application.ScreenUpdating = False

Range("F3:PO3").Clear
x = 21

Select Case ComboBox2
    Case Is = "AF CTSAC"
        With Sheets("Sheet2")
            For y = 2 To 52
                If Len(.Cells(2, y).Value) > 0 And InStr(.Cells(2, y).Value, " - ") Then
                    SDates = Split(Sheets("Sheet2").Cells(2, y).Value, " - ")
                    Sheets("Sheet1").Cells(x, 1).Resize(, UBound(SDates) + 1).Value = SDates
                    x = x + 1
                    Erase SDates
                End If
            Next y
        End With
End Select

Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
@JackDanIce So when trying this code, it literally did nothing. So I stepped into it to try to see why, ever time it jumps straight to the end if...so I guess nothing is matching the If statement?
 
Upvote 0
Try breaking out the AND part of that IF statement and see if you can narrow the cause down:
Code:
Private Sub ComboBox2_Change()

Dim SDates() As String
Dim y   As Long
Dim x   As Long

Application.ScreenUpdating = False

Range("F3:PO3").Clear
x = 21

Select Case ComboBox2
    Case Is = "AF CTSAC"
        With Sheets("Sheet2")
            For y = 2 To 52
                If Len(.Cells(2, y).Value) > 0 Then
                    If InStr(.Cells(2, y).Value, " - ") Then
                        SDates = Split(Sheets("Sheet2").Cells(2, y).Value, " - ")
                        Sheets("Sheet1").Cells(x, 1).Resize(, UBound(SDates) + 1).Value = SDates
                        x = x + 1
                        Erase SDates
                    End If
                End If
            Next y
        End With
End Select

Application.ScreenUpdating = True

End Sub
Does your combo box selection match the select statement value?
 
Last edited:
Upvote 0
@JackDanIce So when trying this code, it literally did nothing. So I stepped into it to try to see why, ever time it jumps straight to the end if...so I guess nothing is matching the If statement?
That is because JackDanIce omitted the ">0" that I noted was needed. Try this (his code with the ">0" included)...
Code:
Private Sub ComboBox2_Change()

Dim SDates() As String
Dim y   As Long
Dim x   As Long

Application.ScreenUpdating = False

Range("F3:PO3").Clear
x = 21

Select Case ComboBox2
    Case Is = "AF CTSAC"
        With Sheets("Sheet2")
            For y = 2 To 52
                If Len(.Cells(2, y).Value) > 0 And InStr(.Cells(2, y).Value, " - ") [B][COLOR="#FF0000"]> 0[/COLOR][/B] Then
                    SDates = Split(Sheets("Sheet2").Cells(2, y).Value, " - ")
                    Sheets("Sheet1").Cells(x, 1).Resize(, UBound(SDates) + 1).Value = SDates
                    x = x + 1
                    Erase SDates
                End If
            Next y
        End With
End Select

Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
@JackDanIce so you're a genius and I was being really dumb. I moved all my data to row 3 because I had to add an extra row to sheet 2 and just forgot about it. So it was running on a completely blank row which is why it wasn't displaying anything. Your code works magnificently. I can't thank you enough for all the help!
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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