アプリ版:「スタンプのみでお礼する」機能のリリースについて

下記のプログラムを実行すると表題のエラーが出ます。
コード上、何かおかしいでしょうか。

Sub kintaiC()
Dim i As Long, j As Long, lastRow As Long
Dim mycol As Long, endT As Long
Dim wScriptHost As Object
Dim Filename2 As String
Dim fl2 As Workbook
Dim c As Range

'ファイルを開く
Set wScriptHost = CreateObject("WScript.shell")
ChDir wScriptHost.specialFolders("Desktop")
Filename2 = Application.GetOpenFilename
Set fl2 = Workbooks.Open(Filename:=Filename2)
Application.ScreenUpdating = False

With Worksheets(2)
lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
For j = 1 To .Cells(1, Columns.Count).End(xlToLeft).Column
With .Cells(1, j)
Select Case .Value
Case "深夜60時間超"
.Value = "振替"
.Interior.ColorIndex = 45
Case "確認者氏名"
.Interior.ColorIndex = 3
Case "勤怠項目"
mycol = j
Case "実績終業時間"
endT = j
.Columns(j).NumberFormatLocal = "hh:mm"
End Select
End With
Next j

.Range("A1").AutoFilter field:=mycol, Criteria1:="振替出勤"
If .Cells(Rows.Count, "A").End(xlUp).Row > 1 Then
Range(.Cells(2, mycol), .Cells(lastRow, mycol)).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 37
End If
.Range("A1").AutoFilter field:=mycol, Criteria1:="振替休日"
If .Cells(Rows.Count, "A").End(xlUp).Row > 1 Then
Range(.Cells(2, mycol), .Cells(lastRow, mycol)).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 38
End If

'//▼ココから色付け//
.AutoFilterMode = False '//フィルタを解除//
Set c = .Rows(1).Find(what:="振替", LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
.Range("A1").AutoFilter field:=c.Column, Criteria1:="<>" '//←空白以外でフィルタを掛ける//
If .Cells(Rows.Count, "A").End(xlUp).Row > 1 Then
Range(.Cells(2, c.Column), .Cells(lastRow, c.Column)).SpecialCells(xlCellTypeVisible) _
.Interior.Color = .Cells(1, c.Column).Interior.Color
End If
End If

.AutoFilterMode = False '//フィルタを解除//
Set c = .Rows(1).Find(what:="確認者氏名", LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
.Range("A1").AutoFilter field:=c.Column, Criteria1:="<>"
If .Cells(Rows.Count, "A").End(xlUp).Row > 1 Then
Range(.Cells(2, c.Column), .Cells(lastRow, c.Column)).SpecialCells(xlCellTypeVisible) _
.Interior.Color = .Cells(1, c.Column).Interior.Color
End If
End If

'//▲ココまで追加//
.AutoFilterMode = False
For i = 2 To lastRow
With .Cells(i, endT)
If .Value = TimeValue("17:30") Then
.Interior.ColorIndex = 43
Next i
End If
End With
End With

Application.ScreenUpdating = True

End Sub

A 回答 (2件)

'//▲ココまで追加//


.AutoFilterMode = False
For i = 2 To lastRow
With .Cells(i, endT)
If .Value = TimeValue("17:30") Then
.Interior.ColorIndex = 43
End If
End With
Next i

End With

Application.ScreenUpdating = True


if 文が閉じられていません
with文が閉じられていません
上記を閉じて next jになります
    • good
    • 1
この回答へのお礼

ありがとうございました。
For や If 、Withなどを入れ子にすると初心者にとってはすごい複雑で理解するのが難しいです。
でも問題は解決しました。

お礼日時:2018/02/15 15:59

1)For i = 2 To lastRow


2)With .Cells(i, endT)
3)If .Value = TimeValue("17:30") Then
.Interior.ColorIndex = 43
1’)Next i’←ここと
3’)End If’←ここ
2’)End With
(1)For_Nextと(3)IF_End Ifの関係が変ではないですか?
ついでに
(2)With_End Withの位置もおかしい
本来は
A)
B)
C)
C’)
B’)
A’)
のような構文になっているはずです。
    • good
    • 1
この回答へのお礼

ご返答ありがとうございます。
For~NextやWith、IF_End If などが絡み合うと位置づけが初心者にとっては難しいです。
勉強になりました。

お礼日時:2018/02/15 16:00

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!