Link to home
Start Free TrialLog in
Avatar of Maverick1001
Maverick1001

asked on

Excel Macro 2007 sorting, merging

Gentlemen
I appreciate the assitance in
I have column A : names of companies any particular company might be repaeated
any where in the column.  
I need to
1-copy the same data in the range A3-G60 into Sheet B
2-Sorted by company entry.
3-After each company string of entries there should be an empty row, this to include a en empty row in the begining    
3- in each of the empty rows cells from column b-g will be merged
4- content of the merged cells will be the company name that follow below
5- please see my screen shot of the final look

Screen-Shot-2011-10-30-at-16.48..jpg
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Hi
Several questions:
1) Can you post a sample workbook it would make the details better to handle (like sheet names, header, format etc...)
2) You mentione "Sheet B" is it exactly like this or the default "Sheet2" ?
3) You want to copy A3-G60 to "Sheet B" but whatabout the header in Sheet B should we copy to A3 or A1 ?? all this is not defined and posting sample workbook would avoid all these questions.

As far as your request it is pretty simple once you reply to my questions even if not posted I will hv the macro ready for you.

gowflow
try this macro code from current worksheet.  will copy to new worksheet and work on that one
Option Explicit
Sub CopyFormatSheet()
    Dim currs As Worksheet, rngSort As Range, rngSortKey As Range
    Dim oldcompany As String, i As Integer, rngsize As Integer
    ActiveSheet.Copy before:=Sheets(2)
    
    Set currs = ActiveSheet
    Set rngSort = currs.UsedRange
    
    '>1>> do the sort
    Set rngSortKey = rngSort.Columns(1)
    currs.Sort.SortFields.Clear
    currs.Sort.SortFields.Add Key:=rngSortKey _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With currs.Sort
        .SetRange rngSort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    '>2>>>add empty rows & apply header
    oldcompany = ""
    rngsize = rngSort.Columns.Count
    i = 1
    Do While i < rngSort.Rows.Count
        i = i + 1
        If rngSort.Cells(i, 1).Value <> oldcompany Then
            oldcompany = rngSort.Cells(i, 1).Value
            'insert a blank row
            rngSort.Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
           
            'merge cells
            rngSort.Rows(i).Cells.Offset(0, 1).Resize(1, rngsize - 1).Merge
            
            'set merged value
            rngSort.Rows(i).Cells(2).Value = rngSort.Rows(i + 1).Cells(1).Value
            rngSort.Rows(i).Cells(2).HorizontalAlignment = xlCenter
            rngSort.Rows(i).Cells(2).VerticalAlignment = xlBottom
            rngSort.Rows(i).Cells(2).Font.Bold = True
        End If
    Loop
    
End Sub

Open in new window

Avatar of Maverick1001
Maverick1001

ASKER

Hello
@ robberbaron:
The new merged cell shoubd be from column B-G, here its from B-T
After merging can the text inside be centered ?
regards
One more thing how can I name the MACRO "Create Report"
Hi
I found out some issues, please see the yellow highlighted  cells when you apply the macro

thanx
test1.xlsx
Sorry for delay in answer, If yoour still intrested here is my version based on the workbook you have posted. As you have requested in Sheet B. Still did not see your comments on my initial request though !

gowflow
test1.xlsm
Sorry for no answering
@gowflow:
looks fine , 2 comments :
1-can you highlight the merged cells with a color say light gray
2-can the text boxes at the end of the sheet be copied

regards
@gowflow: and the merge should be from B to G
sure sorry my mistake, remember you mentioned B to G.
You want the highlighting from A to G or B to G ? B to G look like a bit cut anyway your choice.
gowflow
B to G please
Here it is
gowflow
test1.xlsm
Hi
a small issue, see attached
and a question what is the cell range covered by this macro

regards
Screen-Shot-2011-11-01-at-00.23..jpg
ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
much appreciated
thanx
quick and as requested
thank you for the grade and gald I could help.
gowflow