将工作表对象传递给另一个子获取"object doesn't support this property or method"



我正在编写一个宏,它与其他东西一起格式化了一堆很多工作表,我编写的一个子例程需要接受我之前定义和使用的工作表对象。当我尝试使用工作表对象作为参数调用子例程时,我得到一个"对象不支持此属性或方法">

Option Explicit
Public wb As Workbook
Public data, summary_month, summary_item, summary_charge, summary_pro As Worksheet
Public i, n, x, y, z As Variant
Public libName(), itype(), tcharge(), tprofile() As Variant
Public profile, pay_lib, item_auth, total_paid, month, item_type, chr_type As Variant
Public Sub main()
Set wb = ThisWorkbook
Set data = wb.Sheets(1)
Set summary_month = wb.Sheets(2)
Set summary_item = wb.Sheets(3)
Set summary_charge = wb.Sheets(4)
Set summary_pro = wb.Sheets(5)
With data
Set pay_lib = .Range( _
.Cells(2, 2), _
.Cells(2, 2).End(xlDown))
Set item_auth = .Range( _
.Cells(2, 4), _
.Cells(2, 4).End(xlDown))
Set month = .Range( _
.Cells(2, 3), _
.Cells(2, 3).End(xlDown))
Set total_paid = .Range( _
.Cells(2, 10), _
.Cells(2, 10).End(xlDown))
Set item_type = .Range( _
.Cells(2, 5), _
.Cells(2, 5).End(xlDown))
Set chr_type = .Range( _
.Cells(2, 6), _
.Cells(2, 6).End(xlDown))
Set profile = .Range( _
.Cells(2, 8), _
.Cells(2, 8).End(xlDown))
End With

'''''''''''''''''''''''''''''''''''''''''''''
''''''''''some other stuff'''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''

change_colors (summary_month)
End Sub
Sub change_colors(wksheet As Worksheet)
With wksheet
For Each i In .Range(.Cells(2, 1), .Cells(2, 1).End(xlDown))
If i.Row Mod 2 = 0 Then
.Range(.Cells(i.Row, i.Column), .Cells(i.Row, i.Column).End(xlToRight)).Interior.ColorIndex = 31
Else
.Range(.Cells(i.Row, i.Column), .Cells(i.Row, i.Column).End(xlToRight)).Interior.ColorIndex = 2
End If
Next i
End With

当调用子例程时,"with"语句应该根据行交替颜色,从我的测试来看,似乎没有任何圆润,我在调用子例程时出现错误。 两个子在同一个模块中。

显然,我对工作表对象的工作方式并不完全了解,任何帮助将不胜感激

单独声明工作表而不是内联

Option Explicit
Public wb As Workbook
Public i, n, x, y, z As Variant
Public libName(), itype(), tcharge(), tprofile() As Variant
Public profile, pay_lib, item_auth, total_paid, month, 
item_type, chr_type As Variant
Public Sub main()
Dim data As Worksheet
Dim summary_month As Worksheet
Dim summary_item As Worksheet
Dim summary_charge As Worksheet
Dim summary_pro As Worksheet
Set wb = ThisWorkbook
Set data = wb.Sheets(1)
Set summary_month = wb.Sheets(2)
Set summary_item = wb.Sheets(3)
Set summary_charge = wb.Sheets(4)
Set summary_pro = wb.Sheets(5)

并且还确保按以下方式而不是在括号中调用子例程

change_colors summary_month

相关内容

最新更新