需要的对象 - 将变量传递给Function

时间:2015-04-23 16:31:12

标签: excel-vba parameter-passing vba excel

我正在第一次尝试Excel VBA,从Access项目中学到了一些VBA。我试图在过滤的数据集上找到第一个和第二个可见单元格。我目前的方法是通过子函数找到第一个可见单元格,然后使用该第一个单元格引用来查找下一个单元格。我在Vis1cell =

上编译时遇到了Object required错误

以下是代码:

Sub volvar()
'
' Volume Var Sort Macro
'
Dim rng As Range
Dim filterDept As Range

Dim volrng As String
Dim Vis1 As Range
Dim Vis1cell As String
Dim Vis2 As Range
Dim Vis2cell As String

Set rng = Range("$A$12:$BF$45000")
Set filterDept = Range("D8")

'volrng is static and just needs to be passed to the function.
volrng = "J12"
With ThisWorkbook.Sheets("Comparison")
    Set Vis1cell = VolVis(volrng)
    Set Vis1 = Range(Vis1cell)
    Set Vis2cell = VolVis(Vis1cell)
    Set Vis2 = Range(Vis2cell)

    Debug.Print "Vis1"; Vis1
    Debug.Print "Vis2"; Vis2
End With

    'if FilterCheck = 1, sort based on dept and then order
    If FilterCheck = 1 Then
        If Vis1 > Vis2 Then
            Debug.Print "FilterCheck 1 Asc"
            With ThisWorkbook.Sheets("Comparison")
                .AutoFilterMode = False
                rng.AutoFilter Field:=1, Criteria1:=filterDept.Value
            End With
            GoTo FilterAsc
        Else
            Debug.Print "FilterCheck 1 Desc"
            With ThisWorkbook.Sheets("Comparison")
                .AutoFilterMode = False
                rng.AutoFilter Field:=1, Criteria1:=filterDept.Value
            End With
            GoTo FilterDesc
        End If
    Else
        If Range("J13") > Range("J14") Then
            Debug.Print "FilterCheck 0 Asc"
            GoTo FilterAsc
        Else
            Debug.Print "FilterCheck 0 Desc"
            GoTo FilterDesc
        End If
    End If

FilterAsc:
    Debug.Print "Asc Goto"
    With ThisWorkbook.Sheets("Comparison")
        rng.AutoFilter Field:=10
        rng.CurrentRegion.Sort Key1:=.Range("J12"), Order1:=xlAscending, _
            Header:=xlYes, DataOption1:=xlSortNormal
    End With
    Exit Sub

FilterDesc:
    Debug.Print "Desc Goto"
    With ThisWorkbook.Sheets("Comparison")
        rng.AutoFilter Field:=10
        rng.CurrentRegion.Sort Key1:=.Range("J12"), Order1:=xlDescending, _
            Header:=xlYes, DataOption1:=xlSortNormal
    End With
    Exit Sub

End Sub

这是我试图传递的功能:

Function VolVis(rng As String) As String

    ActiveSheet.Range(rng).Select
    'Set rng = Range("J12")
    ActiveCell.Offset(1, 0).Select
    Do Until ActiveCell.EntireRow.Hidden = False
        ActiveCell.Offset(1, 0).Select
    Loop

    Set VolVis = ActiveCell.Address

    Debug.Print "Cell Address"; VolVis

End Function

1 个答案:

答案 0 :(得分:3)

问题在于使用关键字SetSet用于设置对象引用,而不是简单地指定值。

您在此处正确分配了一个字符串:

volrng = "J12"

Vis1cell被声明为字符串,但您正在尝试引用一个对象。

该行:

Set Vis1cell = VolVis(volrng)

应该只是:

Vis1cell = VolVis(volrng)

现在,Range是需要设置的对象。这一行:

Set Vis1 = Range(Vis1cell)

是对的。

您还经常引用Range对象,并且您希望尝试比较两个值。要访问范围的值,您必须使用Range.Value

例如,如果您尝试比较J13和J14中的两个值,则不起作用:

Range("J13") > Range("J14")

您尚未指定要比较的范围 。由于您使用的是>运算符,因此我将假设这些值。要比较值,您必须执行以下操作:

Range("J13").Value > Range("J14").Value

我已在我的代码中进行了适当的更改,并在评论'JR中添加了我的首字母,以帮助您了解它的外观。

Sub volvar()
'
' Volume Var Sort Macro
'
Dim rng As Range
Dim filterDept As Range

Dim volrng As String
Dim Vis1 As Range
Dim Vis1cell As String
Dim Vis2 As Range
Dim Vis2cell As String

Set rng = Range("$A$12:$BF$45000")
Set filterDept = Range("D8")

'volrng is static and just needs to be passed to the function.
volrng = "J12"
With ThisWorkbook.Sheets("Comparison")
    Vis1cell = VolVis(volrng) 'JR
    Set Vis1 = Range(Vis1cell)
    Vis2cell = VolVis(Vis1cell) 'JR
    Set Vis2 = Range(Vis2cell)

    Debug.Print "Vis1"; Vis1.Address 'JR
    Debug.Print "Vis2"; Vis2.Address 'JR
End With

    'if FilterCheck = 1, sort based on dept and then order
    If FilterCheck = 1 Then
        If Vis1.Value > Vis2.Value Then 'JR
            Debug.Print "FilterCheck 1 Asc"
            With ThisWorkbook.Sheets("Comparison")
                .AutoFilterMode = False
                rng.AutoFilter Field:=1, Criteria1:=filterDept.Value
            End With
            GoTo FilterAsc
        Else
            Debug.Print "FilterCheck 1 Desc"
            With ThisWorkbook.Sheets("Comparison")
                .AutoFilterMode = False
                rng.AutoFilter Field:=1, Criteria1:=filterDept.Value
            End With
            GoTo FilterDesc
        End If
    Else
        If Range("J13").Value > Range("J14").Value Then 'JR
            Debug.Print "FilterCheck 0 Asc"
            GoTo FilterAsc
        Else
            Debug.Print "FilterCheck 0 Desc"
            GoTo FilterDesc
        End If
    End If

FilterAsc:
    Debug.Print "Asc Goto"
    With ThisWorkbook.Sheets("Comparison")
        rng.AutoFilter Field:=10
        rng.CurrentRegion.Sort Key1:=.Range("J12"), Order1:=xlAscending, _
            Header:=xlYes, DataOption1:=xlSortNormal
    End With
    Exit Sub

FilterDesc:
    Debug.Print "Desc Goto"
    With ThisWorkbook.Sheets("Comparison")
        rng.AutoFilter Field:=10
        rng.CurrentRegion.Sort Key1:=.Range("J12"), Order1:=xlDescending, _
            Header:=xlYes, DataOption1:=xlSortNormal
    End With
    Exit Sub

End Sub

对于您的Function,您将返回一个字符串,因此Set关键字的语法不正确。它应该看起来像:

Function VolVis(rng As String) As String

    ActiveSheet.Range(rng).Select
    'Set rng = Range("J12")
    ActiveCell.Offset(1, 0).Select
    Do Until ActiveCell.EntireRow.Hidden = False
        ActiveCell.Offset(1, 0).Select
    Loop

    VolVis = ActiveCell.Address

    Debug.Print "Cell Address"; VolVis

End Function

您可以详细了解如何正确使用Set关键字here。或者,您可以关注Dim vs Set上的YouTube视频教程及其相应用途here