Option Explicit
Private Sub Worksheet_Activate()
Dim r As Range, rng As Range, snRow As Range, TmRow As Range
Dim x As Integer, ETRow As Long, LTRow As Long
Dim TMName As String
Application.ScreenUpdating = False
ETRow = 10: LTRow = 10
ActiveSheet.Range("C4:AG5,C11:L41").ClearContents
For x = 1 To Sheets.Count
If Sheets(x).Name <> "Summary" Then
With Sheets(Sheets(x).Name)
TMName = Left(Sheets(x).Name, 6)
With .Range("C:C")
Set snRow = .Find("Total Staff (inc Supervisors)", LookIn:=xlValues, LookAt:=xlWhole)
End With
Set rng = .Range("D5", "AH5")
For Each r In rng
If InStr(1, r.Value, "LT") > 0 Then
With Sheets("Summary")
.Cells(5, r.Column - 1) = Sheets(Sheets(x).Name).Cells(snRow.Row, r.Column).Value
With .Range("I9:L9")
Set TmRow = .Find(TMName, LookIn:=xlValues, LookAt:=xlWhole)
End With
.Cells(LTRow, TmRow.Column) = Left(r.Value, InStr(1, r.Value, "LT", vbTextCompare) - 1)
LTRow = LTRow + 1
End With
ElseIf InStr(1, r.Value, "ET") > 0 Then
With Sheets("Summary")
.Cells(4, r.Column - 1) = Sheets(Sheets(x).Name).Cells(snRow.Row, r.Column).Value
With .Range("C9:F9")
Set TmRow = .Find(TMName, LookIn:=xlValues, LookAt:=xlWhole)
End With
.Cells(ETRow, TmRow.Column) = Left(r.Value, InStr(1, r.Value, "ET", vbTextCompare) - 1)
ETRow = ETRow + 1
End With
End If
Next
End With
End If
Next
Application.ScreenUpdating = True
End Sub
据说
存在问题.Cells(LTRow, TmRow.Column) = Left(r.Value, InStr(1, r.Value, "LT", vbTextCompare) - 1)
和
.Cells(ETRow, TmRow.Column) = Left(r.Value, InStr(1, r.Value, "ET", vbTextCompare) - 1)
此代码适用于4张名单,如果用户在某个日期旁边放入ET或LT,那么如果有人值班(由W表示)
代码用于摘要表。
不确定为什么它不起作用但是当我尝试通过在C5下面添加额外的行来更改实际的摘要表时,就会发生这种情况。然后,即使我撤消所有内容,它仍然会发生。
答案 0 :(得分:0)
问题在于您要为其分配值
.Cells(LTRow, TmRow.Column)
并且在你之前的行中:
Set TmRow = .Find(TMName, LookIn:=xlValues, LookAt:=xlWhole)
因此,如果TmRow
未通过.Find()
分配给某个值,则TmRow.Column
会出现此错误。
试着像这样:
With .Range("I9:L9")
Set TmRow = .Find(TMName, LookIn:=xlValues, LookAt:=xlWhole)
If TmRow Is Nothing Then
MsgBox "TmRow knows nothing"
Stop
End If
End With
然后想一想重建代码的方法。