2017-01-18 65 views
1

我有一个过滤表/范围的代码,然后复制结果并尝试创建新的工作簿并将数据粘贴到那里(粘贴值&保持格式化)。粘贴数据值 - 将表格复制到另一个工作表

也完成后,它会尝试清除源表中的过滤器。

我无法将它粘贴到新工作簿中的数据(包括hedears)。它卡住了,说“范围没有定义”。(它只粘贴格式并在试图粘贴值时卡住)。

另外我是新来的vba,但我有这样的感觉,代码从Selection.Copy开始;工作簿。添加行是基本的/易于出错的错误。

请帮帮忙,这是代码:

Sub ExportRezAng() 
' 
' ExportRezAng Macro 


    Dim src As Worksheet 
    Dim tgt As Worksheet 
    Dim filterRange As range 
    Dim copyRange As range 
    Dim lastRow As Long 

    Set src = ThisWorkbook.Sheets("- - REZULTAT ANAF - -") 


    ' turn off any autofilters that are already set 
    src.AutoFilterMode = False 

    ' find the last row with data in column A 
    lastRow = src.range("D" & src.Rows.Count).End(xlUp).Row 

    ' the range that we are auto-filtering (all columns) 
    Set filterRange = src.range("A3:R" & lastRow) 

    ' the range we want to copy (only columns we want to copy) 
    ' in this case we are copying country from column A 
    ' we set the range to start in row 2 to prevent copying the header 
    Set copyRange = src.range("A3:N" & lastRow) 

    ' filter range based on column B 
    filterRange.AutoFilter Field:=9, Criteria1:="DA", _ 
     Operator:=xlOr, Criteria2:="=NU" 

    ' copy the visible cells to our target range 
    ' note that you can easily find the last populated row on this sheet 
    ' if you don't want to over-write your previous results 
    copyRange.SpecialCells(xlCellTypeVisible).Select 

“从这里问题START!

Selection.Copy 
    Workbooks.Add 
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ 
     SkipBlanks:=False, Transpose:=False 
    range("A1").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 


    Columns("M:N").Select 
    Application.CutCopyMode = False 
    Selection.NumberFormat = "m/d/yyyy" 
    Sheets("Sheet1").Select 
    Sheets("Sheet1").Name = "CREDIT_DOSARE_EXECUTORI" 

ThisWorkbook. _ 
     Activate 
    Rows("3:3").Select 
    Application.CutCopyMode = False 
    ThisWorkbook.Worksheets("- - REZULTAT ANAF - -").AutoFilter.Sort.SortFields. _ 
     Clear 
    ActiveSheet.ShowAllData 


End Sub 

回答

0

您的代码不知道您正在引用哪个工作簿或工作表。

看到这个:

Sub ExportRezAng() 
' 
' ExportRezAng Macro 
    Dim Src As Worksheet 
    Dim tgt As Worksheet 
    Dim filterRange As Range 
    Dim copyRange As Range 
    Dim lastRow As Long 
    Dim wB As Workbook 

    Set Src = ThisWorkbook.Sheets("- - REZULTAT ANAF - -") 

    ' turn off any autofilters that are already set 
    Src.AutoFilterMode = False 

    ' find the last row with data in column A 
    lastRow = Src.Range("D" & Src.Rows.Count).End(xlUp).Row 

    ' the range that we are auto-filtering (all columns) 
    Set filterRange = Src.Range("A3:R" & lastRow) 

    ' the range we want to copy (only columns we want to copy) 
    ' in this case we are copying country from column A 
    ' we set the range to start in row 2 to prevent copying the header 
    Set copyRange = Src.Range("A3:N" & lastRow) 

    ' filter range based on column B 
    filterRange.AutoFilter Field:=9, Criteria1:="DA", _ 
     Operator:=xlOr, Criteria2:="=NU" 

    ' copy the visible cells to our target range 
    ' note that you can easily find the last populated row on this sheet 
    ' if you don't want to over-write your previous results 
    copyRange.SpecialCells(xlCellTypeVisible).Copy 

    Set wB = Workbooks.Add 
    With wB.Sheets(1) 
     With .Range("A1") 
      .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ 
       SkipBlanks:=False, Transpose:=False 
      .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _ 
       SkipBlanks:=False, Transpose:=False 
      Application.CutCopyMode = False 
     End With '.Range("A1") 
     .Columns("M:N").NumberFormat = "m/d/yyyy" 
     .Name = "CREDIT_DOSARE_EXECUTORI" 
    End With 'wB.Sheets(1) 

    Src.AutoFilter.Sort.SortFields.Clear 
    Src.ShowAllData 
End Sub 
+0

您是right..Thank你:) – MisterA

+0

最后一个问题,请,如果我想两个范围一次复制?如下所示:设置copyRange = Src.range(“A3:H,O3:R”&lastRow) - 如同跳过复制粘贴中的某些列一样,我该怎么做? – MisterA

+0

@mistera:我不知道,通常我使用宏记录器,然后自定义代码!但粘贴它可能会相当混乱,所以也许做2复制/粘贴! ;) – R3uK

相关问题