我想通过创建新的值将当前工作表中的值复制到具有自动过滤功能的其他工作簿中,一旦运行代码,我就会报错:
对象变量或未设置块变量
代码如下:
Sub copyvaluetoanothersheet()
Dim selectrange As Range
Dim wb As Workbook
Dim Dsheet As Worksheet
Dim Lastrow As Long
Application.ScreenUpdating = False
Set wb = Workbooks.Add
Set Dsheet = wb.Worksheets(1)
Lastrow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
selectrange = Sheet2.Range("A2:BP" & Lastrow)
With Worksheets("Production data")
.AutoFilterMode = False
selectrange.AutoFilter field:="Branch", Criteria1:="Direct Response"
selectrange.SpecialCells(xlCellTypeVisible).EntireRow.Copy
End With
Dsheet.PasteSpecial xlPasteValues
Application.ScreenUpdating = True
End Sub
非常感谢
答案 0 :(得分:0)
分配对象变量时,必须使用Set
(您已经在其他地方完成了此操作)。
Set selectrange = Sheet2.Range("A2:BP" & Lastrow)
还要注意,工作表代码名称,选项卡名称和索引的混合使用会造成混淆,并且如果看不到代码,则代码将出错。
答案 1 :(得分:0)
尝试关注
Sub cpVisible()
Dim MyProdName As String
Dim FilteredRange As Range
Dim myArr As Variant
Sheets("Production Data").Range("$A$2:$BP$50000").AutoFilter Field:="Branch", Criteria1:="Direct Response"
Set FilteredRange = Sheets("Production Data").Range("$A$2:$BP$50000").SpecialCells(xlCellTypeVisible)
FilteredRange.Copy Sheets("Dsheet").Range("A1")
End Sub