简体   繁体   中英

VBA Excel - How to sort between two bold cells

in Excel, we have a project management file, where different tasks are assigned to different projects and dates.

The first task for each project is in bold letters, every other entry to the same project is regular. Then, when a different task is added, again, the first entry is bold, the rest is regular.

The problem is that up until now, the date of the tasks was completely ignored. They are pretty much just sorted by project and the order in which they are added to the database.

Now I could just sort it by date obviously, but then i would lose the sorting by project, which is very important, too. I need to find a way to keep the tasks sorted by project and in addition to that, sort them by date.

In excel, the entries look like this:

在此处输入图片说明

(Can't show you the rest for obvious reasons. In This example, the rows ARE ordered, but that's only because the employee adds them in order manually. Other employees mostly ignore the dates in which they did a certain task and just add them how they remember)

So what i want to do is go through each worksheet and sort the rows by the content of the "Start" column. First, I want to sort everything in between two cells that contain bold letters. And after that, i want to rearrange the "groups" of rows to be ordered by date also. So that the projects as a whole are ordered by date as well.

All my previous attempts either sorted everything and lost the project order, or they didn't do anything at all. I'm a total programming beginner.

I'd love any kind of tip or help.

Thanks in advance

EDIT:

Here's a bigger sample. I sadly can't post any files. The columns are fix, the number of rows varies from employee to employee. Some are two rows short, some go on till row 50.

The problem, again, is to sort for example the tasks under the project STACKOVERFLOW111, then to sort the tasks under the project STACKOVERFLOW222, and then to sort the big "groups" again, without intermingling them.

[ 在此处输入图片说明

edited after the last screenshot example

you could try this

Option Explicit

Sub main()
Dim dataRng As Range
Dim sortCol As Long, helperCol As Long

Set dataRng = ActiveSheet.Range("B4:H11") '.SpecialCells(xlCellTypeConstants)
sortCol = 6 '<<==  "Start" dates column is column "F" -> column index 6
With dataRng
    helperCol = .Columns(.Columns.Count).Column + 1
    Names.Add name:="bolds", refersTo:="=GET.CELL(20,OFFSET(INDIRECT(""RC"",FALSE),0,-" & helperCol - sortCol & "))"
    With .Offset(, helperCol - .Columns(1).Column).Resize(, 1)
        .FormulaR1C1 = "=if(bolds,RC" & sortCol & ","""")"
        .Offset(, 1).FormulaR1C1 = "=if(RC[-1]<>"""", RC[-1] + countif(R1C[-1]:R[-1]C[-1],RC[-1])*0.01,R[-1]C+0.0001)"
    End With
    .Resize(, .Columns.Count + 2).Sort key1:=.Columns(.Columns.Count + 2), order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlNo
    .Columns(.Columns.Count + 1).Resize(, 2).Clear
End With

End Sub

with following caveats

  • it makes use of Names collection. so the first time it runs it sets a "name" named "bolds" which is available form then on in the entire workbook.

  • it makes use of "GET.CELL" function which is a macro function from the "old" Excel 4.0 macro languag. In my Excel 2013 it works!

  • I assumed you have less then 100 equal "start" dates and that each "start" date has less then 100 "child" dates. should it not be like that, you can easily enlarge the "capacity" by editing those 0.01 and 0.0001 in the last part of line .Offset(, 1).FormulaR1C1 =...

  • as you can see stepping through code while executing, it uses two "helper" columns to build sorting indexes, which are eventually deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM