+ Reply to Thread
Results 1 to 12 of 12

Need help with macro for converting data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-12-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    127

    Need help with macro for converting data

    anyone have know how to convert the following source data that looks like this:

    5105473, 5105474, 5105475 batch 12/21/2012 187836




    where 5105473, 5105474, 5105475 is in column A, batch is in column B, 12/21/2012 is in column C and 187836 is in column D


    goal is to return 3 rows and 4 columns that look like this:

    5105473 batch 12/21/2012 187836
    5105474 batch 12/21/2012 187836
    5105475 batch 12/21/2012 187836

    needs to be dynamic, values in column A can vary 2-20 values
    total number of rows can also vary

    thanks in advance

  2. #2
    Registered User
    Join Date
    11-15-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Need help with macro for converting data

    Here's a quick suggestion:
    Sub dataFixing()
    Dim i As Integer
    Dim exRow As Integer
    Dim splitString As Variant
    ThisWorkbook.Sheets("Sheet1").Activate
    i = 1
    exRow = 1
    Do
        splitString = Split(Range("A" & i).Value, ",")
        For Each dString In splitString
            Sheets("Sheet2").Cells(exRow, 1) = dString
            Sheets("Sheet2").Cells(exRow, 2) = Sheets("Sheet1").Cells(i, 2)
            Sheets("Sheet2").Cells(exRow, 3) = Sheets("Sheet1").Cells(i, 3)
            Sheets("Sheet2").Cells(exRow, 4) = Sheets("Sheet1").Cells(i, 4)
            exRow = exRow + 1
        Next dString
    i = i + 1
    Loop While Cells(i, 1) <> ""
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    09-12-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    127

    Re: Need help with macro for converting data

    sullivan372,

    Thanks for the code, I follow the logic but when I try to execute the macro it doesn't do anything. Any suggestions?

  4. #4
    Registered User
    Join Date
    11-15-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Need help with macro for converting data

    Sorry for the delayed response - The first things that come to mind are:
    -You need to rename the sheets in the code to whatever yours are named
    -Your first row is blank meaning the loop only executes once, on nothing

    Check these things and let me know if you had already solved it. Otherwise, it would be easiest if you can post your file, so I can test it directly.

  5. #5
    Forum Contributor
    Join Date
    09-12-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    127

    Re: Need help with macro for converting data

    Thanks for getting back to me. I did replace the tab in the the macro and there's not a blank row first. The file I'm working with is quite large so I just included a sample section of it. Let me know if you can can get it to work.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need help with macro for converting data

    Quote Originally Posted by s4driver View Post
    Thanks for getting back to me. I did replace the tab in the the macro and there's not a blank row first. The file I'm working with is quite large so I just included a sample section of it. Let me know if you can can get it to work.
    Here's another:

    Sub s4driver()
    Dim lr As Long
    Dim x As Long
    Dim s As String
    
    s = Left(Range("A2"), 7)
    
    Do Until Range("A2") = s
    
    Range("A1").EntireRow.Insert xlDown
    Range("A1").Value = Range("A3").Value
    Range("A1").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
            :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
    x = 1
    
    Do Until ActiveCell.Value = ""
    
        ActiveCell.Offset(, 1).Select
        
        x = x + 1
        
    Loop
    
    Range(Cells(1, 1), Cells(1, x - 1)).Copy
    Range("A" & Rows.Count).End(3)(2).PasteSpecial Transpose:=True
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("B" & Rows.Count).End(3)(2).Resize(x - 1).Value = Range("B3").Value
    Range("C" & Rows.Count).End(3)(2).Resize(x - 1).Value = Range("C3").Value
    Range("D" & Rows.Count).End(3)(2).Resize(x - 1).Value = Range("D3").Value
    
    Range("A1").EntireRow.Delete xlUp
    Range("A2").EntireRow.Delete xlUp
    
    Loop
    
    End Sub

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need help with macro for converting data

    What are the values in column A separated by?
    Comma
    5105473, 5105474, 5105475

    Space
    5105473 5105474 5105475

    SemiColon
    5105473; 5105474; 5105475
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need help with macro for converting data

    Give this a try

    Sub abc()
     Const shName As String = "Sheet1"   '<-- Change here to your sheet name
     Const Delim As String = " "         '<-- Change here for you needs. Ex "," or ";"
     Dim a, i As Long, ii As Long
     
     With Worksheets(shName)
        a = .Range("a1").CurrentRegion
        .Cells.ClearContents
        .Cells(1).Resize(, 4) = Array("Col A", "Batch", "Date", "Col D")
        For i = 1 To UBound(a)
           x = Split(a(i, 1), Delim)
           For ii = 0 To UBound(x)
               With .Cells(Rows.Count, "a").End(xlUp).Offset(1)
                   .Resize(, 4) = Array(x(ii), a(i, 2), a(i, 3), a(i, 4))
               End With
           Next
        Next
     End With
    End Sub

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need help with macro for converting data

    Give this modified code a try

    Sub abc()
     Const shName As String = "Sheet1"   '<-- Change here to your sheet name
     Const Delim As String = ","         '<-- Change here for you needs. Ex "," or ";"
     Dim a, i As Long, ii As Long
     Dim b
     With Worksheets(shName)
        a = .Range("a1").CurrentRegion
        .Cells.ClearContents
        .Cells(1).Resize(, 4) = Array("PO #", "Batch", "Date", "Invoice")
        For i = 2 To UBound(a)
           x = Split(a(i, 1), Delim)
           For ii = 0 To UBound(x)
               With .Cells(Rows.Count, "a").End(xlUp).Offset(1)
                   .Resize(, 4) = Array(x(ii), a(i, 2), a(i, 3), a(i, 4))
               End With
           Next
        Next
     End With
    End Sub

  10. #10
    Forum Contributor
    Join Date
    09-12-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    127

    Re: Need help with macro for converting data

    Mike,


    That code works. How can I modify to have the output on another tab so the source data remains intact?

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need help with macro for converting data

    Here you go

    Sub abc()
     Const shName As String = "Sheet1"     '<-- Change here to your sheet name
     Const shOutput As String = "Sheet2"   '<-- Change here to your sheet name
     Const Delim As String = ","           '<-- Change here for you needs. Ex "," or ";"
     Dim a, i As Long, ii As Long
    
     With Worksheets(shName)
        a = .Range("a1").CurrentRegion
     End With
     
     Application.ScreenUpdating = False
     With Worksheets(shOutput)
        .Cells.ClearContents
        .Cells(1).Resize(, 4) = Array("PO #", "Batch", "Date", "Invoice")
        For i = 2 To UBound(a)
           x = Split(a(i, 1), Delim)
           For ii = 0 To UBound(x)
               With .Cells(Rows.Count, "a").End(xlUp).Offset(1)
                   .Resize(, 4) = Array(x(ii), a(i, 2), a(i, 3), a(i, 4))
               End With
           Next
        Next
        .Cells.EntireColumn.AutoFit
        .Select
     End With
     Application.ScreenUpdating = True
    End Sub
    Last edited by mike7952; 12-31-2012 at 10:50 AM.

  12. #12
    Forum Contributor
    Join Date
    09-12-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    127

    Re: Need help with macro for converting data

    Works perfect. Problem solved.

    Thanks for all your help...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1