使用模块运行工作簿中的所有工作表 - VBA

时间:2016-10-06 18:55:46

标签: excel vba excel-vba

对于我一直在研究的项目,我最终完成了用于分析季度计划的代码,现在我正在尝试让我的代码分析工作簿中的所有四个表(每个表代表不同的季度)。

我遇到的错误是“运行时错误1004对象_global失败的方法范围”

我目前正在使用模块来运行所有四张纸的代码。我的代码如下:

sub scheduleAnalyzer()

Dim ws_count As Integer
Dim I As Integer

ws_count = ActiveWorkbook.Worksheets.Count

For I = 1 To ws_count 

    'then my code which analyzes the schedule and prints results on a new sheet

Next I

End Sub

这是我第一次使用模块运行代码。错误发生在以下位置:

Dim month1 As Range
Dim month2 As Range
Dim month3 As Range
Dim month1_start As String
Dim month1_end As String
Dim month2_start As String
Dim month2_end As String
Dim month3_start As String
Dim month3_end As String  

month1_start = "B6"
monthRow = 6
dayRow = 7

For k = 2 To maxCol
    If Cells(monthRow, k).Interior.color = colorArray(10) And Cells(monthRow, k + 1).Interior.color = colorArray(11) Then
        month1_end = Cells(monthRow, k).Address
        month2_start = Cells(monthRow, k + 1).Address
    ElseIf Cells(monthRow, k).Interior.color = colorArray(11) And Cells(monthRow, k + 1).Interior.color = colorArray(10) Then
        month2_end = Cells(monthRow, k).Address
        month3_start = Cells(monthRow, k + 1).Address
    ElseIf Cells(monthRow, k).Interior.color = colorArray(10) And Cells(monthRow, k + 1).Interior.color = colorArray(9) Then
        month3_end = Cells(monthRow, k).Address
    End If
Next k

month1 = Range(month1_start, month1_end) '<--------------- run-time error 1004 method range of object _global failed
month2 = Range(month2_start, month2_end)
month3 = Range(month3_start, month3_end)

我知道当我正在为一个特定工作表运行时,此代码可以正常工作。这是在整个工作簿中运行它的最佳方法吗?如果是这样,我该如何解决此错误?我尝试使用带有“ActiveWorkbook.Worksheets()”的With语句但是我无法将单数ws命名为括号,因为我希望它在所有四个中运行。

1 个答案:

答案 0 :(得分:0)

据我所知,由于没有使用Set设置Range变量,肯定会出现错误,但这将是“对象变量或未设置块变量” “错误。

您获得的“对象_global失败的方法范围”错误可能是因为未设置month1_end,这可能是因为您的代码从未设置变量maxCol(因此您的{{1}你永远不会执行循环),或者你可能在你的问题中没有发布的代码中设置For k =,在这种情况下maxCol可能没有正确设置,因为它没有处理你想要的工作表。

希望以下代码可以解决其中一些错误,并为您提供一个可以构建循环的结构:

month1_end

编辑以下评论:

启用现有Sub调用scheduleAnalyzer(,假定当前正在处理单个活动工作表)的最简单方法是处理工作簿中的每个工作表:

sub scheduleAnalyzer()

    Dim ws As Worksheet
    Dim month1 As Range
    Dim month2 As Range
    Dim month3 As Range
    Dim month1_start As String
    Dim month1_end As String
    Dim month2_start As String
    Dim month2_end As String
    Dim month3_start As String
    Dim month3_end As String  
    'monthRow is not defined in original code
    Dim monthRow As Long
    'dayRow is not defined in original code
    Dim dayRow As Long
    'k is not defined in original code
    Dim k As Long
    'maxCol isn't defined or assigned a value in original code
    Dim maxCol As Long
    maxCol = 36 ' <-- purely random number - please assign it how you were assigning it when code was working

    For Each ws in ActiveWorkbook.Worksheets

        With ws

            month1_start = "B6"
            monthRow = 6
            dayRow = 7

            For k = 2 To maxCol
                If .Cells(monthRow, k).Interior.color = colorArray(10) And .Cells(monthRow, k + 1).Interior.color = colorArray(11) Then
                    month1_end = .Cells(monthRow, k).Address
                    month2_start = .Cells(monthRow, k + 1).Address
                ElseIf .Cells(monthRow, k).Interior.color = colorArray(11) And .Cells(monthRow, k + 1).Interior.color = colorArray(10) Then
                    month2_end = .Cells(monthRow, k).Address
                    month3_start = .Cells(monthRow, k + 1).Address
                ElseIf .Cells(monthRow, k).Interior.color = colorArray(10) And .Cells(monthRow, k + 1).Interior.color = colorArray(9) Then
                    month3_end = .Cells(monthRow, k).Address
                End If
            Next k

            Set month1 = .Range(month1_start, month1_end)
            Set month2 = .Range(month2_start, month2_end)
            Set month3 = .Range(month3_start, month3_end)

        End With

    Next

End Sub

我不建议将此作为短期解决方法以外的任何内容 - 使用Sub RunAllScheduleAnalyzer Dim ws As Worksheet For Each ws in Worksheets ws.Activate scheduleAnalyzer Next End Sub (和Activate)来确定VBA应用程序将处理哪些单元格是否应该避免所有可能的,因为它导致太多“但这个代码通常有效!?!?”问题类型。