遍历工作表以将数组粘贴到范围中时键入不匹配

时间:2018-06-21 16:16:04

标签: vba excel-vba excel

我正在编写一个将月份和年份粘贴到一系列工作表中的函数。但是,由于某种原因,代码在以下行中出错:     工作表(RepName3).Range(“ R2:AC2”)= ary 错误消息是“类型不匹配”,但是您可以在Sub I的前面看到,将数组粘贴到另一个范围ws.Range(“ B3:M3”)中。那部分工作正常,所以我认为这必须与我引用每个新工作表的方式有关。但是,我使用在不同子目录中循环工作表的系统,没有任何问题。关于我在做什么错的任何建议,将不胜感激。

Sub Finish_Numbers()
Dim i As Long, j As Long
Dim ws As Worksheet, ws2 As Worksheet
Dim LastRow As Long, LastColumn As Long
Dim ary() As Variant, RepName3 As Variant, RepList3 As Variant
ary = Array("Jan-", "Feb-", "Mar-", "Apr-", "May-", "Jun-", "Jul-", "Aug-", 
"Sep-", "Oct-", "Nov-", "Dec-")
Dim yr As Integer

yr = Year(Now)

Set ws = ActiveWorkbook.Sheets("Reps")
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row - 2
Set RepList3 = ws.Range("A3:A" & (LastRow + 2))

ws.Range("B3:M3") = ary

With ActiveWorkbook
For Each RepName3 In RepList3
    Worksheets(RepName3).Range("R2:AC2") = ary
    For i = 18 To 29
        Worksheets(RepName3).Cells(2, i).Value = 
Worksheets(RepName3).Cells(2, i).Value & yr
    Next i
Next RepName3
End With

End Sub

0 个答案:

没有答案