Vba 粘贴数据值-将一张图纸复制到另一张图纸
我有一个代码,它过滤一个表/区域,然后复制结果,并尝试创建一个新工作簿并将数据粘贴到那里(粘贴值并保持格式设置) 完成后,它还会尝试从源表中清除过滤器 我无法让它从A1(包括HeDear)将数据粘贴到新工作簿中。它被卡住并显示“未定义范围”。(它仅粘贴格式,并在尝试粘贴值时被卡住) 我也是vba新手,但我觉得代码从Selection.Copy开始;工作手册。添加行是基本的/浮夸的,并且容易出错 请帮助,这是代码:Vba 粘贴数据值-将一张图纸复制到另一张图纸,vba,excel,Vba,Excel,我有一个代码,它过滤一个表/区域,然后复制结果,并尝试创建一个新工作簿并将数据粘贴到那里(粘贴值并保持格式设置) 完成后,它还会尝试从源表中清除过滤器 我无法让它从A1(包括HeDear)将数据粘贴到新工作簿中。它被卡住并显示“未定义范围”。(它仅粘贴格式,并在尝试粘贴值时被卡住) 我也是vba新手,但我觉得代码从Selection.Copy开始;工作手册。添加行是基本的/浮夸的,并且容易出错 请帮助,这是代码: Sub ExportRezAng() ' ' ExportRezAng Macro
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
您的代码不知道您引用的是哪个工作簿或工作表 见此:
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
最后一个问题,如果我想同时复制两个范围?如:Set copyRange=Src.range(“A3:H,O3:R”和lastRow)-如从复制粘贴中跳过某些列,我该怎么做?@mistera:我不知道,通常我使用宏记录器,然后自定义代码!但是粘贴可能会非常混乱,所以可能需要2次拷贝/粘贴最后一个问题,如果我想同时复制两个范围?如:Set copyRange=Src.range(“A3:H,O3:R”和lastRow)-如从复制粘贴中跳过某些列,我该怎么做?@mistera:我不知道,通常我使用宏记录器,然后自定义代码!但是粘贴可能会非常混乱,所以可能需要2次拷贝/粘贴