在总览页面中显示最近更新的行 – Excel

我有一个excel共享工作簿,几张纸和一个叫做概览的主表。

例如:概览 – 工作表1 – 工作表2 – 工作表3

在表1/2/3中,我或其他人用一些数据更新行。

很难跟踪工作簿中的最后一个条目(因为我需要在所有工作表中进行search),所以我想在我的概览页面中创build一个“前10名”dynamic列表,该列表将自动更新为最后一个工作簿中有10个更新的行。

有人可以帮我吗?

这是一个例子: 在这里输入图像说明

谢谢!

最干净的解决scheme是这样的:

在工作簿级别添加事件处理程序以捕获正在更改的单元格; 在处理程序中,做这些事情:

  • closures事件处理(你要改变工作表,不想进入无限循环)!
  • closures屏幕更新
  • 在第一行的前面一排插入一行
  • 在那里input已更改行的副本
  • 添加更改它的用户和date/时间,在一个额外的列(如果你想)
  • 返回到原来的select
  • 打开屏幕更新
  • 打开事件处理

这里是一步一步的说明(示例文件可以从http://www.floris.us/SO/download/XLexample.xlsm下载) – 在PC上假设Excel 2010。 其他版本将有大部分小差异…

  1. 确保您的文件保存为.xlsm格式 – 这告诉Excel有macros
  2. 在添加所有这些东西之前创build一个文件的备份 – 万一你搞砸了!
  3. closures所有其他文件(现在) – 请参阅前面的注释
  4. 确保你的文件有四个工作表:“摘要”,“小工具”,“东西”和“东西”(或者任何你觉得有用的名字 – 我会用这些名字来引用它们,而不是“Sheet1”等)
  5. 右键单击“小部件”选项卡,然后select“查看代码”
  6. 将以下代码粘贴到工作表的“代码”窗口中:

 Private Sub Worksheet_Change(ByBal Target as Range) On Error GoTo procErr process_change Target Exit Sub procErr: MsgBox "Got an error: " & Err.Description Err.Clear Application.EnableEvents = True End Sub` 
  1. 对每个“数据”工作表重复上述步骤:“事物”和“东西”(但不是“摘要”)
  2. 在Visual Basic编辑器打开的情况下(这是所有粘贴操作的地方),在工作簿中插入一个新的代码模块,使用Insert-> Module
  3. 将以下代码粘贴到您创build的模块中:

 Option Explicit Sub process_change(ByVal Target As Range) ' when a cell is changed on one of the worksheets, this function is called ' it copies the most recently changed row ' and inserts it on the second line of the "summary" worksheet ' right below the headers ' if the headers include "changed by" and/or "last changed" (exactly) ' then that column will be updated with the (windows) user name and date, respectively ' similarly, if a column named "source" exists, it will contain the address of the row ' (sheet name / row number). In that case, if there was an earlier occurrence of the same row ' (multiple edits), the earlier occurence is removed ' you may use this code as is - but there is no warranty as to its useability Dim s1 As Worksheet, s2 As Worksheet Dim srcAddress As String Dim oldSelection As Range ' don't update screen during processing - prevent "flickering" Application.ScreenUpdating = False ' set to True when debugging ' don't accept events until we're done Application.EnableEvents = False ' store old selection Set oldSelection = Selection Dim ri As Integer ' index of changed row Dim rowAddress As String ri = Target.Row rowAddress = ri & ":" & ri ' address of changed row if ri = 1 Then Application.EnableEvents = True Exit Sub ' don't record changes to the headers End If Range(rowAddress).Select Selection.Copy ' copy changed row Set s1 = ActiveSheet ' know where we will go back to srcAddress = s1.Name & ":row" & ri ' full address to be used later Set s2 = ActiveWorkbook.Sheets("summary") s2.Range("2:2").Insert ' add a row at the top of the list s2.Select ' activate sheet where we want to paste Range("A2").Select ' leftmost cell of column ActiveSheet.Paste ' paste the entire changed row ' optionally, we can add "source", "last changed" and "changed by" ' we do this if appropriately named columns exist ' slightly clumsy code to catch errors... Dim lcCol If Not IsError(Application.Match("last changed", Range("1:1"), 0)) Then lcCol = Application.Match("last changed", Range("1:1"), 0) Range("A2").Offset(0, lcCol - 1).Value = Date End If Dim cbCol If Not IsError(Application.Match("changed by", Range("1:1"), 0)) Then cbCol = Application.Match("changed by", Range("1:1"), 0) Range("A2").Offset(0, cbCol - 1).Value = UserName End If Dim srcCol If Not IsError(Application.Match("source", Range("1:1"), 0)) Then srcCol = Application.Match("source", Range("1:1"), 0) ' find earlier entry regarding this row... Columns("A:A").Offset(0, srcCol - 1).Select Dim sf As Range Set sf = Selection.Find(What:=srcAddress, After:=ActiveCell, LookIn:= _ xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _ xlNext, MatchCase:=False, SearchFormat:=False) If Not sf Is Nothing Then ri = sf.Row rowAddress = ri & ":" & ri ' address of changed row Range(rowAddress).Select Selection.Delete End If Range("A2").Offset(0, srcCol - 1).Value = srcAddress End If s1.Activate ' go back to original worksheet Application.CutCopyMode = False ' get rid of the "marching ants" oldSelection.Select ' select the previous selection "like nothing happened" ' and turn on screenupdating and events... Application.EnableEvents = True Application.ScreenUpdating = True End Sub Sub eventsOn() Application.EnableEvents = True End Sub Public Function UserName() ' note - this function only works on PC UserName = Environ$("UserName") End Function 

完成所有工作后,您现在可以将标题放在表单中 – 在所有四张表中使用相同的列标题。 在第一个(摘要)工作表中,您可以select添加三个标题:这些标题不应与您使用的其他标题相同,并被调用(确切地说 – 没有额外的空格,大写,…): sourcelast changedchanged by

如果最后三列标题不存在,行为将如下所示:

每次对三个工作表中的一个进行更改时,进行更改的行将被复制到汇总表的第一行,即标题下方。 其他一切将下移一行。

如果添加“源”列,则会发生两件事情:源(表名:行号)将被添加到该列中,并且同一源(同一行)的任何以前的条目将被删除。 因此,您只能看到给定行的“最近更改”。

如果添加“更改者”,您将获得进行最后更改的用户的姓名; “最后更改”标题将具有最后更改的date。

让我知道,如果你能从这里弄明白 – 使用我上面链接的示例电子表格,如果你卡住了,可以引导你。