从不同的工作表自动过滤

时间:2015-07-21 14:41:30

标签: excel vba excel-vba

我正在尝试为后期处理数据创建一个VBA宏,它目前有第一张工作表的“原始数据”表和第二张表上的后期处理工具。到目前为止我所拥有的是一个按钮,它将搜索数据并创建所需变量的图,但它会为所有测试点提取数据。我想要做的是能够从数据审查表中按测试点过滤。最理想的是在我的后处理表上有一个自动过滤器类型下拉菜单,可以选择测试点,并过滤上一页上的数据。

这是我一直在使用的搜索功能:

public partial class ANAG_OperatoriMedici
    {
        public ANAG_OperatoriMedici()
        {
            this.ANAG_OperatoriMediciXAssistiti = new HashSet<ANAG_OperatoriMediciXAssistiti>();
            this.PE_Schedulazione_Attivita = new HashSet<PE_Schedulazione_Attivita>();
            this.ANAG_Associazioni = new HashSet<ANAG_Associazioni>();
            this.PE_Attivita = new HashSet<PE_Attivita>();
        }

        public string CodicePersoneFisiche { get; set; }
        public string Descrizione { get; set; }

        public virtual ANAG_PersoneFisiche ANAG_PersoneFisiche { get; set; }
        public virtual ICollection<ANAG_OperatoriMediciXAssistiti> ANAG_OperatoriMediciXAssistiti { get; set; }
        public virtual ICollection<PE_Schedulazione_Attivita> PE_Schedulazione_Attivita { get; set; }
        public virtual ICollection<ANAG_Associazioni> ANAG_Associazioni { get; set; }
        public virtual ICollection<PE_Attivita> PE_Attivita { get; set; }
    }

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

我不是100%确定你需要什么。但是这段代码会对您想要的值和数据表进行比较。如果它=它会将它复制到行D,所以如果你有东西,你需要改变它。它还假设数据在第1行。

            Dim i As Long, lastRowD As Long, lastRowA As Long

        With Sheets("datasheetname")
        lastRowA = .Range("A" & .Rows.count).End(xlUp).Row
        For i = 1 To lastRowA
        lastRowD = .Range("D" & .Rows.count).End(xlUp).Row
        If .Cells(i, 1).Value = "testvalue" Then
            .Cells(lastRowD, 4).Value = "testvalue"
        End If
        Next i

        .Range("D1", "D" & lastRowD).Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Range("'datasheetname'!$D$1:$D" & lastRowD)


        End With

请注意,您需要更改测试值和数据表名称。以及任何列信息。我尝试了它,它的工作原理。我不知道这是否是你需要的。如果您需要复制超过1行,您将更改if中有多少行。

.cells(lastRowD,5).value= .cells(i,2).value

答案 1 :(得分:0)

对于凌乱的代码感到抱歉,但我发现这对我有用。基本上我将唯一的测试点值复制到另一个工作表,将它们链接到ComboBox,并链接一个宏以与ComboBox一起运行以自动过滤另一个工作表上的数据。我确信必须有更好的方法,但它对我有用。

   Sub ValueSelectionData()
    '
    Dim TestPt As Long
    Dim rows As Long
    Dim Value As Long 'used to select test point
    rows = Sheets(1).UsedRange.rows.Count 'Row count on data sheet
    Value = Sheets(2).Cells(2, 6).Value 'value linked to ComboBox selection
    '
    Sheets(2).Columns("A:A").ClearContents
    Sheets(1).Select
    Cells.Find(What:="TargetTestPointNumber", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate 'searches for test point column in data
  TestPt = ActiveCell.Column 

  Range(Sheets(1).Cells(2, TestPt), Sheets(1).Cells(rows, TestPt)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 1)), Unique:=True

If Value > 0 Then

     Sheets(1).Select
     Cells.Find(What:="TargetTestPointNumber", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
TestPt = ActiveCell.Column
Value = Sheets(2).Cells(2, 6).Value 'desired test point to filter for
Sheets(1).Range(Sheets(1).Cells(1, TestPt), Sheets(1).Cells(rows, TestPt)).AutoFilter Field:=1, Criteria1:=Value 'autofilters data for desired test point
Else
   'Clear all auto filters
    If Sheets(1).AutoFilterMode Then
    Sheets(1).ShowAllData

End If
End If
End Sub