将某些列从筛选列表复制到另一个工作表

时间:2014-03-21 18:49:54

标签: excel vba autofilter

我已经在这个项目上工作了一段时间,无法使调试器运行干净。如果有人能让我朝着正确的方向前进,我会非常感激!

此工作簿过滤订单表单上的列表(“Soap_List”)中的数据。我的目标是按一个按钮来过滤列表,然后从列A,B和D中仅将可见单元格复制到从单元格“C12”开始的Echo France工作表。

如果您可以告诉我如何在完成复制/粘贴功能后删除过滤器,可以获得奖励!

这是我到目前为止所拥有的:

Private Sub cmbPrintEchoFrance_Click() 
    Dim OrderForm As Worksheet
    Dim EchoFrance As Worksheet
    Dim SoapList As ListObject  
    Dim lastRow As Long

    Set OrderForm = Worksheets("ORDER FORM")
    Set EchoFrance = Worksheets("ECHO FRANCE")
    Set SoapList = Worksheets("ORDER FORM").ListObjects("SOAP_LIST")

    SoapList.Range.AutoFilter Field:=3, Criteria1:="ECHO FRANCE"
    SoapList.Range.AutoFilter Field:=1, Criteria1:="<>"

    Set lastRow = OrderForm.Range("A" & OrderForm.Rows.Count).End(xlUp).Row

    Set copyRange = OrderForm.Range("A:A" & lastRow)

    copyRange.SpecialCells(xlCellTypeVisible).Copy EchoFrance.Range("C12")


End Sub

*的 更新 * 我仍然在代码的lastrow部分收到错误消息。不知道下一步该尝试什么。我正在设置一个名为ListObject的问题吗?

Private Sub cmbPrintEchoFrance_Click()
    Dim OrderForm As Worksheet
    Dim EchoFrance As Worksheet
    Dim SoapList As ListObject


    Set OrderForm = Worksheets("ORDER FORM")
    Set EchoFrance = Worksheets("ECHO FRANCE")

    Set SoapList = Worksheets("ORDER FORM").ListObjects("SOAP_LIST")

    SoapList.Range.AutoFilter Field:=3, Criteria1:="ECHO FRANCE"
    SoapList.Range.AutoFilter Field:=1, Criteria1:="<>"

    LastRow = SoapList.Range("A" & Rows.Count).End(xlUp).Row

    OrderForm.Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).Copy EchoFrance.Range("C12")




 End Sub

1 个答案:

答案 0 :(得分:0)

我终于明白了!这是我现在拥有的工作代码。我现在真正想要的是找出一个If语句来检查是否有单元格&#34; D15&#34;在PO工作表上为空,如果是,则不打印页面并移动到代码的下一部分。基本上,如果我们不从某个公司订购任何肥皂,我​​不希望打印该页面。有什么想法吗?

Private Sub cmbPrintEchoFrance_Click()
    Dim OrderForm As Worksheet
    Dim PO As Worksheet
    Dim SoapList As ListObject
    Dim lRow As Long
    Dim rngToCopy As Range, rRange As Range

    Set OrderForm = Worksheets("ORDER FORM")
    Set PO = Worksheets("PRINT ORDER")

    Set SoapList = Worksheets("ORDER FORM").ListObjects("SOAP_LIST")
    Application.ScreenUpdating = False

     With OrderForm
     lRow = .Range("A" & .Rows.Count).End(xlUp).Row
     Set rRange = .Range("A1:A" & lRow)
    .AutoFilterMode = False

        With rRange
        'print the Echo France order
        .AutoFilter Field:=3, Criteria1:="ECHO FRANCE"
        .AutoFilter Field:=1, Criteria1:="<>"
        .Range("A1:A" & lRow).Copy PO.Range("D14")
        .Range("B1:B" & lRow).Copy PO.Range("E14")
        .Range("D1:D" & lRow).Copy PO.Range("F14")
        PO.Range("E12").Value = "ECHO FRANCE"
        PO.PrintOut
    End With

'clear print order
PO.Range("D14:F84").Clear
PO.Range("D14:F84").ClearFormats

    With rRange
    'print the european soaps order
    .AutoFilter Field:=3, Criteria1:="EUROPEAN SOAPS"
    .AutoFilter Field:=1, Criteria1:="<>"
    .Range("A1:A" & lRow).Copy PO.Range("D14")
    .Range("B1:B" & lRow).Copy PO.Range("E14")
    .Range("D1:D" & lRow).Copy PO.Range("F14")
    PO.Range("E12").Value = "EUROPEAN SOAPS"
    PO.PrintOut
    End With

 'clear print order
PO.Range("D14:F84").Clear
PO.Range("D14:F84").ClearFormats

    With rRange
    'print the la lavande order
    .AutoFilter Field:=3, Criteria1:="LA LAVANDE"
    .AutoFilter Field:=1, Criteria1:="<>"
    .Range("A1:A" & lRow).Copy PO.Range("D14")
    .Range("B1:B" & lRow).Copy PO.Range("E14")
    .Range("D1:D" & lRow).Copy PO.Range("F14")
    PO.Range("E12").Value = "LA LAVANDE"
    PO.PrintOut
    End With

     'clear print order
    PO.Range("D14:F84").Clear
    PO.Range("D14:F84").ClearFormats

    End With

SoapList.Range.AutoFilter Field:=1
SoapList.Range.AutoFilter Field:=3
Application.ScreenUpdating = True

End Sub