运行脚本时,有时会出现错误
对象变量或With Block变量未设置
错误发生在行上
.Cells(7, maxCustomerRng2.Column).Copy
Sub DailyBH()
Dim dailySht As Worksheet 'worksheet storing latest store activity
Dim recordSht As Worksheet 'worksheet to store the highest period of each day
Dim lColDaily As Integer ' Last column of data in the store activity sheet
Dim lCol As Integer ' Last column of data in the record sheet
Dim maxCustomerRng2 As Range ' Cell containing the highest number of customers
Dim maxCustomerCnt As Double ' value of highest customer count
Set dailySht = ThisWorkbook.Sheets("hourly KPI")
Set recordSht = ThisWorkbook.Sheets("@BH KPI")
With recordSht
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
' KMC BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
'Row to scan
maxCustomerCnt = Round(Application.Max(.Range(.Cells(38, 1), .Cells(38, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(38, 1), .Cells(38, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
Range(.Cells(1, maxCustomerRng2.Column), .Cells(2, maxCustomerRng2.Column)).Copy
recordSht.Cells(1, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(1, lCol + 1).PasteSpecial xlPasteFormats
recordSht.Cells(1, lCol + 1) = DateValue(.Cells(1, maxCustomerRng2.Column))
Range(.Cells(10, maxCustomerRng2.Column), .Cells(11, maxCustomerRng2.Column)).Copy
recordSht.Cells(10, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(10, lCol + 1).PasteSpecial xlPasteFormats
recordSht.Cells(10, lCol + 1) = DateValue(.Cells(10, maxCustomerRng2.Column))
Range(.Cells(19, maxCustomerRng2.Column), .Cells(20, maxCustomerRng2.Column)).Copy
recordSht.Cells(19, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(19, lCol + 1).PasteSpecial xlPasteFormats
recordSht.Cells(19, lCol + 1) = DateValue(.Cells(19, maxCustomerRng2.Column))
Range(.Cells(28, maxCustomerRng2.Column), .Cells(29, maxCustomerRng2.Column)).Copy
recordSht.Cells(28, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(28, lCol + 1).PasteSpecial xlPasteFormats
recordSht.Cells(28, lCol + 1) = DateValue(.Cells(28, maxCustomerRng2.Column))
Range(.Cells(37, maxCustomerRng2.Column), .Cells(38, maxCustomerRng2.Column)).Copy
recordSht.Cells(37, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(37, lCol + 1).PasteSpecial xlPasteFormats
recordSht.Cells(37, lCol + 1) = DateValue(.Cells(37, maxCustomerRng2.Column))
End With
' WCR BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(39, 1), .Cells(39, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(39, 1), .Cells(39, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(3, maxCustomerRng2.Column).Copy
recordSht.Cells(3, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(3, lCol + 1).PasteSpecial xlPasteFormats
.Cells(12, maxCustomerRng2.Column).Copy
recordSht.Cells(12, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(12, lCol + 1).PasteSpecial xlPasteFormats
.Cells(21, maxCustomerRng2.Column).Copy
recordSht.Cells(21, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(21, lCol + 1).PasteSpecial xlPasteFormats
.Cells(30, maxCustomerRng2.Column).Copy
recordSht.Cells(30, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(30, lCol + 1).PasteSpecial xlPasteFormats
.Cells(39, maxCustomerRng2.Column).Copy
recordSht.Cells(39, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(39, lCol + 1).PasteSpecial xlPasteFormats
End With
' NRR BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(40, 1), .Cells(40, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(40, 1), .Cells(40, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(4, maxCustomerRng2.Column).Copy
recordSht.Cells(4, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(4, lCol + 1).PasteSpecial xlPasteFormats
.Cells(13, maxCustomerRng2.Column).Copy
recordSht.Cells(13, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(13, lCol + 1).PasteSpecial xlPasteFormats
.Cells(22, maxCustomerRng2.Column).Copy
recordSht.Cells(22, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(22, lCol + 1).PasteSpecial xlPasteFormats
.Cells(31, maxCustomerRng2.Column).Copy
recordSht.Cells(31, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(31, lCol + 1).PasteSpecial xlPasteFormats
.Cells(40, maxCustomerRng2.Column).Copy
recordSht.Cells(40, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(40, lCol + 1).PasteSpecial xlPasteFormats
End With
' LRR BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(41, 1), .Cells(41, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(41, 1), .Cells(41, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(5, maxCustomerRng2.Column).Copy
recordSht.Cells(5, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(5, lCol + 1).PasteSpecial xlPasteFormats
.Cells(14, maxCustomerRng2.Column).Copy
recordSht.Cells(14, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(14, lCol + 1).PasteSpecial xlPasteFormats
.Cells(23, maxCustomerRng2.Column).Copy
recordSht.Cells(23, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(23, lCol + 1).PasteSpecial xlPasteFormats
.Cells(32, maxCustomerRng2.Column).Copy
recordSht.Cells(32, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(32, lCol + 1).PasteSpecial xlPasteFormats
.Cells(41, maxCustomerRng2.Column).Copy
recordSht.Cells(41, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(41, lCol + 1).PasteSpecial xlPasteFormats
End With
' CRR BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(42, 1), .Cells(42, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(42, 1), .Cells(42, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(6, maxCustomerRng2.Column).Copy
recordSht.Cells(6, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(6, lCol + 1).PasteSpecial xlPasteFormats
.Cells(15, maxCustomerRng2.Column).Copy
recordSht.Cells(15, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(15, lCol + 1).PasteSpecial xlPasteFormats
.Cells(24, maxCustomerRng2.Column).Copy
recordSht.Cells(24, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(24, lCol + 1).PasteSpecial xlPasteFormats
.Cells(33, maxCustomerRng2.Column).Copy
recordSht.Cells(33, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(33, lCol + 1).PasteSpecial xlPasteFormats
.Cells(42, maxCustomerRng2.Column).Copy
recordSht.Cells(42, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(42, lCol + 1).PasteSpecial xlPasteFormats
End With
' Network BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(44, 1), .Cells(44, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(44, 1), .Cells(44, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(8, maxCustomerRng2.Column).Copy
recordSht.Cells(8, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(8, lCol + 1).PasteSpecial xlPasteFormats
.Cells(17, maxCustomerRng2.Column).Copy
recordSht.Cells(17, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(17, lCol + 1).PasteSpecial xlPasteFormats
.Cells(26, maxCustomerRng2.Column).Copy
recordSht.Cells(26, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(26, lCol + 1).PasteSpecial xlPasteFormats
.Cells(35, maxCustomerRng2.Column).Copy
recordSht.Cells(35, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(35, lCol + 1).PasteSpecial xlPasteFormats
.Cells(44, maxCustomerRng2.Column).Copy
recordSht.Cells(44, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(44, lCol + 1).PasteSpecial xlPasteFormats
End With
' URR BH
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(43, 1), .Cells(43, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(43, 1), .Cells(43, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(7, maxCustomerRng2.Column).Copy
recordSht.Cells(7, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(7, lCol + 1).PasteSpecial xlPasteFormats
.Cells(16, maxCustomerRng2.Column).Copy
recordSht.Cells(16, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(16, lCol + 1).PasteSpecial xlPasteFormats
.Cells(25, maxCustomerRng2.Column).Copy
recordSht.Cells(25, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(25, lCol + 1).PasteSpecial xlPasteFormats
.Cells(34, maxCustomerRng2.Column).Copy
recordSht.Cells(34, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(34, lCol + 1).PasteSpecial xlPasteFormats
.Cells(43, maxCustomerRng2.Column).Copy
recordSht.Cells(43, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(43, lCol + 1).PasteSpecial xlPasteFormats
End With
Set maxCustomerRng = Nothing
Set dailySht = Nothing
Set recordSht = Nothing
End Sub
答案 0 :(得分:0)
如果maxCustomerRng2
失败,则需要确保Not Nothing
是Find
。
因此,您需要使用以下代码来捕获此类错误:
Set maxCustomerRng2 = .Range(.Cells(38, 1), .Cells(38, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
If Not maxCustomerRng2 Is Nothing Then ' <-- added this line
' rest of your code goes here