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...
Thanks in advance!!!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
Cheers,
Dexter
Last edited by dexter87; 05-15-2011 at 07:24 AM.
Hi dexter87
Welcome to the Forum
Please wrap your code in code tags, before the moderators get you...
Forum rules
Cheers3. 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.
Thanks, sorry!!! put the code in code tags now!!![]()
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.
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!!![]()
Hmm?
That looks like adding rows to me!.......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 ......
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.
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!!!!
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!
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks