1

我在我的 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

结束子

4

2 回答 2

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

“模板”是否在索引为 1(或小于 Worksheets.Count - 5)的同一工作簿中?我已经注释掉了,Worksheets(shIndex).Activate因为似乎不需要每次在 For 循环中运行这个子程序。

于 2013-09-02T23:48:39.263 回答
0

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

所以使用

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

或者

.Range("1:100").RowHeight = 15
于 2013-09-03T06:10:07.343 回答