0

私はVBAにかなり慣れていないので、次の質問はそれほど難しくないはずです。しかし、私はコードについて約1時間半を費やしたので、コードがどのように見えるかについての基本的な理解が不足していると思うので、ここに投稿する価値があると思いました。

コードが実行しているのは、基本的に、ある種類のスケジュールを含む同じアクティブなブック内のあるシートのある範囲から別のシートの別の範囲に値をコピーすることです。

セクションのループを最初に実行したときに、「ランタイムエラー「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
4

2 に答える 2

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
于 2013-01-03T10:08:20.057 に答える
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
于 2013-01-03T11:33:30.963 に答える