Excel:创建可排序的化合物 ID

发布于 2024-11-09 09:34:50 字数 1193 浏览 0 评论 0原文

所有,我问了一个问题“Excel VBA:排序,然后复制和粘贴”并得到了两个很好的答案。但是,由于我未能提供足够的用户需求,它们将无法工作:我要求修复我创建的现有解决方案,而不是指定实际的业务需求并查看是否有人有更好的方法。

(叹气)这里是:

我的老板要求我创建一个 ss 来记录问题。他想要一个复合 ID,将“分配日期”与一个数字连接起来,该数字指示仅当天的编号问题。新的一天,计数必须从 1 重新开始。例如:

Assigned     Issue   Concatenated
  Date    &  Count =      ID

5/11/2011 &    1   =   5112011-1
5/11/2011 &    2   =   5112011-2
5/11/2011 &    3   =   5112011-3
5/12/2011 &    1   =   5122011-1

我用隐藏列 C 解决了这个问题,它计算 =IF(D2<>D1,1,C1+1),从而通过增加前一个问题计数来计算问题计数如果 D 列中指定的日期与前一个日期相同,则当日期更改时从 1 开始。另一列连接指定日期和问题计数,我有我的问题 ID。

快速、简单、优雅、进、出、完成。正确的?但当我交付 ss 时,他指出,如果您(即他)对电子表格的任何部分进行排序,问题 ID 就会乱序。当然,如果行按指定日期顺序排序,则每个公式不会按顺序引用前一个日期。

我的第一反应是首先正确地重新排序分配的日期顺序,然后复制并粘贴计算出的问题计数的值以将其锁定,从而保留串联的 ID,这引发了我之前的问题。

我能想到的唯一其他方法(当然是在 VBA 中)是:

  1. 评估“指定日期”列中的所有日期
  2. 评估问题计数列中的所有数字
  3. 计算在给定分配日期分配的新项目的最新连续问题计数
  4. 将该顺序问题计数分配给新项目
  5. 最好将光标置于用户通常会转到的下一个单元格中,该单元格与刚刚输入的分配日期相邻;不过,这不是必要的

,这将避免重新排序物理 ss 的需要。然而,除了模糊地猜测这将涉及 VLOOKUP 之外,我什么也没得到。我通过搜索找不到任何东西。

有人可以帮忙吗?或者推荐一个可以去的地方?谢谢!!!

All, I asked a question "Excel VBA: Sort, then Copy and Paste" and received two excellent answers. However, because I failed to provide sufficient user requirements, they won't work: I asked for a fix to the existing solution I created, instead of specifying the actual business need and seeing if anyone has a better way.

(sigh) Here goes:

My boss asked me to create a ss to log issues. He wants a compound ID that concatenates the "Assigned Date" with a number that indicates what number issue it is for that day only. A new day, the count must restart at 1. E.g.:

Assigned     Issue   Concatenated
  Date    &  Count =      ID

5/11/2011 &    1   =   5112011-1
5/11/2011 &    2   =   5112011-2
5/11/2011 &    3   =   5112011-3
5/12/2011 &    1   =   5122011-1

I solved this with a hidden column C that calculates =IF(D2<>D1,1,C1+1), thus calculating the Issue Count by incrementing the previous issue count if the assigned date in column D is the same as the previous date, and starting over at 1 when the date changes. Another column concatenates the assigned date and the issue count, and I have my issue ID.

Quick, easy, elegant, in, out, and done. Right? But when I delivered the ss, he pointed out that if you (that is, he) sorts any part of the spreadsheet, the issue ID goes out of sequence. Of course---each formula isn't referencing the previous date in sequence if the rows are sorted out of Assigned Date order.

My immediate thought, which prompted my previous question, was to first re-sort the Assigned Date order correctly, then copy and paste the value of the calculated Issue Count to lock it in, and thus preserve the concatenated ID.

The only other way I can see to do this (in VBA, natch) is to:

  1. evaluate all the dates in the Assigned Date column
  2. evaluate all the numbers in the Issue Count column
  3. calculate the latest sequential Issue Count for an a new item assigned on a given Assigned Date
  4. Assign that sequential Issue Count to the new item
  5. It'd be nice to then place the cursor into the next cell that the user would ordinarily go to, which would be the one right adjacent to the just-entered Assigned Date; however, that isn't necessary

That would avoid the need to re-sort the physical ss. However, besides a hazy guess that this would involve VLOOKUP, I got nothing. I couldn't find anything through searching.

Can anyone help? Or suggest a place to go? Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁把谁当真 2024-11-16 09:34:50

听起来您只想自动执行“选择性粘贴”操作。以下将 a1:a100 中的公式替换为其计算值:

   Set src = ActiveSheet.Range("a1:a100")

    src.Copy
    src.Select

    Selection.PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, _
        SkipBlanks:=False, _
        Transpose:=False

