简体   繁体   English

VBA Excel-如何在两个粗体单元格之间排序

[英]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. 在Excel中,我们有一个项目管理文件,其中将不同的任务分配给不同的项目和日期。

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: 在excel中,条目如下所示:

在此处输入图片说明

(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. 有些短两排,有些一直排到第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. 同样,问题是例如对项目STACKOVERFLOW111下的任务进行排序,然后对项目STACKOVERFLOW222下的任务进行排序,然后再次对大型“组”进行排序,而不会将它们混合在一起。

[ [ 在此处输入图片说明

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. 它利用了Names集合。 so the first time it runs it sets a "name" named "bolds" which is available form then on in the entire workbook. 因此,它第一次运行时会设置一个名为“ bolds”的“名称”,该名称可以在整个工作簿中使用。

  • it makes use of "GET.CELL" function which is a macro function from the "old" Excel 4.0 macro languag. 它利用了“ GET.CELL”函数,该函数是“旧” Excel 4.0宏语言中的宏函数。 In my Excel 2013 it works! 在我的Excel 2013中,它可以工作!

  • I assumed you have less then 100 equal "start" dates and that each "start" date has less then 100 "child" dates. 我假设您有少于100个相等的“开始”日期,并且每个“开始”日期都具有少于100个“子”日期。 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 =... 如果不是这样,则可以通过在行.Offset(, 1).FormulaR1C1 =...的最后一部分中编辑0.010.0001来轻松地扩大“容量” .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. 如您所见,在执行过程中逐步执行代码,它使用两个“帮助程序”列来构建排序索引,这些索引最终会被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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