简体   繁体   English

从不同工作表中的Activex控件启动过滤数据的副本

[英]Initiating copy of filtered data from activex control in different sheet

I'm having issues copying and moving filtered data to a new sheet for further evaluation. 我在将过滤后的数据复制和移动到新表进行进一步评估时遇到问题。 The goal is to use an activex textbox and command button on sheet1 to filter data on sheet2(Data) and copy the results to sheet3(Calculation). 目的是使用sheet1上的activex文本框和命令按钮来过滤sheet2(Data)上的数据,并将结果复制到sheet3(Calculation)。 Here's the code I'm working with: 这是我正在使用的代码:

Private Sub CommandButton1_Click()

    Sheets("Data").Range("C2").Value = TextBox1.Text

    Worksheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter _
        field:=21, Criteria1:="=*" & TextBox1.Text & "*"

    Range("Table1[[#Headers],[Comp Date]]").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy

    Sheets("Calculation").Select
    ActiveSheet.Paste

End Sub

When I run this code on the module for sheet1, I get a "Method 'Range' of object '_Worksheet' failed" error message. 当我在sheet1的模块上运行此代码时,出现“对象'_Worksheet'失败的方法'范围'”错误消息。 However, if I take the bit of code relating to the copy and paste task and place it in the module for sheet2 initiated by a second command button, it works fine. 但是,如果我使用了与复制和粘贴任务有关的一点代码,并将其放在由第二个命令按钮启动的sheet2模块中,则可以正常工作。 I'd like to accomplish both the filter and copy and paste with the push of one button if possible. 如果可能的话,我想同时完成过滤和复制和粘贴操作。 Can anyone offer any assistance in where I'm going wrong? 任何人都可以在我出问题的地方提供任何帮助吗?

Based on your comments, try this: 根据您的评论,尝试以下操作:

Private Sub CommandButton1_Click()
    Dim lo                    As ListObject
    With Sheets("Data")
        .Range("C2").Value = TextBox1.Text

        Set lo = .ListObjects("Table1")
    End With
    With lo
        .Range.AutoFilter field:=21, Criteria1:="=*" & TextBox1.Text & "*"
        .Range.SpecialCells(xlCellTypeVisible).Copy
    End With
    Sheets("Calculation").Paste

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM