首页 > 解决方案 > 对象工作表的 VBA 范围失败

问题描述

我在用 *** 表示的行上收到一系列对象工作表失败错误。我无法弄清楚,因为如果我在收到错误消息后单步执行代码,它就不会再次出错。有什么看起来不对吗?

Sub BuildMonthCalender()

Dim wkb As Workbook, shift_lines As Worksheet, month_lines As Worksheet, inputs As Worksheet, schedule As Worksheet
Dim d As Integer, w As Integer, last_row As Double, shift_line As Double, shifts As Double, p As Double, cRow As Double, cCol As Double
Dim counter As Double, cell_value As String

Set wkb = Excel.Workbooks("Call Center Headcount Model.xlsm")

Set schedule = wkb.Worksheets("Schedule")
Set shift_lines = ThisWorkbook.Worksheets("Shift Lines")
Set month_lines = ThisWorkbook.Worksheets("Month Lines")
Set inputs = wkb.Worksheets("Inputs")

schedule.Range("A1:K100000").Clear

last_row = month_lines.Range("A" & Rows.Count).End(xlUp).Row

shift_line = 3
shifts = 3
cRow = 0
cCol = 2
p = 1

'For shift_line = 3 To last_row
With month_lines
    Do
        If month_lines.Cells(shift_line, 1).Value <> "" Then
            For shifts = shift_line To month_lines.Cells(shift_line, 1).CurrentRegion.Rows.Count + shift_line
                cell_value = month_lines.Cells(shifts, 1).Value
                If cell_value = "" Then
                    Exit For
                End If
                schedule.Cells(cRow + month_lines.Cells(shifts, 9).Value, cCol + month_lines.Cells(shifts, 8).Value) = month_lines.Cells(shifts, 7).Value

                *****schedule.Range(Cells(p, 1), Cells(p + 5, 1)) = month_lines.Cells(shifts, 3).Value**

                schedule.Range(Cells(p, 2), Cells(p + 5, 2)) = month_lines.Cells(shifts, 10).Value
            Next shifts
        cRow = cRow + 10
        p = p + 10
        End If
        shift_line = .Cells(shift_line, 1).End(xlDown).Row + 3
    Loop Until shift_line > last_row
End With
p = 0
    For p = 1 To last_row
            With schedule.Range(Cells(p, 3), Cells(p + 5, 9))
                .Borders.LineStyle = xlContinuous
                .HorizontalAlignment = xlCenter
            End With
        p = p + 9
    Next p
MsgBox "Complete!"
End Sub

标签: excelvbasyntax-error

解决方案


推荐阅读