我知道我在VBA代码中忽略了一个简单而关键的步骤,所以到了我无法弄清代码语法的地步。我每个月都在一个工作簿中设置标签。我只想清除列A的ClearContents,然后其余代码检索相同的对象错误。关于如何解决此“对象变量未设置”错误以及结构不同的代码以及影响该错误的其余代码的任何快速构想都将有所帮助?如果需要更多信息,请告诉我。
Sub copypaste()
Application.ScreenUpdating = False
Dim Jan As Worksheet
Dim Feb As Worksheet
Dim Mar As Worksheet
Dim Apr As Worksheet
Dim May As Worksheet
Dim Jun As Worksheet
Dim Jul As Worksheet
Dim Aug As Worksheet
Dim Sep As Worksheet
Dim Oct As Worksheet
Dim Nov As Worksheet
Dim Dec As Worksheet
Dim Template As Worksheet
Set Jan = ThisWorkbook.Sheets("Jan")
Set Feb = ThisWorkbook.Sheets("Feb")
Set Mar = ThisWorkbook.Sheets("Mar")
Set Apr = ThisWorkbook.Sheets("Apr")
Set May = ThisWorkbook.Sheets("May")
Set Jun = ThisWorkbook.Sheets("Jun")
Set Jul = ThisWorkbook.Sheets("Jul")
Set Aug = ThisWorkbook.Sheets("Aug")
Set Sep = ThisWorkbook.Sheets("Sep")
Set Oct = ThisWorkbook.Sheets("Oct")
Set Nov = ThisWorkbook.Sheets("Nov")
Set Dec = ThisWorkbook.Sheets("Dec")
Set Template = ThisWorkbook.Sheets("Template")
'Clear past SKUs
Jan.Columns("A").ClearContents
Feb.Columns("A").ClearContents 'Object Error here
Mar.Columns("A").ClearContents
Apr.Columns("A").ClearContents
May.Columns("A").ClearContents
Jun.Columns("A").ClearContents
Jul.Columns("A").ClearContents
Aug.Columns("A").ClearContents
Sep.Columns("A").ClearContents
Oct.Columns("A").ClearContents
Nov.Columns("A").ClearContents
Dec.Columns("A").ClearContents
'Copy SKUs
Template.Range(Range("B2"), Range("B2").End(xlDown)).Copy
'Paste SKUs on month sheets
Jan.Range("A1").Paste 'Error: Object does not support this property or method
Feb.Range("A1").Paste
Mar.Range("A1").Paste
Apr.Range("A1").Paste
May.Range("A1").Paste
Jun.Range("A1").Paste
Jul.Range("A1").Paste
Aug.Range("A1").Paste
Sep.Range("A1").Paste
Oct.Range("A1").Paste
Nov.Range("A1").Paste
Dec.Range("A1").Paste
'Paste SKUs to template on last row of column
Jan.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Feb.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Mar.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Apr.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
May.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Jun.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Jul.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Aug.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Sep.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Oct.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Nov.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Dec.Range("A1:C1").End(xlDown).Copy
Template.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.Paste
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
不是您的问题的答案,而是建议进行重构以减少重复:
Sub copypaste()
Dim arrMonths, m
Dim Template As Worksheet
Set Template = ThisWorkbook.Sheets("Template")
arrMonths = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", _
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Application.ScreenUpdating = False
For Each m In arrMonths
With ThisWorkbook.Sheets(m)
.Columns("A").ClearContents 'Clear past SKUs
Template.Range("B2").End(xlDown).Copy .Range("A1") 'Copy SKUs from template
.Range("A1:C1").End(xlDown).Copy Template.Range("B1").End(xlDown).Offset(1, 0)
End With
Next m
Application.ScreenUpdating = True
End Sub