+ Reply to Thread
Results 1 to 9 of 9

Thread: Find string and re-arrange values from table

  1. #1
    Registered User
    Join Date
    05-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    12

    Question Find string and re-arrange values from table

    Hi all,

    I have a worksheet which contains 3 columns of values on the "Data" worksheet. Like below:


    3 Fig Monday
    4 Fig Monday
    3 Banana Tuesday
    5 Pineapple Tuesday
    12 Coconut Wednesday
    32 Mandarine Wednesday
    6 Cucumber Thursday
    3 Tomato Thursday
    14 Lettuce Thursday


    And I am looking to write a macro which will re arrange the table format once a button is pressed to make it look like this:


    Monday
    3 Fig
    4 Fig
    Tuesday
    3 Banana
    5 Pineapple
    Wednesday
    12 Coconut
    32 Mandarine
    Thursday
    6 Cucumber
    3 Tomato
    14 Lettuce

    So my macro needs to be able to find the day of the week in the 3rd column and return whatever is in the 2 cells to the left of it, and if there are any other "Monday" cases find those also and return their values, and so on for the other days of the week.

    I have begun writing code but am not sure whether to use a find function or a vlookup type macro.

    I have pasted what I have so far but not sure if I am on the right track...

    
    Private Sub CommandButton1_Click()
    Dim s As String
    Dim i As Integer
    Dim f As Integer
    Dim g As Range
    Activate Range("A12")
    
    
    For i = 2 To 10
    
    
       
    mySelectedArea = ThisWorkbook.Worksheets("Data").Range("A" & i & ":" & "C" & i)
    s = Application.VLookup("Apple", mySelectedArea, 3, False)
    
    ActiveCell = s
    
    Next i
    
    End Sub
    Thanks in advance!!!

    Cheers,

    Dexter
    Attached Files Attached Files
    Last edited by dexter87; 05-15-2011 at 07:24 AM.

  2. #2
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Find string and re-arrange values from table

    Hi dexter87

    Welcome to the Forum

    Please wrap your code in code tags, before the moderators get you...

    Forum rules
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button.
    Cheers

  3. #3
    Registered User
    Join Date
    05-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find string and re-arrange values from table

    Thanks, sorry!!! put the code in code tags now!!

  4. #4
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Find string and re-arrange values from table

    Try this
    Private Sub CommandButton1_Click()
        Dim LastRow As Long, RowNo As Long
        
        LastRow = Range("C" & Rows.Count).End(xlUp).Row
        For RowNo = LastRow To 2 Step -1
            If Cells(RowNo, "D") <> Cells(RowNo - 1, "D") Then
               Cells(RowNo, "D").EntireRow.Insert
               Cells(RowNo, "B") = Cells(RowNo + 1, "D")
            End If
        Next
        LastRow = Range("D" & Rows.Count).End(xlUp).Row
        Range("D1:D" & LastRow).Clear
    End Sub

    Hope this helps
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  5. #5
    Registered User
    Join Date
    05-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find string and re-arrange values from table

    wow that works really well... thanks for the quick response!!

    one question...

    With that macro it inserts a new row, which alters the graph and values I have to the left of the worksheet...

    is there a way to do it by using offset instead of insert row?

    Thanks so much for your help Marcol!!

  6. #6
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Find string and re-arrange values from table

    Hmm?
    .......I am looking to write a macro which will re arrange the table format once a button is pressed to make it look like this ......
    That looks like adding rows to me!
    And what graph? There was no mention of it in your original post and it isn't in your sample workbook!!!

    However.
    With your sample sheet, clear the contents of Column A, or put the contents elsewhere.

    Then run this macro.
    Private Sub CommandButton1_Click()
        Dim LastRow As Long, RowNo As Long
    
        LastRow = Range("C" & Rows.Count).End(xlUp).Row
        For RowNo = LastRow To 2 Step -1
            If Cells(RowNo, "D") <> Cells(RowNo - 1, "D") Then
                Cells(RowNo, "A") = Cells(RowNo, "D")
            End If
        Next
        Range("D1:D" & LastRow).Clear
    
    End Sub

    Hope this helps
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  7. #7
    Registered User
    Join Date
    05-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find string and re-arrange values from table

    Thanks, sorry I should have made mention of the graph earlier.

    One more thing...

    Is there any way of having the result appear on the Result worksheet rather then re arranging the original data?

    The table would need to remain in that format on the Data sheet.

    Sorry if that was not made clear - and thanks so much for your help!!!!

  8. #8
    Valued Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    504

    Re: Find string and re-arrange values from table

    One more aproach.You can select the range and run this macro to get result in the immediate right column
    Sub DoIt()
     Dim OneCell As Range
     For Each OneCell In Selection.Cells
        If Right(OneCell.Value, Len(OneCell.Value) - InStrRev(OneCell.Value, " ")) <> _
           Right(OneCell.Offset(-1, 0).Value, Len(OneCell.Offset(-1, 0).Value) - InStrRev(OneCell.Offset(-1, 0).Value, " ")) Then
            OneCell.Offset(Rows.Count - OneCell.Row, 1).End(xlUp).Offset(1, 0).Value = _
            Right(OneCell.Value, Len(OneCell.Value) - InStrRev(OneCell.Value, " "))
        End If
        OneCell.Offset(Rows.Count - OneCell.Row, 1).End(xlUp).Offset(1, 0).Value = Left(OneCell.Value, InStrRev(OneCell.Value, " ") - 1)
     Next OneCell
    End Sub
    regards

    johnjohns

    When you are not sure where to go, every road takes you there!

  9. #9
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Find string and re-arrange values from table

    To copy to sheet "Result" without added rows.
    Private Sub CommandButton1_Click()
        Dim LastRow As Long, RowNo As Long
        Dim wsData As Worksheet, wsResult As Worksheet
    
        Set wsData = Sheets("Data")
        Set wsResult = Sheets("Result")
    
        With wsData
            LastRow = .Range("C" & .Rows.Count).End(xlUp).Row
            .Range("B1:D" & LastRow).Copy wsResult.Range("B1")
        End With
        With wsResult
            For RowNo = LastRow To 2 Step -1
                If .Cells(RowNo, "D") <> .Cells(RowNo - 1, "D") Then
                    .Cells(RowNo, "A") = .Cells(RowNo, "D")
                End If
            Next
            .Range("D1:D" & LastRow).Clear
        End With
    End Sub

    Hope this helps
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

+ 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