我需要跟踪单元格 B1 中单元格 A1、单元格 B2 中单元格 A2 的变化等等......
For Ex initially
Cell A1 & Cell B1 should be Blank
If Cell A1=9/14/2017 & Cell B1 should be having a Drop down with Blank
If Cell A1=9/21/2017 & Cell B1 Should be having a Drop down with 9/14/2017
If Cell A1=9/28/2017 & Cell B1 Should be having a Drop down with 9/14/2017 & 9/21/2017
If Cell A1=10/08/2017 & Cell B1 Should be having a Drop down with 9/14/2017 , 9/21/2017 & 9/28/2017
等等.. 我将根据我的要求以日期格式(mm/dd/yyyy)手动给出 A 列的值。
A2 和 B2、A3 和 B3 等也是一样。
答案1
以下代码应该可以工作,只需将其添加到您输入所有数据的工作表的 VBA 代码中即可。您需要设置一些内容
- 名为“数据”的新工作表
- 在此新表中,您需要添加标题为 HISTA1、HISTA2、HISTA3 等的列。
- 然后,您需要将这些列定义为命名范围,其名称与您指定的标题相同(在代码中,我最多设置了 6 列,但如果您需要更多列,您可以在代码中添加额外的行
对于输入表中的单元格 B1、B2、B3 等,您需要添加数据验证列表,并引用正确的命名范围。例如,在单元格 B1 中,范围将是“=HISTA1”
Private Sub Worksheet_Change(ByVal Target As Range) Dim a, b, c, d, e, f, x, newval, hist As Long Dim data As Worksheet Set data = ThisWorkbook.Worksheets("Data") Set ws = ThisWorkbook.ActiveSheet If Intersect(Target, Range("A:A")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub x = Target.Row newval = Target.Value Application.EnableEvents = False Application.Undo hist = Target.Value y = data.Cells(Rows.Count, x).End(xlUp).Row data.Cells(y + 1, x).Value = hist Target.Value = newval a = data.Range("A" & Rows.Count).End(xlUp).Row b = data.Range("B" & Rows.Count).End(xlUp).Row c = data.Range("C" & Rows.Count).End(xlUp).Row d = data.Range("D" & Rows.Count).End(xlUp).Row e = data.Range("E" & Rows.Count).End(xlUp).Row f = data.Range("F" & Rows.Count).End(xlUp).Row ActiveWorkbook.Names("HISTA1").Delete ActiveWorkbook.Names("HISTA2").Delete ActiveWorkbook.Names("HISTA3").Delete ActiveWorkbook.Names("HISTA4").Delete ActiveWorkbook.Names("HISTA5").Delete ActiveWorkbook.Names("HISTA6").Delete ActiveWorkbook.Names.Add Name:="HISTA1", RefersTo:="=Data!$A$2:$A$" & a ActiveWorkbook.Names.Add Name:="HISTA2", RefersTo:="=Data!$B$2:$B$" & b ActiveWorkbook.Names.Add Name:="HISTA3", RefersTo:="=Data!$C$2:$C$" & c ActiveWorkbook.Names.Add Name:="HISTA4", RefersTo:="=Data!$D$2:$D$" & d ActiveWorkbook.Names.Add Name:="HISTA5", RefersTo:="=Data!$E$2:$E$" & e ActiveWorkbook.Names.Add Name:="HISTA6", RefersTo:="=Data!$F$2:$F$" & f Application.EnableEvents = True End Sub
我根据您的示例编写了代码,但如果单元格引用不同,则需要修改代码,因为输入行链接到历史记录列。
如有任何疑问或问题,我很乐意为您提供帮助。