在Macos上运行marco时下标超出范围

时间:2018-09-09 09:08:03

标签: vba excel-vba

我在代码的错误下标范围内挣扎:

Set wsCondition = wbCondition.Worksheets(2)

此代码在Windows上运行得很好,但是当我在MacOS上尝试时会发生错误。我是VBA的新手,我完全不明白为什么会发生此错误。

预先感谢您的建议。

Option Explicit
Public Sub btn1_Click()
Dim i As Double
Dim N As Double
Dim strKeyWord As String
Dim myCount As Integer
Dim OrderCount As Integer
Dim SubTotal As Range, Country As Range, DisCount As Range, Quantity As Range, ItemName As Range, OrderName As Range, RequiredData As Range

Dim wsOrder As Worksheet
Dim wsResult As Worksheet
Dim wsCondition As Worksheet
Dim wbOrder As Workbook
Dim wbCondition As Workbook



Dim OrderFile As String
Dim ConditionFile As String

'Open Order wb
OrderFile = Application.GetOpenFilename()
Set wbOrder = Workbooks.Open(OrderFile)

Set wsOrder = wbOrder.Worksheets(1)

'Open Condition wb
ConditionFile = Application.GetOpenFilename()

Set wbCondition = Workbooks.Open(ConditionFile)
Set wsCondition = wbCondition.Worksheets(2)
Set wsResult = wbCondition.Worksheets(1)


With wsResult
    .Range("A1").Value = "Product code"
    .Range("B1").Value = "Order Condition"
    .Range("C1").Value = "Order Name"
    .Range("D1").Value = "Subtotal"
    .Range("E1").Value = "Discount"
    .Range("F1").Value = "Quantity"
    .Range("G1").Value = "Item Name"
    .Range("H1").Value = "Country"

    .Range("A1").Characters(1, 12).Font.Bold = True
    .Range("B1").Characters(1, 16).Font.Bold = True
    .Range("C1").Characters(1, 16).Font.Bold = True
    .Range("D1").Characters(1, 12).Font.Bold = True
    .Range("E1").Characters(1, 12).Font.Bold = True
    .Range("F1").Characters(1, 12).Font.Bold = True
    .Range("G1").Characters(1, 12).Font.Bold = True
    .Range("H1").Characters(1, 12).Font.Bold = True

    .Range("A1").WrapText = True
    .Range("B1").WrapText = True
    .Range("C1").WrapText = True
    .Range("D1").WrapText = True
    .Range("E1").WrapText = True
    .Range("F1").WrapText = True
    .Range("G1").WrapText = True
    .Range("H1").WrapText = True

    .Range("A1").ColumnWidth = 13
    .Range("A1").RowHeight = 17
    .Range("B1").ColumnWidth = 12
    .Range("B1").RowHeight = 17
    .Range("C1").ColumnWidth = 14.5
    .Range("C1").RowHeight = 17
    .Range("G1").ColumnWidth = 99
    .Range("G1").RowHeight = 17
End With

'using the CountA ws function (all non-blanks)
myCount = Application.CountA(wsCondition.Range("A:A"))

For i = 2 To myCount Step 1
    strKeyWord = wsCondition.Range("A" & i)
    wsOrder.Range("R:R").AutoFilter Field:=1, Criteria1:="=*" & strKeyWord & "*"

    If wsOrder.Cells(Rows.Count, 1).End(xlUp).Row > 1 Then

        Set SubTotal = wsOrder.Range("I2", wsOrder.Range("I" & Rows.Count).End(xlUp))
        Set Country = wsOrder.Range("AG2", wsOrder.Range("AG" & Rows.Count).End(xlUp))
        Set DisCount = wsOrder.Range("N2", wsOrder.Range("N" & Rows.Count).End(xlUp))
        Set Quantity = wsOrder.Range("Q2", wsOrder.Range("Q" & Rows.Count).End(xlUp))
        Set OrderName = wsOrder.Range("A2", wsOrder.Range("A" & Rows.Count).End(xlUp))
        Set ItemName = wsOrder.Range("R2", wsOrder.Range("R" & Rows.Count).End(xlUp))

        Set RequiredData = Union(SubTotal, Country, DisCount, Quantity, OrderName, ItemName)
        RequiredData.SpecialCells(xlCellTypeVisible).Copy
        OrderCount = wsOrder.Range("A2", wsOrder.Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Cells.Count

        With wsResult
            If OrderCount >= 2 Then
                For N = 1 To OrderCount Step 1
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = strKeyWord
                    .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = "Available"
                Next N
            Else
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = strKeyWord
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = "Available"
            End If
                .Cells(.Rows.Count, "C").End(xlUp).Offset(1).PasteSpecial
        End With

    Else

        With wsResult
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = strKeyWord
            .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = "No Order"
            .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Value = "N/A"
            .Cells(.Rows.Count, "D").End(xlUp).Offset(1).Value = "N/A"
            .Cells(.Rows.Count, "E").End(xlUp).Offset(1).Value = "N/A"
        End With

    End If
        OrderCount = 0
Next i

wbCondition.Sheets("Result").Activate
wsOrder.AutoFilterMode = False

End Sub

1 个答案:

答案 0 :(得分:2)

无论此代码是在Mac还是Win环境中运行,这都无关紧要。您必须检查打开的文件中是否存在第二个工作表。您可以添加以下代码

slide

如果下标超出范围,则表示您尝试访问不存在的工作表。这可能是由于以下原因

  • 赋予工作表的工作表名称拼写错误。
  • 工作表的名称已更改。工作表已删除。
  • 索引很大,例如您使用了工作表(5),但是只有四个工作表
  • 使用了错误的工作簿,例如Workbooks(“ book1.xlsx”)。Worksheets(“ Sheet1”)代替Workbooks(“ book3.xlsx”)。Worksheets(“ Sheet1”)

您找到了这个here