Excel VBA ::使用循环和放大器复制值运行时错误9

时间:2013-01-03 08:11:10

标签: excel-vba for-loop runtime-error vba excel

我对VBA很新,所以下面的问题应该不会太难。但是因为我花了大约一个半小时的代码,我想我只是缺乏基本的理解它应该是什么样子所以我认为值得在这里发布。

代码的作用基本上是将值从一个工作表中的一个范围复制到包含某种计划的同一活动工作簿中另一个工作表中的另一个范围。

我在第一次循环中已经出现“运行时错误'9'下标超出范围”错误消​​息

'Subject
Morning = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day)

我在空白工作簿中尝试了代码,因此错误不应归因于格式化(如果这可能是原因)。但如果您需要有关上下文等的更多信息,我当然可以提供更多详细信息。

感谢您的回答!

帕特里克

Sub SetData_new()
' SubModule to copy the data from 'Master_Plan' to particular sheets

Dim CurrWS As Long, StartWS As Long, EndWS As Long
StartWS = Sheets("W_1").Index
EndWS = Sheets("L_1").Index
Dim Weeknr As Integer
Weeknr = 5
Dim Day As Long
Dim Morning As Range, Afternoon As Range, Noon As Range

For CurrWS = StartWS To EndWS

   'Set updated ranges
   Set Morning = Worksheets(CurrWS).Cells(4, 4)
   Set Afternoon = Worksheets(CurrWS).Cells(8, 4)
   Set Noon = Worksheets(CurrWS).Cells(12, 4)

    For Day = 3 To 21 Step 3
    'Subject
       Morning = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day)
       Afternoon = Worksheets("ExamSchedule").Cells(Weeknr + 4, Day)
       Noon = Worksheets("ExamSchedule").Cells(Weeknr + 6, Day)

     'Category
       Morning.Offset(0, 1) = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day + 1)
       Afternoon.Offset(0, 1).Value = Worksheets("ExamSchedule").Cells(Weeknr + 4, Day + 1)
       Noon.Offset(0, 1).Value = Worksheets("ExamSchedule").Cells(Weeknr + 6, Day + 1)

     'Type
       Morning.Offset(0, 2) = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day + 2)
       Afternoon.Offset(0, 2).Value = Worksheets("ExamSchedule").Cells(Weeknr + 4, Day + 2)
   Noon.Offset(0, 2).Value = Worksheets("ExamSchedule").Cells(Weeknr + 6, Day + 2)

     'Specifications
       Morning.Offset(0, 3) = Worksheets("ExamSchedule").Cells(Weeknr + 3, Day)
       Afternoon.Offset(0, 3).Value = Worksheets("ExamSchedule").Cells(Weeknr + 5, Day)
       Noon.Offset(0, 3).Value = Worksheets("ExamSchedule").Cells(Weeknr + 7, Day)

     'Increment
       Morning = Morning.Offset(12, 0)
       Afternoon = Afternoon.Offset(12, 0)
       Noon = Noon.Offset(12, 0)
     Next Day

    Wochennummer = Wochennummer + 9
Next CurrWS

Worksheets("ExamSchedule").Activate

End Sub

2 个答案:

答案 0 :(得分:2)

错误是由于不合格Variant的默认Dim类型

行是什么?

Morning = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day)

ExamSchedule的值放入Morning,并将其类型设置为单元格值类型。

当行

Morning.Offset(0, 1) = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day + 1)
运行

Morning不属于Range类型,因此没有Offset属性和错误

使用

Dim Morning as Range

等应该解决你的问题。

修改

简化一下,试试这个 使用变量作为工作表
明确Value引用

