code only works when there is more than 1 cell within the specified range containing data...

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
VBA Code:
Dim vA1, vA2
    With Worksheets("ANALYTICS")
        vA1 = .Range("P1", .Cells(Rows.Count, "P").End(xlUp))
        ReDim vA2(1 To UBound(vA1), 1 To 2)

        For vN = 1 To UBound(vA1)
            vA2(vN, 1) = Split(vA1(vN, 1), ":")(0)
            vA2(vN, 2) = Trim(Split(vA1(vN, 1), ":")(1))
        Next vN
        .Range("Q1").Resize(UBound(vA2), 2) = vA2
    End With

what do I need to do to make it handle when there is just ONE cell with data? (i already have an If statement before this code starts so that "if" there are NO cells that contain anything, then it bypasses it completely and jumps down to the Else... but, this doesn't address the situations where these is 1 cell with something.)

In most circumstances there is almost always going to be more than 1, but, occasionally it encounters a situation where there is only 1, and when that happens it doesnt work. ?

(this is what it sees when there is only one cell within "P" with any data.)
1.xlsm.png

usually it encounters a range that looks like this (column "J" this time instead of "P") and it works fine and splits the the cell contents into 2 columns at the " : " location:
2.xlsm.png


So I realize that it's right here when it encounters this:

VBA Code:
ReDim vA2(1 To UBound(vA1), 1 To 2)

that it crashes as theres only 1 so obviously it cant go 'To 2', but I can't figure out how to make it work where there is only 1 (putting in another "If" statement to just skip it wont work either as I still need the code to do just spilt the one cell into two parts like shown above.)

as usual, thanks in advance for any help!! (y)
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
    With Worksheets("ANALYTICS")
        vA1 = .Range("P1", .Cells(Rows.Count, "P").End(xlUp))
        If IsArray(vA1) Then
           ReDim vA2(1 To UBound(vA1), 1 To 2)
   
           For vN = 1 To UBound(vA1)
               vA2(vN, 1) = Split(vA1(vN, 1), ":")(0)
               vA2(vN, 2) = Trim(Split(vA1(vN, 1), ":")(1))
           Next vN
           .Range("Q1").Resize(UBound(vA2), 2) = vA2
        Else
            .Range("Q1").Resize(, 2) = Array(Split(vA1, ":")(0), Trim(Split(vA1, ":")(1)))
      End If
    End With
 
Upvote 0
Solution
How about
VBA Code:
    With Worksheets("ANALYTICS")
        vA1 = .Range("P1", .Cells(Rows.Count, "P").End(xlUp))
        If IsArray(vA1) Then
           ReDim vA2(1 To UBound(vA1), 1 To 2)
  
           For vN = 1 To UBound(vA1)
               vA2(vN, 1) = Split(vA1(vN, 1), ":")(0)
               vA2(vN, 2) = Trim(Split(vA1(vN, 1), ":")(1))
           Next vN
           .Range("Q1").Resize(UBound(vA2), 2) = vA2
        Else
            .Range("Q1").Resize(, 2) = Array(Split(vA1, ":")(0), Trim(Split(vA1, ":")(1)))
      End If
    End With
YES! perfect. thanks so much, fluff
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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