我有一个 Excel VBA 宏,每周运行一次。我有一段代码可以过滤出不同的数据,然后将剩余的单元格复制到不同的工作表中
以下是受影响的代码部分:
dim data as worksheet
dim sku vp as worksheet
Set skuvp = Workbooks("weekly Brand snapshot report.xlsx").Sheets("SKU VP")
set data = Workbooks("weekly Brand snapshot report.xlsx").Sheets("SKU Data")
data.Range("A1").AutoFilter Field:=4, Criteria1:="Foods", Operator:=xlFilterValues
data.Range("Onsales[[Product]]").SpecialCells(xlCellTypeVisible).Copy Destination:=skuvp.Range("B2")
skuvp.Range("foods").Sort key1:=skuvp.Range("C1"), order1:=xlDescending, Header:=xlYes
data.ShowAllData
data.Range("A1").AutoFilter Field:=4, Criteria1:="Treats", Operator:=xlFilterValues
data.Range("Onsales[[Product]]").SpecialCells(xlCellTypeVisible).Copy Destination:=skuvp.Range("H2")
skuvp.Range("treats").Sort key1:=skuvp.Range("I1"), order1:=xlDescending, Header:=xlYes
data.ShowAllData
data.Range("A1").AutoFilter Field:=3, Criteria1:="Hardgoods", Operator:=xlFilterValues
data.Range("B2:B16354").SpecialCells(xlCellTypeVisible).Copy Destination:=skuvp.Range("N2")
skuvp.Range("hard").Sort key1:=skuvp.Range("O1"), order1:=xlDescending, Header:=xlYes
data.ShowAllData
data.Range("A1").AutoFilter Field:=3, Criteria1:="Specialty", Operator:=xlFilterValues
data.Range("B2:B16354").SpecialCells(xlCellTypeVisible).Copy Destination:=skuvp.Range("T2")
skuvp.Range("spcl").Sort key1:=skuvp.Range("U1"), order1:=xlDescending, Header:=xlYes
data.ShowAllData
数据和skuvp设置为工作表。
这段代码在我第一次运行时运行良好。然而,此后它开始出现错误。错误出现在这一行:
data.Range("B2:B16354").SpecialCells(xlCellTypeVisible).Copy Destination:=skuvp.Range("N2")
它给出的错误是“无法获取范围类的 Specialcells 属性。”
我最初将该代码中的范围设置为表列“Onsales[[Product]]”作为范围,就像我之前两次使用该代码一样,但将其更改为设定范围以查看是否可以解决问题。
为什么当相同的基本代码在前面几行工作时,该代码在该行上出现错误?
我在 stackoverflow 和其他在线资源中搜索了解决方案,但没有成功。
最佳答案
所以,从评论来看,问题似乎是通过使用 .Cells
解决的:
data.Range("B2:B16354").SpecialCells(xlCellTypeVisible).Cells.Copy Destination:=skuvp.Range("N2")
关于vba - 无法获取 xlcelltypevisible 范围类的 Specialcells 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425326/