Excel VBA - 不从函数返回的值

时间:2012-03-22 21:16:20

标签: excel function vba arguments

我试图让Excel函数返回的参数多于最初传递给它的参数。我使用Excel VBA已经有一段时间了,但我想知道是否可以这样做?这是我一直在开发的代码。

函数(“SelectColumn”)需要返回6个值,我在下面的代码中将它们作为参数。 非常感谢任何帮助。

Sub match_names3()
 Dim i As Integer
 Dim strRow, strCol As Integer
 Dim UpBound, LowBound As Range

 Dim strUpBoundRow, strUpBoundColumn, strLowBoundRow, strLowBoundColumn As Integer
 Dim CompareRange_alum_names As Range
 Dim CompareRange_bio_names As Range
 Dim alum As Variant, bio As Variant
 Dim AlumCount, BioCount As Long
 strRow = 2
 strCol = 8
 strUpBoundRow = 0
 strUpBoundColumn = 0
 strLowBoundRow = 0
 strLowBoundColumn = 0

 SelectColumn strRow, strCol, strUpBoundRow, strUpBoundColumn, strLowBoundRow, strLowBoundColumn


 Set CompareRange_alum_names = Worksheets("Sheet1").Range(Cells(strUpBoundRow, strUpBoundColumn) & ":" & Cells(strLowBoundRow, strLowBoundColumn))


 strRow = 2
 strCol = 17
 strUpBoundRow = 0
 strUpBoundColumn = 0
 strLowBoundRow = 0
 strLowBoundColumn = 0

 SelectColumn strRow, strCol, strUpBoundRow, strUpBoundColumn, strLowBoundRow, strLowBoundColumn


 Set CompareRange_alum_names = Worksheets("Sheet1").Range(Cells(strUpBoundRow, strUpBoundColumn) & ":" & Cells(strLowBoundRow, strLowBoundColumn))

 AlumCount = 2
     For Each alum In CompareRange_alum_names

      BioCount = 2
         For Each bio In CompareRange_bio_names

             If bio.Value = alum.Value Then

             Cells(AlumCount, 19).Value = Cells(BioCount, 16)
             End If
          BioCount = BioCount + 1
          Next bio

     AlumCount = AlumCount + 1
     Next alum


 End Sub



 Function SelectColumn(ByVal strRow As Integer, ByVal strCol As Integer, ByVal strUpBoundRow As Integer, ByVal strUpBoundColumn As Integer, ByVal strLowBoundRow As Integer, ByVal strLowBoundColumn As Integer)

 Dim UpBound As Range
 Dim LowBound As Range

 Worksheets("Sheet1").Cells(strRow, strCol).Select

 If ActiveCell.Row > 1 Then
     If IsEmpty(ActiveCell.Offset(-1, 0)) Then
     Set UpBound = ActiveCell
     Else
     Set UpBound = ActiveCell.End(xlUp)
     End If
 Else
     Set UpBound = ActiveCell
 End If
 strUpBoundRow = UpBound.Row
 strUpBoundColumn = UpBound.Column
 MsgBox ("strUpBoundRow " & strUpBoundRow)
 MsgBox ("strUpBoundColumn " & strUpBoundColumn)


 If ActiveCell.Row < Rows.Count Then
     If IsEmpty(ActiveCell.Offset(1, 0)) Then
     Set LowBound = ActiveCell
     Else
     Set LowBound = ActiveCell.End(xlDown)
     End If
 Else
     Set LowBound = ActiveCell
 End If
 strLowBoundRow = LowBound.Row
 strLowBoundColumn = LowBound.Column
 MsgBox ("strLowBoundRow " & strLowBoundRow)
 MsgBox ("strLowBoundColumn " & strLowBoundColumn)


 Range(UpBound, LowBound).Select


 Set UpBound = Nothing
 Set LowBound = Nothing

 End Function

1 个答案:

答案 0 :(得分:2)

为了进一步阐明Tim Williams的评论,传递值ByRef意味着对SelectColumn函数的变量(存储器)位置的引用将被赋予。在SelectColumn函数内部,当其中一个变量发生变化时,该函数将改变该参考变量的值,即原始变量。

传递变量ByVal时,会生成该变量的副本。这仅存在于SelectColumn函数中。如果在SelectColumn中更改它,则会更改此副本,而不是原始副本。

编辑:

您需要更改声明。声明以下内容时:

Dim strRow, strCol As Integer

你希望strRow和strCol都是整数,但strRow是一个变量,strCol是一个整数(这极大地引起了我的兴趣)。您需要按如下方式声明:

Dim strRow as Integer, _
    strCol as integer

与许多其他声明类似。