当我从另一个宏 "Call" 时宏不工作,但当我单独 select 时宏不工作

Macro not working when I "Call" it from another macro, but does work when I select it individually

我下面有一个格式化宏:

Sub Colour_whole_sheet()

Dim lastRow As Long
Dim lastColumn As Long

lastRow = Range("A1").End(xlDown).Row
lastColumn = Range("A3").End(xlToRight).Column

'Colour alternate rows purple / white
For Each cell In Range(Cells(1, 1), Cells(lastRow, lastColumn))
    If cell.Row Mod 2 = 1 Then
        cell.Interior.Color = RGB(242, 230, 255)
    Else
        cell.Interior.Color = RGB(255, 255, 255)
    End If
Next cell

End Sub

当我从另一个宏调用它时,它不会 运行,它只是:

Sub Run_macros()

[A bunch of other subs]
Call Colour_whole_sheet
[A bunch of other subs]

End Sub

它没有出现错误 - 它只是什么也没做。但是当我 select 单独使用它时,从 View > Macros > View Macros > 运行,它工作正常。

你知道为什么会这样吗?

编辑:

Sub Colour_whole_sheet()

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Calendar")

Dim lastRow As Long
Dim lastColumn As Long

lastRow = ws.Range("A1").End(xlDown).Row
lastColumn = ws.Range("A3").End(xlToRight).Column

'Colour alternate rows purple / white
For Each cell In ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastColumn))
    If cell.Row Mod 2 = 1 Then
        cell.Interior.Color = RGB(242, 230, 255)
    Else
        cell.Interior.Color = RGB(255, 255, 255)
    End If
Next cell

End Sub

这里Range("A1")没有指定这个范围在哪个工作表中。 始终 所有 指定一个工作表,您的 Range()Cells()Rows()Columns()对象。

否则很可能你的代码运行在错误的工作表上。请注意,这适用于 所有 您的宏(不仅仅是这个)。检查您是否已指定工作表处处,否则您的代码可能随机工作或失败。

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'your sheet name here

然后调整以下几行:

lastRow = ws.Range("A1").End(xlDown).Row
lastColumn = ws.Range("A3").End(xlToRight).Column

For Each cell In ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastColumn))

另请注意,您可以 format an Excel table 使行交替着色。


补充说明:

您使用的方法在查找上次使用时不可靠 row/column。最好反过来做。从最后一行开始 xlUp.

lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Last used row
lastColumn = ws.Cells(3, ws.Columns.Count).End(xlToLeft).Column 'last used column in row 3

此外,您不需要遍历所有单元格。遍历行就可以了。

Dim i As Long
For i = 1 To lastRow
    If i Mod 2 = 1 Then
        ws.Rows(i).Interior.Color = RGB(242, 230, 255)
    Else
        ws.Rows(i)..Interior.Color = RGB(255, 255, 255)
    End If
Next i

或者如果您不想为整行着色,而只想为最后使用的列着色

ws.Cells(i, lastColumn).Interior.Color 

请注意,如果有很多行,单独为每一行着色会减慢很多。因此,我建议收集参考中的所有 even/uneven 行并立即为其着色。

lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Last used row
lastColumn = ws.Cells(3, ws.Columns.Count).End(xlToLeft).Column 'last used 

Dim EvenRows As Range
Dim OddRows As Range

Dim i As Long
For i = 1 To lastRow
    If i Mod 2 = 1 Then
        If OddRows Is Nothing Then
            Set OddRows = ws.Rows(i)
        Else
            Set OddRows = Union(OddROws, ws.Rows(i))
        End If
    Else
        If EvenRows Is Nothing Then
            Set EvenRows = ws.Rows(i)
        Else
            Set EvenRows = Union(EvenRows, ws.Rows(i))
        End If
    End If
Next i

If Not OddRows Is Nothing Then OddRows.Interior.Color = RGB(242, 230, 255)
If Not EvenRows Is Nothing Then EvenRows.Interior.Color = RGB(255, 255, 255)

您可能在修改代码之后

Sub Colour_whole_sheet(Optional sht As Variant)

    If IsMissing(sht) Then Set sht = ActiveSheet ' if no argument is passed assume ActiveSheet

    Dim lastRow As Long
    Dim lastColumn As Long
    Dim i As Long

    With sht ' reference passed/assumed sheet object
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row ' best way to get a column last used cell row index
        lastColumn = .Cells(3, .Columns.Count).End(xlToLeft).Column ' best way to get a row last used cell column index

        'Colour alternate rows purple / white
        With .Range("A1", Cells(lastRow, lastColumn)) ' reference all your range
            .Interior.Color = vbWhite ' color it white
            For i = 1 To .Rows.Count Step 2 ' loop through referenced range uneven rows
                .Rows(i).Interior.Color = RGB(242, 230, 255) ' color them with purple
            Next
        End With
    End With

End Sub

如您所见:

  • 它总是引用一些sheet(无论是通过子参数传递还是激活)

  • 它不会遍历所有单元格,而只是遍历不均匀的行