粘贴数据值 – 将表单复制到另一个工作表

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

在完成之后,它会尝试清除源表中的filter。

我不能让它粘贴在新的工作簿从A1的数据(包括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 

从这里开始的问题!

  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 

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

看到这个:

 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