私有子用户定义类型未定义范围表

时间:2018-01-24 20:47:50

标签: excel vba excel-vba compiler-errors

第一篇文章。我有下面相对简单的代码,我得到了一个

  

未定义的用户定义类型

错误。我知道当我将它放入一个Sub时,独立代码可以工作但由于各种原因我想将它拆分出来,以便在我的大型工作簿中我可以调用第二个子而不必复制并粘贴整个循环多个倍。代码的目的是在excel中自动调整指定范围。

Sub letsGo()
Dim rng As Range
Dim sht As Worksheet
Set rng = ThisWorkbook.Sheets("Sheet1").Range("Range1")
Set sht = ThisWorkbook.Sheets("Sheet1")
Call whyDoesntThisWork(sht, rng)
End Sub

Private Sub whyDoesntThisWork(rangeSheet As Sheet, rangeTable As Range)
Dim Col As Range
Dim reSize As Range
For Each Col In rangeTable.Columns
    If Col.Hidden = False Then
        Set reSize = rangeSheet.Range(rangeSheet.Cells(rangeTable.Row, Col.Column), rangeSheet.Cells(rangeTable.Rows.Count, Col.Column)) reSize.Columns.autoFit
    End If
Next Col
End Sub

2 个答案:

答案 0 :(得分:2)

您有两种不同的数据类型:

Private Sub whyDoesntThisWork(rangeSheet As Sheet, rangeTable As Range)

rangeSheet是一张工作表,但是当你调用它时,你传递:

Call whyDoesntThisWork(sht, rng)

sht is of type WorkSheet

这是你的不一致。我建议您将定义更改为:

Private Sub whyDoesntThisWork(rangeSheet As WorkSheet, rangeTable As Range)

答案 1 :(得分:-1)

rangeSheet As Sheet更改为rangeSheet As Worksheet