+ Reply to Thread
Results 1 to 6 of 6

Thread: Unable to create macro due to varying data

  1. #1
    Registered User
    Join Date
    11-17-2010
    Location
    Westbrook, Maine
    MS-Off Ver
    Excel 2007
    Posts
    10

    Question Unable to create macro due to varying data

    If anyone could help me with this, I would be very appreciative! I'm trying to figure out a macro to rearrange some data, however, it varies from item to item so I am unable to simply use text to columns or a cell offset to do the rearranging. I have attached an example for your review. Please let me know if I can provide any additional information.
    Attached Files Attached Files
    Last edited by theatricalveggie; 02-23-2011 at 03:00 PM. Reason: Change Title

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Unable to create macro due to varying data

    Hi theatricalveggie

    Welcome to the forum

    Option Explicit
    Sub ptest()
        Dim a, b(), i As Long, n As Long
        a = Range("A3:C19").Value" or 
       '  With Sheets(1).Range("a1").CurrentRegion
         '   a = .Resize(, 5).Value
         '    End With
      '  ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .comparemode = vbTextCompare
            For i = LBound(a, 1) To UBound(a, 1)
                If Not .Exists(a(i, 1)) Then
                    n = n + 1
                    b(n, 1) = a(i, 1)
                    .Add a(i, 1), n
                End If
                Select Case a(i, 2)
                Case "Fruit"
                    b(.Item(a(i, 1)), 2) = a(i, 3)
                Case "Brand"
                    b(.Item(a(i, 1)), 4) = a(i, 3)
                Case "Color"
                    b(.Item(a(i, 1)), 5) = a(i, 3)
                Case Else
                    b(.Item(a(i, 1)), 3) = b(.Item(a(i, 1)), 3) & IIf(b(.Item(a(i, 1)), 3) <> "", " , ", "") & a(i, 3)
                End Select
            Next
        End With
        Range("F1").Resize(1, 5).Value = Array("Number", "Fruit", "Value", "Brand", "Color")
        Range("F2").Resize(n, 5).Value = b
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  3. #3
    Registered User
    Join Date
    11-17-2010
    Location
    Westbrook, Maine
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Unable to create macro due to varying data

    Thank you so much!

    Question though: I copied and pasted this into VBA as is on my example sheet, and I received a Syntax error. Do you know how to resolve this issue?

    Thanks!

    Quote Originally Posted by pike View Post
    Hi theatricalveggie

    Welcome to the forum

    Option Explicit
    Sub ptest()
        Dim a, b(), i As Long, n As Long
        a = Range("A3:C19").Value" or 
       '  With Sheets(1).Range("a1").CurrentRegion
         '   a = .Resize(, 5).Value
         '    End With
      '  ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .comparemode = vbTextCompare
            For i = LBound(a, 1) To UBound(a, 1)
                If Not .Exists(a(i, 1)) Then
                    n = n + 1
                    b(n, 1) = a(i, 1)
                    .Add a(i, 1), n
                End If
                Select Case a(i, 2)
                Case "Fruit"
                    b(.Item(a(i, 1)), 2) = a(i, 3)
                Case "Brand"
                    b(.Item(a(i, 1)), 4) = a(i, 3)
                Case "Color"
                    b(.Item(a(i, 1)), 5) = a(i, 3)
                Case Else
                    b(.Item(a(i, 1)), 3) = b(.Item(a(i, 1)), 3) & IIf(b(.Item(a(i, 1)), 3) <> "", " , ", "") & a(i, 3)
                End Select
            Next
        End With
        Range("F1").Resize(1, 5).Value = Array("Number", "Fruit", "Value", "Brand", "Color")
        Range("F2").Resize(n, 5).Value = b
    End Sub

  4. #4
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Unable to create macro due to varying data

    one little type-0.. tested this one
    Option Explicit
    Sub ptest()
        Dim a, b(), i As Long, n As Long
        a = Sheets(1).Range("A3:C19").Value '" or
       '  With Sheets(1).Range("a1").CurrentRegion
         '   a = .Resize(, 5).Value
         '    End With
        ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .comparemode = vbTextCompare
            For i = LBound(a, 1) To UBound(a, 1)
                If Not .Exists(a(i, 1)) Then
                    n = n + 1
                    b(n, 1) = a(i, 1)
                    .Add a(i, 1), n
                End If
                Select Case a(i, 2)
                Case "Fruit"
                    b(.Item(a(i, 1)), 2) = a(i, 3)
                Case "Brand"
                    b(.Item(a(i, 1)), 4) = a(i, 3)
                Case "Color"
                    b(.Item(a(i, 1)), 5) = a(i, 3)
                Case Else
                    b(.Item(a(i, 1)), 3) = b(.Item(a(i, 1)), 3) & IIf(b(.Item(a(i, 1)), 3) <> "", " , ", "") & a(i, 3)
                End Select
            Next
        End With
        Sheets(1).Range("F1").Resize(1, 5).Value = Array("Number", "Fruit", "Value", "Brand", "Color")
       Sheets(1).Range("F2").Resize(n, 5).Value = b
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  5. #5
    Registered User
    Join Date
    11-17-2010
    Location
    Westbrook, Maine
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Unable to create macro due to varying data

    This is sooo close to what I need. When I ran the macro, I got:

    Number Fruit Value Brand Color
    1 Banana Trader Joe's Chiquita Yellow
    2 Banana 3 oz Chiquita Green
    3 Apple New Hampshire Washington Green
    4 Apple Whole Foods , 5 oz Fuji Red

    However, I need to keep the values in a separate row, as they are in the example.

    Again, I can't tell you how much I appreciate the help. I cannot figure out how to get the values in their own rows....



    Quote Originally Posted by pike View Post
    one little type-0.. tested this one
    Option Explicit
    Sub ptest()
        Dim a, b(), i As Long, n As Long
        a = Sheets(1).Range("A3:C19").Value '" or
       '  With Sheets(1).Range("a1").CurrentRegion
         '   a = .Resize(, 5).Value
         '    End With
        ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .comparemode = vbTextCompare
            For i = LBound(a, 1) To UBound(a, 1)
                If Not .Exists(a(i, 1)) Then
                    n = n + 1
                    b(n, 1) = a(i, 1)
                    .Add a(i, 1), n
                End If
                Select Case a(i, 2)
                Case "Fruit"
                    b(.Item(a(i, 1)), 2) = a(i, 3)
                Case "Brand"
                    b(.Item(a(i, 1)), 4) = a(i, 3)
                Case "Color"
                    b(.Item(a(i, 1)), 5) = a(i, 3)
                Case Else
                    b(.Item(a(i, 1)), 3) = b(.Item(a(i, 1)), 3) & IIf(b(.Item(a(i, 1)), 3) <> "", " , ", "") & a(i, 3)
                End Select
            Next
        End With
        Sheets(1).Range("F1").Resize(1, 5).Value = Array("Number", "Fruit", "Value", "Brand", "Color")
       Sheets(1).Range("F2").Resize(n, 5).Value = b
    End Sub

  6. #6
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Unable to create macro due to varying data

    Hi theatricalveggie,
    Apologies on the delay as i have been away for the last week or is it two
    any way maybe
    Option Explicit
    Sub ptest()
        Dim a, b(), c(), i As Long, ii As Long, n As Long, e, w As Long
        a = Range("A3:C19").Value
        ReDim b(1 To UBound(a, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .comparemode = vbTextCompare
            For i = LBound(a, 1) To UBound(a, 1)
                If Not .Exists(a(i, 1)) Then
                    n = n + 1
                    b(n, 1) = a(i, 1)
                    .Add a(i, 1), n
                End If
                Select Case a(i, 2)
                Case "Fruit"
                    b(.Item(a(i, 1)), 2) = a(i, 3)
                Case "Brand"
                    b(.Item(a(i, 1)), 4) = a(i, 3)
                Case "Color"
                    b(.Item(a(i, 1)), 5) = a(i, 3)
                Case Else
                    b(.Item(a(i, 1)), 3) = b(.Item(a(i, 1)), 3) & IIf(b(.Item(a(i, 1)), 3) <> "", " , ", "") & a(i, 3)
                w = w + 1
                            
                End Select
            Next
         End With
        ii = 1
        Range("F1").Resize(1, 5).Value = Array("Number", "Fruit", "Value", "Brand", "Color")
        ReDim c(1 To n + w, 1 To 5)
        For i = 1 To n
           c(ii, 1) = b(i, 1): c(ii, 2) = b(i, 2): c(ii, 4) = b(i, 4): c(ii, 5) = b(i, 5)
            For Each e In Split(b(i, 3), ",")
             ii = 1 + ii
               c(ii, 1) = b(i, 1): c(ii, 3) = e
            Next
        ii = 1 + ii
        Next
    
        Range("F2").Resize(n + w, 5).Value = c
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0