user2546749
user2546749

Reputation: 15

Printing multiple text strings from single cell

I'm currently using Office 2003 to create a calendar with department codes relating to certain departments. Each "event" on the schedule has its own set of dept codes hidden next to each date, and I am trying to print the corresponding string (there can be multiple dept codes for each "event"). I need help to do this.

Summary


Sub DepartmentNames()

Dim i As Long

Dim p As Integer

Dim LastRow As Long

LastRow = Range("D" & Rows.Count).End(xlUp).Row

For i = 10 To LastRow

    For p = 1 To Len("D" & i) Step 3

        ' Placeholder

    Next

Next i

End Sub

Upvotes: 1

Views: 230

Answers (1)

Julien Marrec
Julien Marrec

Reputation: 11895

Here is my proposed solution, using the Split function and a collection.

Sub Reference()

' Disable screen updating
Application.ScreenUpdating = False

Dim wS As Worksheet
Set wS = ActiveSheet   ' you can change it to be a specific sheet

Dim i As Long
Dim LastRow As Long
LastRow = Range("D" & Rows.Count).End(xlUp).Row


Dim Dpts As Variant
Dim dFullText As Variant
Dim LookUp As New Collection

' Create a collection where the key is the shortcode and the value is the full name of the dpt
On Error Resume Next
For i = 3 To 11

    LookUp.Add wS.Cells(i, 17), wS.Cells(i, 16)

Next i
On Error GoTo 0


' Loop on each row
For i = 10 To LastRow

    Dpts = Split(wS.Cells(i, 4), ",") ' Split creates an array

    ' First case
    dFullText = LookUp.Item(Trim(Dpts(0)))   ' TRIM = remove trailing and leading spaces

    ' The rest of them
    For j = 1 To UBound(Dpts)

        dFullText = dFullText & ", " & LookUp.Item(Trim(Dpts(j)))

    Next j

    ' Put full text in column K
    wS.Cells(i, 11).Value = dFullText

Next i

' Enable screen updating again
Application.ScreenUpdating = True

End Sub

Let me know if you need clarification

Upvotes: 1

Related Questions