VBA - concatenate with a loop

A

adam cook

Hi guys
I have two macro's.
one tocheck for a status and act accordingly and one to split a full name into three parts.

View the code below:

Sub ConsCodeV3()
Dim c As Range
For Each c In Range("E1:E20")
If c <> "CONS" Then
c.Offset(, -3) = UCase(c.Offset(, -2))
c.Offset(, -4) = "ConsCode"
End If
If c = "CONS" Then
c.Offset(, -3) = UCase(c.Offset(, -2))
End If
Next c
End Sub

Sub NameSplit()
Dim rng As Range

For Each rng In Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
Range(Cells(rng.Row, rng.Column + 10), Cells(rng.Row, rng.Column + 13)) = Split(rng, " ")
Next rng
End Sub

Now what i want to do is the split names, i want to concatenate them in a specific way. I need to take the last name and truncate down to 4 letters, then add the initials of the first name and any other initials remainging. This then needs to go into Column A.

this will only want to happen for those that have "ConsCode" in column A following the ConsCodeV3() macro.

Is this easily done?


Submitted via EggHeadCafe - Software Developer Portal of Choice
..NET - Writing a self-updating application in C#
http://www.eggheadcafe.com/tutorial...9df7-85ec9bf2e619/net--writing-a-selfupd.aspx
 
J

joel

Use a function and pass the variables that are required. In th
function set the return value back to to function name.


Sub ConsCodeV3()
Dim c As Range
For Each c In Range("E1:E20")
If c <> "CONS" Then
c.Offset(, -3) = UCase(c.Offset(, -2))
ColB = c.offset(,-3)
ColL = c.offset(,+7)
ColO = c.offset(,+10)
c.Offset(, -4) = NameSplit(ColB,ColL,ColO)
End If

If c = "CONS" Then
c.Offset(, -3) = UCase(c.Offset(, -2))
End If
Next c
End Sub

Function NameSplit(ColB,ColL,ColO)
Dim rng As Range

NameSplit = left(ColB,4) & Coll & ColO
End Functio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top