UBound error - Subscript out of range

Doug_James

New Member
Joined
Feb 28, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello:

I have a range of data that is dynamic. Rows and columns change in size all the time. I previously had the code to look only for Column O and update this column (delete certain characters). When I created this code I didn't realize Column O sometimes moves. It could be any column. So what I need to do now is look for the header name "BASIS" (originally known as Column O) and do the rest. While this is recognizing the correct column, when I get to Cells(r, (ColNum)) = arr(i - 1), I get the error "Subscript out of range".

Any help is appreciated, this is what I have so far:

VBA Code:
Sub FormatBasis()

Dim SelRange As Range
Dim ColNum As Integer
Dim CWS As Worksheet, TmpWS As Worksheet

Set CWS = ActiveSheet
ColNum = Application.WorksheetFunction.Match("BASIS", CWS.Rows(1), 0)

Set SelRange = CWS.Columns(ColNum)

Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
       
Dim lr As Long
Dim r As Long
Dim arr() As String
Dim i As Long
   
    lr = Cells(Rows.Count, (ColNum)).End(xlUp).Row
   
    For r = 2 To lr
        If Cells(r, (ColNum)) <> "" Then
            arr = Split(Cells(r, (ColNum)), ";")
            'If Not IsEmpty(arr) Then
                i = UBound(arr)
                'End If
            Cells(r, (ColNum)) = arr(i - 1) 'HERE I'M GETTING STUCK
        End If
    Next r

End Sub

EDIT: Forgot to mention, Column "BASIS", sometimes is populated and some other times is blank
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Let's try adding in a few things, such as making sure that there is data (last row is greater than 1), and there is more than one object in your array (since you seem to want to pull the second from last value), i.e.
Rich (BB code):
Sub FormatBasis()

Dim SelRange As Range
Dim ColNum As Integer
Dim CWS As Worksheet, TmpWS As Worksheet

Set CWS = ActiveSheet
ColNum = Application.WorksheetFunction.Match("BASIS", CWS.Rows(1), 0)

Set SelRange = CWS.Columns(ColNum)

Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
      
Dim lr As Long
Dim r As Long
Dim arr() As String
Dim i As Long
  
    lr = Cells(Rows.Count, (ColNum)).End(xlUp).Row

'   Exit sub if no data
    If lr < 2 Then Exit Sub

    For r = 2 To lr
        If Cells(r, (ColNum)) <> "" Then
            arr = Split(Cells(r, (ColNum)), ";")
            'If Not IsEmpty(arr) Then
                i = UBound(arr)
                'End If
            If i > 0 Then
                Cells(r, (ColNum)) = arr(i - 1)
            End If
        End If
    Next r

End Sub
 
Upvote 0
Solution
Let's try adding in a few things, such as making sure that there is data (last row is greater than 1), and there is more than one object in your array (since you seem to want to pull the second from last value), i.e.
Rich (BB code):
Sub FormatBasis()

Dim SelRange As Range
Dim ColNum As Integer
Dim CWS As Worksheet, TmpWS As Worksheet

Set CWS = ActiveSheet
ColNum = Application.WorksheetFunction.Match("BASIS", CWS.Rows(1), 0)

Set SelRange = CWS.Columns(ColNum)

Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
     
Dim lr As Long
Dim r As Long
Dim arr() As String
Dim i As Long
 
    lr = Cells(Rows.Count, (ColNum)).End(xlUp).Row

'   Exit sub if no data
    If lr < 2 Then Exit Sub

    For r = 2 To lr
        If Cells(r, (ColNum)) <> "" Then
            arr = Split(Cells(r, (ColNum)), ";")
            'If Not IsEmpty(arr) Then
                i = UBound(arr)
                'End If
            If i > 0 Then
                Cells(r, (ColNum)) = arr(i - 1)
            End If
        End If
    Next r

End Sub
Thanks Joe4, this works perfectly.
 
Upvote 0
You are welcome!
Glad I was able to help.
:)
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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