Solved

Sort with spaces and borders

Posted on 2013-11-17
8
102 Views
Last Modified: 2014-02-04
trying to sort column a but keep format the same
each "block" a2:e3 need to remain
but also have odd size "blocks" a8:e10"

the list is a long list and also have multiple worksheets in workbook - all with the same column numbers and layout
Book1.xlsx
0
Comment
Question by:spirodem
  • 4
  • 3
8 Comments
 
LVL 20

Expert Comment

by:Ejgil Hedegaard
Comment Utility
Add a couple of columns with formulas to hold the sort order, like attached.
Book1-Q28296557.xlsx
0
 

Author Comment

by:spirodem
Comment Utility
the list is far too long and have many more products
and too complicated for users
need a button to click and auto sort
0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
Here is a macro that will go through all the worksheets in the workbook, sorting each in turn by the first column. The sort will be performed with variable-size blocks of rows by copying data to a newly created worksheet. After the sort is finished, the original worksheet will be deleted.
Sub SortAllSheets()
Dim i As Long, iHome As Long, n As Long
Dim celHome As Range
Dim sHomeAddress As String
Application.ScreenUpdating = False
Set celHome = ActiveCell
iHome = celHome.Worksheet.Index
sHomeAddress = celHome.Address
With ActiveWorkbook
    n = .Worksheets.Count
    For i = n To 1 Step -1
        Select Case .Worksheets(i).Name
        Case "Summary", "Data"  'Do nothing with these sheets
        Case Else
            SpecialSorter .Worksheets(i)
        End Select
    Next
End With
Application.Goto Worksheets(iHome).Range(sHomeAddress)
End Sub

Sub SpecialSorter(wsSource As Worksheet)
Dim ws As Worksheet
Dim i As Long, iNew As Long, j As Long, k As Long, n As Long, nn As Long
Dim dRank As Double
Dim rg As Range
Dim v As Variant, vRank As Variant, vType As Variant
Dim sName As String
wsSource.Copy After:=wsSource
Set ws = ActiveSheet
ws.UsedRange.Offset(1, 0).EntireRow.Delete      'Preserve header row & column widths. Delete the data.
With wsSource
    Set rg = .UsedRange
    Set rg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1)
    sName = .Name
End With
n = rg.Rows.Count
nn = Application.CountA(rg.Columns(1))
ReDim v(1 To nn, 1 To 3)     'Will contain row index, number of rows
ReDim vType(1 To nn)
ReDim vRank(1 To nn)
ReDim vNewIndex(1 To nn)
For i = 1 To n
    If rg.Cells(i, 1).Value <> "" Then
        If k <> 0 Then v(k, 2) = j
        j = 1
        k = k + 1
        v(k, 1) = i
        vType(k) = rg.Cells(i, 1).Value
    Else
        j = j + 1
    End If
Next
v(k, 2) = j
For i = 1 To nn
    vType(i) = UCase(vType(i))
Next
For i = 1 To nn
    vRank(i) = Application.CountIf(rg.Columns(1), "<" & UCase(vType(i))) + i / 1048576
Next
iNew = 2
For i = 1 To nn
    dRank = Application.Small(vRank, i)
    j = Application.Match(dRank, vRank, 0)
    rg.Rows(v(j, 1)).Resize(v(j, 2)).Copy ws.Cells(iNew, 1)
    iNew = iNew + v(j, 2)
Next
Application.DisplayAlerts = False
wsSource.Delete
Application.DisplayAlerts = True
ws.Name = sName
End Sub

Open in new window

0
 

Author Comment

by:spirodem
Comment Utility
Hi Byndt
can you please add code to exclude "home" worksheet
i want to create a home worksheet to put the sort button and other link buttons to each work sheet so need that one excluded
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 80

Expert Comment

by:byundt
Comment Utility
Did you notice statement 13 in my suggested code? I anticipated the need to exclude one or more sheets from the sorting operation, and even flagged that statement with a comment. To exclude one or more sheets, just list their names in statement 13, separated by commas.
Case "Summary", "Data"  'Do nothing with these sheets

Open in new window

0
 

Author Comment

by:spirodem
Comment Utility
great thanks

one more thing : - there are certain fields that have numbers in them like
690
1190
950
1190 Adv
690 D

they are not been sorted
0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
Could you please post a workbook that illustrates the problem? Five rows of data on a single worksheet are enough.

I'd like to see the exact layout of your data, how you did (or did not) modify the macros to suit your situation, and whether your numbers are text that looks like numbers or actual numeric values.

Brad
0
 

Author Comment

by:spirodem
Comment Utility
attached worksheet
Book11.xlsx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now