VBA代码中的对象必需错误

时间:2018-07-26 03:23:06

标签: excel vba excel-vba

目标: 我有1个字符串数组,1个工作表数组。要计算每个工作表中每个字符串的出现次数。

我的代码:

Option Explicit

Sub Checking()
    Dim banks() As String
    Dim countrysheets As Variant
    Dim sheet As Worksheet
    Dim bank As Variant
    Dim CountryList, BankList As String
    Dim count
    Dim bankcount, sheetcount As Integer

    CountryList = "CN,AU,India,Thai,TW,INDO-IDR,MY,PHP-LOCAL,SG,SK,NZ"

    BankList = "Merrill Lynch,UBS,Citigroup,BNP,Macquarie,Morgan Stanley,Deutsche Bank,HSBC,CLSA,JP Morgan,Credit Suisse, Nomura, Goldman Sachs"

    countrysheets = Split(CountryList, ",")        
    banks = Split(BankList, ",")

    sheetcount = 0        
    For Each sheet In countrysheets        
        bankcount = 0            

        For Each bank In banks            
            count = Application.sheets(sheet).WorksheetFunction.CountIf(sheet.Cells, bank)                
            ActiveCell.Offset(bankcount, sheetcount).Value = count                
            bankcount = bankcount + 1                
        Next

        sheetcount = sheetcount + 1                
    Next

End Sub

为什么我不断收到对象必需的错误?

谢谢!

3 个答案:

答案 0 :(得分:1)

countrysheets = Split(CountryList, ",")将生成一个字符串数组,而不是工作表对象的集合,因此是您的第一个错误。与银行相同。

For i = LBound to UBound循环使用数组的速度更快。然后,您可以使用生成的工作表名称列表通过ThisWorkbook.Worksheets(countrysheets(i))访问工作表。当前数组索引处的字符串名称用作参考。

当您不声明类型时,则它隐式为变体。所以像 Dim bankcount, sheetcount As Integer。仅最后一个为Integer,其他为VariantInteger可能会溢出,因此请继续使用Long.

使用Worksheets而不是Sheets集合来避免图表。如果这些名称不引用图表,虽然对于命名表不是问题。

在“,”上分割银行时,请注意银行名称中可能留有多余的空白,这意味着您不会获得预期的结果。我正在查看您的前两个银行名称,其中前导空格是空白。

它是Application.WorksheetFunction.Countif

重新编写可能类似于:

Option Explicit
Public Sub Checking()
    Dim banks() As String, countrysheets() As String, CountryList As String, BankList As String
    Dim count As Long, bankcount As Long, sheetcount As Long

    CountryList = "CN,AU,India,Thai,TW,INDO-IDR,MY,PHP-LOCAL,SG,SK,NZ"
    BankList = "Merrill Lynch,UBS,Citigroup,BNP,Macquarie,Morgan Stanley,Deutsche Bank,HSBC,CLSA,JP Morgan,Credit Suisse,Nomura,Goldman Sachs"
    countrysheets = Split(CountryList, ",")
    banks = Split(BankList, ",")
    sheetcount = 0
    Dim i As Long, j As Long, ws As Worksheet
    For i = LBound(countrysheets) To UBound(countrysheets)
        Set ws = ThisWorkbook.Worksheets(countrysheets(i))
        bankcount = 0
          For j = LBound(banks) To UBound(banks)
            count = Application.WorksheetFunction.CountIf(ws.Cells, banks(j))
            ActiveCell.Offset(bankcount, sheetcount).Value = count
         bankcount = bankcount + 1
        Next
        sheetcount = sheetcount + 1
    Next
End Sub

答案 1 :(得分:0)

For Each sheet In countrysheets

您是国家工作表,是变体而不是工作表的集合

这将起作用。

Dim str As Variant

For Each str In countrysheets

或者您可以不同地声明数组。可以声明为工作表数组,但要复杂一些。这可能就足够了,您可以在循环内调用Sheets(str)

Dim countrysheets() As String

Dim str As string

For Each str In countrysheets

未设置表格

 count = Application.Sheets(sheet).WorksheetFunction.CountIf(sheet.Cells, bank)

计数未声明为任何内容

Dim count

答案 2 :(得分:0)

您可以使用接受数组作为其参数的Sheets对象遍历图纸列表

显式选项

Sub Checking()
    Dim banks As Variant, bank As Variant, countrysheets As Variant
    Dim sheet As Worksheet 
    Dim CountryList As String, BankList As String
    Dim count As Long, bankcount As Long, sheetcount As Long

    CountryList = "CN,AU,India,Thai,TW,INDO-IDR,MY,PHP-LOCAL,SG,SK,NZ"

    BankList = "Merrill Lynch,UBS,Citigroup,BNP,Macquarie,Morgan Stanley,Deutsche Bank,HSBC,CLSA,JP Morgan,Credit Suisse, Nomura, Goldman Sachs"

    countrysheets = Split(CountryList, ",")        
    banks = Split(BankList, ",")

    For Each sheet In Sheets(countrysheets)       
        bankcount = 0            

        For Each bank In banks            
            count = Application.WorksheetFunction.CountIf(sheet.UsedRange, bank)                
            ActiveCell.Offset(bankcount, sheetcount).Value = count                
            bankcount = bankcount + 1                
        Next

        sheetcount = sheetcount + 1                
    Next

End Sub