View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Leith Ross[_2_] Leith Ross[_2_] is offline
external usenet poster
 
Posts: 128
Default Interop.Excel Split and Copy

On Aug 21, 8:50 am, krayakin
wrote:
I'm trying to create a small plugin for Excel that will take a column (the
current selected column) and split the text into seperate columns based on
spaces.

I have the code to select the current column (although it is taking the
entire column, not the used range, any suggestions on that as well would be
nice)

Code Snippet

if (Application.ActiveWindow.Selection is Excel.Range)
{
Application.ScreenUpdating = false;
Excel.Range range = Application.ActiveWindow.Selection as Excel.Range;
range = range.EntireColumn;

}

I'm not exactly sure how to proceed from here. Below is a primitive example
of what I'd like to see.

Before
|ColA |
|Foo Bar |
|smarmy |
|Split Me|

After

|ColA |ColB |
|--------|--------|
|Foo |Bar |
|smarmy | |
|Split |Me |


Hello Krayakin,

Here is the macro to split the data into columns. It automatically
finds the last entry in column "A". It also will not go beyond the
maximum number of columns on the worksheet.

Sub SplitIntoColumns()

Dim C As Long, R As Long
Dim ColCount As Long, LastRow As Long
Dim Cols As Variant

With ActiveSheet
ColCount = .Columns.Count
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For R = 1 To LastRow
Cols = Split(Cells(R, "A"), " ")
LastCol = UBound(Cols)
If LastCol ColCount Then LastCol = ColCount
For C = 0 To LastCol
Cells(R, C + 1).Value = Cols(C)
Next C
End If
Next R

End Sub

Sincerely,
Leith Ross