Sub SetData_new()
    ' SubModule to copy the data from 'Master_Plan' to particular sheets
    Dim Wochennummer As Long ' unless this is a global variable?
    Dim CurrWS As Long, StartWS As Long, EndWS As Long
    StartWS = Sheets("W_1").Index
    EndWS = Sheets("L_1").Index
    Dim Weeknr As Long
    Weeknr = 5
    Dim Day  As Long
    Dim Morning As Range, Afternoon As Range, Noon As Range

    Dim ws As Worksheet
    Set ws = Worksheets("ExamSchedule")

    For CurrWS = StartWS To EndWS

       'Set updated ranges
       Set Morning = Worksheets(CurrWS).Cells(4, 4)
       Set Afternoon = Worksheets(CurrWS).Cells(8, 4)
       Set Noon = Worksheets(CurrWS).Cells(12, 4)

        For Day = 3 To 21 Step 3
        'Subject
           Morning.Value = ws.Cells(Weeknr + 2, Day).Value
           Afternoon.Value = ws.Cells(Weeknr + 4, Day).Value
           Noon.Value = ws.Cells(Weeknr + 6, Day).Value

         'Category
           Morning.Offset(0, 1).Value = ws.Cells(Weeknr + 2, Day + 1).Value
           Afternoon.Offset(0, 1).Value = ws.Cells(Weeknr + 4, Day + 1).Value
           Noon.Offset(0, 1).Value = ws.Cells(Weeknr + 6, Day + 1).Value

         'Type
           Morning.Offset(0, 2) = ws.Cells(Weeknr + 2, Day + 2)
           Afternoon.Offset(0, 2).Value = ws.Cells(Weeknr + 4, Day + 2)
           Noon.Offset(0, 2).Value = ws.Cells(Weeknr + 6, Day + 2)

         'Specifications
           Morning.Offset(0, 3).Value = ws.Cells(Weeknr + 3, Day).Value
           Afternoon.Offset(0, 3).Value = ws.Cells(Weeknr + 5, Day).Value
           Noon.Offset(0, 3).Value = ws.Cells(Weeknr + 7, Day).Value

         'Increment
           Morning.Value = Morning.Offset(12, 0).Value
           Afternoon.Value = Afternoon.Offset(12, 0).Value
           Noon.Value = Noon.Offset(12, 0).Value
         Next Day

        Wochennummer = Wochennummer + 9
    Next CurrWS

    ws.Activate
End Sub

答案 1 :(得分:2)

对于所有感兴趣的人,我最终实现的版本如下所示。它在Windows上运行得非常好,也可以在Mac上运行:

Sub SetData_new_my()
' SubModule to copy the data from 'Master_Plan' to particular sheets

Dim CurrWS As Long, StartWS As Long, EndWS As Long, ScheduleWS As Long
StartWS = Sheets("W_1").Index
EndWS = Sheets("L_1").Index

Dim Weeknr As Long
Weeknr = 5
Dim Day As Long

Dim Morning As Range, Afternoon As Range, Noon As Range    
Dim ExamSchedule As Worksheet
Set ExamSchedule = Worksheets("ExamSchedule")
    
For CurrWS = StartWS To EndWS
   'Set updated ranges
   Set Morning = Worksheets(CurrWS).Cells(4, 4)
   Set Afternoon = Worksheets(CurrWS).Cells(8, 4)
   Set Noon = Worksheets(CurrWS).Cells(12, 4)

    For Day = 3 To 21 Step 3
    'Subject
       Morning.Value = ExamSchedule.Cells(Weeknr + 2, Day).Value
       Afternoon.Value = ExamSchedule.Cells(Weeknr + 4, Day).Value
       Noon.Value = ExamSchedule.Cells(Weeknr + 6, Day).Value

     'Category
       Morning.Offset(0, 1).Value = ExamSchedule.Cells(Weeknr + 4, Day + 1).Value
       Afternoon.Offset(0, 1).Value = ExamSchedule.Cells(Weeknr + 4, Day + 1).Value
       Noon.Offset(0, 1).Value = ExamSchedule.Cells(Weeknr + 6, Day + 1).Value

     'Type
       Morning.Offset(0, 2).Value = ExamSchedule.Cells(Weeknr + 1, Day + 2).Value
       Afternoon.Offset(0, 2).Value = ExamSchedule.Cells(Weeknr + 4, Day + 2).Value
       Noon.Offset(0, 2).Value = ExamSchedule.Cells(Weeknr + 6, Day + 2).Value

     'Specifications
       Morning.Offset(0, 3).Value = ExamSchedule.Cells(Weeknr + 3, Day).Value
       Afternoon.Offset(0, 3).Value = ExamSchedule.Cells(Weeknr + 5, Day).Value
       Noon.Offset(0, 3).Value = ExamSchedule.Cells(Weeknr + 7, Day).Value

     'Increment
       Set Morning = Morning.Offset(12, 0)
       Set Afternoon = Afternoon.Offset(12, 0)
       Set Noon = Noon.Offset(12, 0)
     Next Day

    Weeknr = Weeknr + 9
Next CurrWS

ExamSchedule.Activate
      
End Sub