VBA code Split string error when Name is 1 month

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone, I have used the code below to split a string into an area. But when the string has the name of a month (from 1 to 12 ), when it appears it is a number. I want the output to remain the same as the original name. For example: August-1 -> August-1 (Currently error August-1 -> 45139 ). Thank you

Input test:
April / August-1 / AAA / December / BBB-1 / June-1 / June-2 / January / August-2 / August-3 / BBB-2 / February / June-3 / June-4 / June-5 /

1687106952453.png


VBA Code:
Sub codeteset()
    On Error Resume Next
    Range("d4:d1000").ClearContents
    Dim arr(1 To 1000, 1 To 1), Tmp, Tem, Str As String
    Dim I As Long, J As Long, K As Long, c As Long, Col As Long
    Str = Range("d2").Value  ' input
    Tmp = Split(Str, " / "): c = UBound(Tmp): K = 1
    For I = 0 To c
        Tem = Split(Tmp(I), "*"): Col = 1
        For J = 0 To 1
            arr(K, Col) = Tem(J)
            Col = Col + 1
        Next J
        If Col > 1 Then
            Col = 1: K = K + 1
        End If
    Next I
    Range("d4").Resize(K, 1) = arr 'output
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try
VBA Code:
With Range("d4").Resize(K, 1)
    .Numberformat = "@"
    .Value = arr
End With
 
Upvote 1
@Excelpromax123 Was thinking similar tp @Fluff but will format the whole range of potential.
Not sure if it is typical but have added a final space to input string to prevent the last string being returned as "June-5 /"

VBA Code:
Sub codeteset()
    On Error Resume Next
    Range("d4:d1000").ClearContents
    Range("d4:d1000").NumberFormat = "@"   '<<<<<<<
    Dim arr(1 To 1000, 1 To 1), Tmp, Tem, Str As String
    Dim I As Long, J As Long, K As Long, c As Long, Col As Long
    Str = Range("d2").Value & " "  ' <<<<<<<<  added space ???????
    Tmp = Split(Str, " / "): c = UBound(Tmp): K = 1
    For I = 0 To c
        Tem = Split(Tmp(I), "*"): Col = 1
        For J = 0 To 1
           arr(K, Col) = Tem(J)
            Col = Col + 1
        Next J
        If Col > 1 Then
            Col = 1: K = K + 1
        End If
    Next I
    Range("d4").Resize(K, 1) = arr 'output
End Sub
 
Upvote 1
@Excelpromax123 Was thinking similar tp @Fluff but will format the whole range of potential.
Not sure if it is typical but have added a final space to input string to prevent the last string being returned as "June-5 /"

VBA Code:
Sub codeteset()
    On Error Resume Next
    Range("d4:d1000").ClearContents
    Range("d4:d1000").NumberFormat = "@"   '<<<<<<<
    Dim arr(1 To 1000, 1 To 1), Tmp, Tem, Str As String
    Dim I As Long, J As Long, K As Long, c As Long, Col As Long
    Str = Range("d2").Value & " "  ' <<<<<<<<  added space ???????
    Tmp = Split(Str, " / "): c = UBound(Tmp): K = 1
    For I = 0 To c
        Tem = Split(Tmp(I), "*"): Col = 1
        For J = 0 To 1
           arr(K, Col) = Tem(J)
            Col = Col + 1
        Next J
        If Col > 1 Then
            Col = 1: K = K + 1
        End If
    Next I
    Range("d4").Resize(K, 1) = arr 'output
End Sub
Thank you !
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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