我有一个过滤表/范围的代码,然后复制结果并尝试创建新工作簿并将数据粘贴到那里(粘贴值并保持格式化)。
完成后,它会尝试从源表中清除过滤器。
我无法将数据粘贴到A1的新工作簿中(包括hedears)。它会卡住并且说“#34;范围未定义"。(它只会粘贴格式并在尝试粘贴值时卡住)。
我也是vba的新手,但我觉得代码从 Selection.Copy开始; Workbooks.Add 行是基本/炫耀的,容易出错。
请帮忙,这是代码:
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
答案 0 :(得分: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