□投稿者/ よねさん -(2018/08/05(Sun) 08:01:05)
| 前に書いたコードで、コンマが抜けていました。
Sheet1以外のシートがアクティブな状態で実行するとまずい結果になってしまいます。
If Weekday(.Cells(i, 2).Value) = sh2.Cells(2, 1).Value Then Cells(i, 3).Value = sh2.Cells(3, 1).Value
↓
If Weekday(.Cells(i, 2).Value) = sh2.Cells(2, 1).Value Then .Cells(i, 3).Value = sh2.Cells(3, 1).Value
のように修正してください。
ついでに、シート2が
月曜 火曜 水曜 木曜 金曜 土曜 日曜
2 3 4 5 6 7 1
出勤 出勤 休日 休日 出勤 出勤 休日
といった具合で、A1〜G1に入力されていて、その右が空欄である場合は
↓のようにすることができます。
Sub test()
Dim i As Long, j As Long
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
Dim RC As Long
With sh1
RC = sh2.Range("A1").End(xlToRight).Column
.Range(.Cells(4, 3), .Cells(35, 3)).Value = ""
For i = 4 To 35
For j = 1 To RC
If .Cells(i, 2).Value = "" Then Exit For
If Weekday(.Cells(i, 2).Value) = sh2.Cells(2, j).Value Then .Cells(i, 3).Value = sh2.Cells(3, j).Value
Next j
Next i
End With
End Sub
|
|