简体   繁体   English

VBA代码中的对象必需错误

[英]Object required error in VBA code

Objective: I have 1 Array of strings, 1 Array of worksheets. 目标:我有1个字符串数组,1个工作表数组。 Want to count the number of occurrences of each string in each worksheet. 要计算每个工作表中每个字符串的出现次数。

My code: 我的代码:

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

Why do I keep getting object required errors? 为什么我不断收到对象必需的错误?

Thanks! 谢谢!

countrysheets = Split(CountryList, ",") will generate a string array not a collection of worksheet objects hence your first error. countrysheets = Split(CountryList, ",")将生成一个字符串数组,而不是工作表对象的集合,因此是您的第一个错误。 Same with banks. 与银行相同。

A For i = LBound to UBound Loop is faster with arrays. For i = LBound to UBound数组, For i = LBound to UBound循环更快。 You can then use the generated list of sheet names to access sheets with ThisWorkbook.Worksheets(countrysheets(i)) . 然后,您可以使用生成的工作表名称列表通过ThisWorkbook.Worksheets(countrysheets(i))访问工作表。 The string name at the current array index is used as a reference. 当前数组索引处的字符串名称用作参考。

When you don't declare a type then it is implicitly a variant. 当您不声明类型时,则隐式为变量。 So lines like Dim bankcount, sheetcount As Integer . 所以像Dim bankcount, sheetcount As Integer这样的行。 Only the last is an Integer and the others are Variant . 只有最后一个是Integer ,其他是Variant And Integer risks overflow so go with Long. Integer溢出的风险,因此请使用Long.

Use the Worksheets , not Sheets , collection to avoid chart sheets. 使用Worksheets而不是Sheets集合来避免使用图表。 Though not a problem with named sheets if those names do not refer to a chart sheet. 如果这些名称不引用图表,则命名表不是问题。

When splitting banks on ",", be careful of any unwanted whitespace that may be left included in a bank name meaning you don't get the results you expect. 在“,”上分割银行时,请注意银行名称中可能会留有多余的空白,这意味着您无法获得预期的结果。 I was looking at your last two banks names where there is leading white space. 我正在查看您的前两个银行名称,其中空格领先。

It is Application.WorksheetFunction.Countif . 它是Application.WorksheetFunction.Countif

A re-write might look like: 重写可能看起来像:

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

You can use Sheets object that accepts an array as its argument to loop through a list of sheets 您可以使用接受数组作为其参数的Sheets对象遍历工作表列表

Option Explicit 显式期权

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM