Attached as a snippet. In the original text I had a few extra DIMs
Main Topics
Browse All TopicsHi All
I am trying to concatenate columns. The columns to concatenate is a variable. Sometimes it can be 10 and sometines 30.
I saw this:
Sub ConcatColumns()
Dim count As Integer
Dim ccol As String
Dim rrow As String
Dim rowcol As String
ccol = 0
rrow = 0
start1 = InputBox("Enter the # of columns to concatenate. Make sure you are in the 1st cell in the first row and column you want to concatenate.", "Concatenation Thingy")
Do While ActiveCell <> "" 'Loops until the active cell is blank.
ActiveCell.Offset(0, 0).FormulaR1C1 = _
ActiveCell.Offset(0, 0) & "," & ActiveCell.Offset(0, 1)
ActiveCell.Offset.Range(0
ActiveCell.Offset(1, 0).Select
ccol = ccol + 1
Loop
End Sub
I don't know how to get it so I can use the input variable to make the offsets work right. I could enter a start and stop column?
I am running out of brain juice..lol
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
That code isn't valid, but I get the general idea.
It looks like what you are looking for is something that will concatenate and pad in blanks where needed.
basically, what needs to happen is each field needs to be inspected to find the largest string length for each column, then when appending the fields together, pad the necessary blanks onto the end of each field so that all the fields line up nicely.
Unfortunately It will be painfully slow if you are processing more than a few dozen rows and columns because every field will need to be inspected for string length, then compared to an array to see if the cell contains a string that is larger current largest string for that column.
maybe another expert can help by actually providing a function that does this, or possibly suggest a more efficient means to accomplish what you want.
I am actually putting a comma between each field. I ran this small snippet in Excel as a test with 2 columns and it was fast. I am trying to figure out how to use an input variable to control the # of offsets that take place.
For instance I want to concatenate 10 columns so I want to offset 10 times and add the comma.
I can manually write them out but was looking for help on a loop function across the row that could satisfy this and add the comma.
Thanks for...for uh...yeah, I can wait until the next expert. Smiles
Hi, try this macro:
Sub ConcatColumns()
Dim count As Integer
Dim ccol As Integer
Dim crow As Integer
start1 = InputBox("Enter the column letter to start concatenation from (inclusive):", "Start Column")
end1 = InputBox("Enter the column letter to end concatenation at (inclusive):", "End Column")
'MsgBox Asc(start1) - 64
' Insert a blank column at the end for concatenation
Columns(Chr(Asc(end1) + 1) & ":" & Chr(Asc(end1) + 1)).Insert Shift:=xlToRight
For crow = 2 To Cells(65536, start1).End(xlUp).Row
For ccol = (Asc(start1) - 64) To (Asc(end1) - 64)
If ccol = (Asc(start1) - 64) Then
Cells(crow, Asc(end1) - 63).FormulaR1C1 = _
Cells(crow, ccol).FormulaR1C1
Else
Cells(crow, Asc(end1) - 63).FormulaR1C1 = _
Cells(crow, Asc(end1) - 63).FormulaR1C1 & _
"," & Cells(crow, ccol).FormulaR1C1
End If
Next
Next
End Sub
Regards,
Rob.
Hi FUROG,
Here's one way to do it but the attached code snippet uses arrays for processsing and is much faster with a large number of rows (15x faster with 900 rows). Try them both and let me know if they work for you.
Sub ConcatColumns()
Dim val As Variant, cel As Range, icount As Long, icols As Long, irows As Long
icols = 0
Do While icols = 0
val = InputBox("Enter the # of columns to concatenate. Make sure you are in the 1st cell in the first row and column you want to concatenate.", "Concatenation Thingy")
If val = "" Then
MsgBox "Process canceled. Exiting routine."
Exit Sub
End If
On Error Resume Next
icols = CLng(val)
On Error GoTo 0
If icols = 0 Then
MsgBox "Please enter the number of columns."
End If
Loop
Application.ScreenUpdating
Set cel = ActiveCell
For irows = cel.Row To Cells(Rows.count, cel.Column).End(xlUp).Row
For icount = 1 To icols - 1
If cel.Offset(0, icount) <> "" Then
cel = cel & "," & cel.Offset(0, icount)
End If
Next icount
Set cel = cel.Offset(1)
Next irows
Application.ScreenUpdating
End Sub
Jim
Business Accounts
Answer for Membership
by: jeffldPosted on 2008-02-07 at 15:08:30ID: 20846334
Can you repost the code as a code snippet? It looks like something is missing.