需要对象 - 将变量传递给函数
Object required - Passing variable to Function
我第一次尝试 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:$BF000")
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
问题是使用关键字 Set
。 Set
用于设置对象引用,而不是简单地赋值。
您在此处正确分配了一个字符串:
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:$BF000")
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
对于您的函数,您返回的是一个字符串,因此 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. Or you can follow a YouTube video tutorial on Dim
vs Set
and their appropriate uses here。
我第一次尝试 Excel VBA 已经从一个 Access 项目中学到了一些 VBA。我试图在过滤后的数据集中找到第一个和第二个可见单元格。我目前的方法是通过子函数找到第一个可见单元格,然后使用第一个单元格引用找到下一个。我在 Vis1cell =
代码如下:
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:$BF000")
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
问题是使用关键字 Set
。 Set
用于设置对象引用,而不是简单地赋值。
您在此处正确分配了一个字符串:
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:$BF000")
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
对于您的函数,您返回的是一个字符串,因此 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. Or you can follow a YouTube video tutorial on Dim
vs Set
and their appropriate uses here。