VBA Excel 2010 - 使用范围时出现运行时1004错误

时间:2013-09-02 23:38:56

标签: excel vba excel-vba runtime range

我在vba程序中引用范围时遇到问题。以下代码片段显示了我的原始代码:

With Worksheets("Overall 6 mo")
    .Columns("A:G").ColumnWidth = 13.57
    .Range("A1:Z100").Rows.RowHeight = 15

    .Columns("F:G").NumberFormat = "0.00%"

    .Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value
    .Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula

    .Range("A1").NumberFormat = "@"
    .Range("A1") = .Name
End With

这将在执行第3行后抛出“运行时1004应用程序定义的或对象定义的错误”。 那么,我改变了

.Range("A1:Z100").Rows.RowHeight = 15

.Rows.RowHeight = 15

关键是要让我需要使用的细胞高度为15,所以这个改变不会伤害我的程序。现在,它将允许,但然后在下一行抛出相同的错误,我再次引用一个范围。所以我想弄清楚为什么它不允许我使用.range?或者至少我如何解决它?

更新: 我已经意识到我不能在我的工作簿中的任何地方使用.Range方法(不仅仅是在上面的实例中)。什么会阻止我使用.Range无处不在?

UPDATE2: 它现在不再允许我在第二行使用.Columns方法。我没有做过任何事情,只是经历了几次。这件事有什么问题?

UPDATE3: 似乎当我重新启动excel时,它将允许我运行工作表“Overall 6 mo”代码一次,然后每次开始抛出错误。我已经包含了表格其余部分的代码。

Option Explicit

Private Sub Worksheet_Activate()

    Application.ScreenUpdating = False

    Dim shIndex As Integer
    Dim rowIndex As Integer
    Dim myLastRow As Integer
    Dim shLastRow As Integer
    Dim col As Integer

    myLastRow = Worksheets("Overall 6 mo").Cells(65536, 1).End(xlUp).Row

    ' Format Worksheet
    Sheets("Overall 6 mo").Cells.Clear
    With Worksheets("Overall 6 mo")
        .Columns.ColumnWidth = 13.57
        .Rows.RowHeight = 15

        .Columns("F:G").NumberFormat = "0.00%"

        .Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value
        .Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula

        .Range("A1").NumberFormat = "@"
        .Range("A1") = .Name
    End With

    ' Clear current sheet data
    myLastRow = Worksheets("Overall 6 mo").Cells(65536, 2).End(xlUp).Row
    Worksheets("Overall 6 mo").Range(Cells(4, 1), Cells(myLastRow, 7)).Clear

    ' Compile data from last six months and add to and display on "Overall 6 mo" sheet
    For shIndex = Worksheets.Count - 5 To Worksheets.Count
        Worksheets(shIndex).Activate

        myLastRow = Worksheets("Overall 6 mo").Cells(65536, 2).End(xlUp).Row
        shLastRow = Worksheets(shIndex).Cells(65536, 1).End(xlUp).Row

        Worksheets("Overall 6 mo").Cells(myLastRow + 1, 1).Value _
            = MonthName(Month(CDate(Worksheets(shIndex).Name)), False)

        Worksheets(shIndex).Range("A4:D" & shLastRow) _
            .Copy (Worksheets("Overall 6 mo").Cells(myLastRow + 1, 2))

    Next shIndex

    ' Call UpdateChart to clear and re-add Quality and Cost charts to wks
    Call UpdateCharts(Worksheets("Overall 6 mo").Index)

    Worksheets("Overall 6 mo").Activate
    Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法更改行高:

.Range("A1:Z100").RowHeight = 15

你可以使用范围复制方法

Worksheets("TEMPLATE").Range("A3:F3").Copy .Range("B3")
Worksheets("TEMPLATE").Range("E4:F100").Copy .Range("F4")

更新:

Option Explicit

Private Sub Worksheet_Activate()    
    Dim oSh As Worksheet
    Dim shIndex As Long
    Dim rowIndex As Long
    Dim myLastRow As Long
    Dim shLastRow As Long

    Application.ScreenUpdating = False
    Set oSh = ThisWorkbook.Worksheets("Overall 6 mo")

    ' Format Worksheet
    With oSh
        .Cells.Clear
        .Columns.ColumnWidth = 13.57
        .Rows.RowHeight = 15
        .Columns("F:G").NumberFormat = "0.00%"
        .Range("A1").NumberFormat = "@"
        .Range("A1") = .Name

        .Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value
        .Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula
    End With

    ' Clear current sheet data
    oSh.Range(oSh.Cells(4, 1), oSh.Cells(GetLastRow(oSh, 2), 7)).Clear

    ' Compile data from last six months and add to and display on "Overall 6 mo" sheet
    For shIndex = Worksheets.Count - 5 To Worksheets.Count
        'Worksheets(shIndex).Activate

        myLastRow = GetLastRow(oSh, 2)
        shLastRow = GetLastRow(Worksheets(shIndex), 1)

        oSh.Cells(myLastRow + 1, 1).Value = MonthName(Month(CDate(Worksheets(shIndex).Name)), False)
        Worksheets(shIndex).Range("A4:D" & shLastRow).Copy oSh.Cells(myLastRow + 1, 2)

    Next shIndex

    ' Call UpdateChart to clear and re-add Quality and Cost charts to wks
    Call UpdateCharts(oSh.Index)

    oSh.Activate
    Set oSh = Nothing
    Application.ScreenUpdating = True
End Sub

Private Function GetLastRow(oSheet As Worksheet, lngColumn As Long) As Long
    GetLastRow = oSheet.Cells(oSheet.UsedRange.SpecialCells(xlLastCell).Row + 1, lngColumn).End(xlUp).Row
End Function

“TEMPLATE”与索引1(或小于Worksheets.Count - 5)在同一工作簿中吗?我已经注释掉了Worksheets(shIndex).Activate,因为似乎没有必要每次都在For循环中运行这个sub。

答案 1 :(得分:0)

RowHeight适用于整行,而不是行的一部分。

所以使用

.Range("A1:A100").EntireRow.RowHeight = 15

.Range("1:100").RowHeight = 15