Excel VBA查找循环以validation最新的数据选项卡

在设置!$ AE:$ AE(度量的总数可以变化)中,我有一个十个度量的列表,如果所有这些度量都存在Data!$ 8:$ 8(作为列标题),需要使用VBA该措施作为唯一标识符。

目标是让这个macros检查数据选项卡中是否存在度量值,如果没有,则在第八行添加一个新的列,并在第八行中添加相应的度量值名称。 如果在“设置!$ AE:$ AE”中不存在度量,并且存在于“数据!$ 8:$ 8”中,则应删除相应的列。 这将用于维护一个最新的措施数据表(每行是由团队成员按date的lineitem)。

我可以找出插入/删除列,但我需要帮助的是写一个macros来检查数据选项卡对设置选项卡。

我search了四周,它似乎是一个.Find和.FindNext循环是解决scheme,但我不知道如何写这个作为What:=值,度量的名称,在循环的每一个迭代(Settings!$ AE :$ 1,Settings!$ AE:$ 2等)。

会有人能够提供一个VBA代码,这应该是什么样子? 谢谢! =)

看看这是否有帮助。

 Sub myColumns() Dim wS As Worksheet Dim wT As Worksheet Dim rS As Range, rT As Range, Cel As Range Dim l As Long Application.ScreenUpdating = False Set wS = ThisWorkbook.Worksheets("Settings") Set wT = ThisWorkbook.Worksheets("Data") 'source range With wS Set rS = .Range("AE1", .Cells(.Rows.Count, "AE:AE").End(xlUp)) End With 'target range With wT Set rT = .Range("A8", .Cells(8, .Columns.Count).End(xlToLeft)) End With 'add source items to target sheet For Each Cel In rS If IsError(Application.Match(Cel.Value, rT, 0)) Then 'doesn't exist in target, add 'add to right of existing data wT.Cells(8, wT.Columns.Count).End(xlToLeft).Offset(, 1).Value = Cel.Value End If Next Cel 'clear target sheet of source non-matches For l = rT.Columns.Count To 1 Step -1 If IsError(Application.Match(rT(l).Value, rS, 0)) Then 'doesn't exist in source, delete rT(l).EntireColumn.Delete End If Next l Application.ScreenUpdating = True End Sub 

DaveU,谢谢。 它指出了我使用Match而不是Find的正确方向。 这解决了我的问题。 以下是我的代码。

简而言之,它正在计算“设置”中的度量数量! 与Data!相匹配,检查该数字是否与数据中的度量总数相匹配,然后进行相反处理。 如果这两个条件都为真,则将MeasuresInDataUpdatedvariables设置为true。

我浏览了白板上的逻辑,似乎消除了正在更新的措施的任何误报。 感觉过度了,但是我认为这是因为工作簿被locking,用户难以手动改变某些东西。

 Private Function MeasuresInDataUpdated() As Boolean 'Check if the measures in Data match the measures in Settings Dim MeasuresInSettings As Integer 'Set variable as integer MeasuresInSettings = WorksheetFunction.CountA(Sheets("Settings").Range("MeasuresNameArea")) 'Set variable to the number of measures in Settings Dim MeasuresInData As Integer 'Set variable as integer MeasuresInData = Sheets("Data").Range("MeasuresArea").Columns.Count 'Set variable to the number of measures in Data Dim MeasuresThatMatchSettings As Integer 'Set variable as integer MeasuresThatMatchSettings = 0 'Set variable to zero Dim MeasuresThatMatchData As Integer 'Set variable as integer MeasuresThatMatchData = 0 'Set variable to zero Dim MeasuresNamesInSettings As Range 'Set variable as range Set MeasuresNamesInSettings = Sheets("Settings").Range("MeasuresNames") 'Set variable Dim MeasuresNamesInData As Range 'Set variable as range Set MeasuresNamesInData = Sheets("Data").Range("MeasuresNames") 'Set variable Dim MeasuresName As Range 'Set variable as range For Each MeasuresName In MeasuresNamesInSettings 'For each measure in Settings If Not IsError(Application.Match(MeasuresName, MeasuresNamesInData, 0)) Then 'If measure in Settings is in Data then MeasuresThatMatchData = MeasuresThatMatchData + 1 'Set variable to equal itself plus one End If Next MeasuresName For Each MeasuresName In MeasuresNamesInData 'For each measure in Data If Not IsError(Application.Match(MeasuresName, MeasuresNamesInSettings, 0)) Then 'If measure in Data is in Settings then MeasuresThatMatchSettings = MeasuresThatMatchSettings + 1 'Set variable to equal itself plus one End If Next MeasuresName If MeasuresThatMatchData = MeasuresInData And MeasuresThatMatchSettings = MeasuresInSettings Then 'If the measures in Settings that match the measures in Data equal the number of measures in Data, and if the measures in Data that match the measures in Settings equal the number of measures in Settings then MeasuresInDataUpdated = True 'Measures in Data are updated Else MeasuresInDataUpdated = False 'Measures in Data are not updated End If MsgBox "Measures In Data Updated: " & MeasuresInDataUpdated _ & vbNewLine & "Measures In Settings: " & MeasuresInSettings _ & vbNewLine & "Measures That Match Settings: " & MeasuresThatMatchSettings _ & vbNewLine & "Measures In Data: " & MeasuresInData _ & vbNewLine & "Measures That Match Data: " & MeasuresThatMatchData End Function 'End function