Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split Cells in Excel 2010 using VBA 1

Status
Not open for further replies.

JasonEnsor

Programmer
Sep 14, 2010
193
GB
Hiya,

I am currently trying to process some data in excel that i need to split in to multiple rows. I have the following piece of code that will loop through the data and split it at the delimeter however i want the value before the delimeter in A2 for example and the value after the delimeter in B2. This currently only gets the data before the delimeter

Code:
 Do Until IsEmpty(ActiveCell)
        
        X = Split(ActiveCell.Value, "-")
        ActiveCell.Offset(, -1).Resize(UBound(X)).Value = Application.Transpose(X)
        ActiveCell.Value = X
        ActiveCell.Offset(1, 0).Select

      Loop

Any ideas, really struggling with this?

Many thanks in advance

Jason
 


hi,

My advise: AVOID the Select and Activate methods as a programming method!
Code:
    Dim r As Range, X
    
    For Each r In Range(Cells(2, "A"), Cells(2, "A").End(xlDown))
        X = Split(r.Value, "-")
        r.Value = X(0)
        r.Offset(0, 1).Value = X(1)
    Next

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
As always Skip you have solved my problem.

Many Thanks
 

And a more general approch for arrays...
Code:
    Dim r As Range, X, i As Integer
    
    For Each r In Range(Cells(2, "A"), Cells(2, "A").End(xlDown))
        X = Split(r.Value, "-")
        For i = 0 To UBound(X)
            Cells(r.Row, i + 1).Value = X(i)
        Next
    Next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top