Sounds like you just want to automate a Paste Special action. The following replaces the formulas in a1:a100 with their calculated values:

   Set src = ActiveSheet.Range("a1:a100")

    src.Copy
    src.Select

    Selection.PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, _
        SkipBlanks:=False, _
        Transpose:=False
一曲琵琶半遮面シ 2024-11-16 09:34:50

我认为公式 =IF(D2<>D1,1,C1+1) 可以改进,因为这依赖于按顺序排列的日期。以下内容将保留已排序的任何订单的计数

假设

     ColA           ColB                   ColC
Row1 Assigned_Date  Issue Count            Concatenate
Row2 05/11/2011     =COUNTIF($A$1:A2,A2)   =TEXT(A2,"ddmmyyyy")&"-"&B2
Row3 05/11/2011     =COUNTIF($A$1:A3,A3)   =TEXT(A3,"ddmmyyyy")&"-"&B3
Row4 05/12/2011     =COUNTIF($A$1:A4,A4)   =TEXT(A4,"ddmmyyyy")&"-"&B4
Row5 05/11/2011     =COUNTIF($A$1:A5,A5)   =TEXT(A5,"ddmmyyyy")&"-"&B5

基本上输入 B2 和 C2 公式并向下拖动。您可能需要将 ddmmyyyy 交换为 mmddyyyy,因为我们首先使用日期而不是月份:)

另外,请注意仅使用 $ - $A$1 锁定范围的第一部分:Ax

这非常适合您当前的问题,但如果问题计数按每个日期的时间顺序分配,则不起作用。

I think the formula =IF(D2<>D1,1,C1+1) could be improved as this relies on dates being in order. The following will preserve the count for any order that is sorted

Assume

     ColA           ColB                   ColC
Row1 Assigned_Date  Issue Count            Concatenate
Row2 05/11/2011     =COUNTIF($A$1:A2,A2)   =TEXT(A2,"ddmmyyyy")&"-"&B2
Row3 05/11/2011     =COUNTIF($A$1:A3,A3)   =TEXT(A3,"ddmmyyyy")&"-"&B3
Row4 05/12/2011     =COUNTIF($A$1:A4,A4)   =TEXT(A4,"ddmmyyyy")&"-"&B4
Row5 05/11/2011     =COUNTIF($A$1:A5,A5)   =TEXT(A5,"ddmmyyyy")&"-"&B5

Essentially enter B2 and C2 formulae and drag down. You might need to swap ddmmyyyy to mmddyyyy as we use dates first rather than months :)

Also, note the locking of the first part of the range only using $ - $A$1:Ax

This works perfectly for your current question but does not work if the Issue Count is assigned in time order per date.

眸中客 2024-11-16 09:34:50

使用程序怎么样?只需单击按钮即可添加下一个条目。

我假设条目将给出今天的日期,并且工作表布局为:
行:1 = 标题 / 2 = 留空 / 3 = 数据块的标题
列:A = 日期 / B = 问题计数 / C = 组合 ID / D 等 = 其他数据

Sub AddEntry()
Dim iDayRef As Long, iNumRows As Long, n As Long

With Range("A3")
    iNumRows = .CurrentRegion.Rows.Count
    For n = 2 To iNumRows
        If .Cells(n, 1).Value = Date Then
            If .Cells(n, 2).Value > iDayRef Then iDayRef = .Cells(n, 2).Value
        End If
    Next
    .Cells(iNumRows + 1, 1).Value = Date
    .Cells(iNumRows + 1, 2).Value = iDayRef + 1
    .Cells(iNumRows + 1, 3).Value = Format(Date, "mm/dd/yyyy") & " - " & iDayRef + 1
    .Cells(iNumRows + 1, 4).Select
End With
End Sub

您真的需要日期、计数和组合 ID 三列吗?如果您使用

yyyy/mm/dd - xx

ID 格式,一列可以替换所有三列,并且您可以轻松对其进行排序。

How about using a procedure? Just click a button to add the next entry.

I've assumed that the entries will be given today's date and that the sheet layout is:
Rows: 1 = Title / 2 = left blank / 3 = Headings of the data block
Columns: A = Date / B = Issue Count / C = Combined ID / D etc = other data

Sub AddEntry()
Dim iDayRef As Long, iNumRows As Long, n As Long

With Range("A3")
    iNumRows = .CurrentRegion.Rows.Count
    For n = 2 To iNumRows
        If .Cells(n, 1).Value = Date Then
            If .Cells(n, 2).Value > iDayRef Then iDayRef = .Cells(n, 2).Value
        End If
    Next
    .Cells(iNumRows + 1, 1).Value = Date
    .Cells(iNumRows + 1, 2).Value = iDayRef + 1
    .Cells(iNumRows + 1, 3).Value = Format(Date, "mm/dd/yyyy") & " - " & iDayRef + 1
    .Cells(iNumRows + 1, 4).Select
End With
End Sub

And do you really need three columns for Date, Count, and Combined ID? If you went with a

yyyy/mm/dd - xx

ID format, one column could replace all three, and you could easily sort on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文