在VBA中调用函数

时间:2017-06-22 09:00:10

标签: vba excel-vba excel

我正在尝试编写一个传递两个字符串参数并返回字符串类型的函数,我通过一个子函数调用相同的函数。我得到一个错误的参数暗示参数不匹配,而我检查和参数是正确的。无法了解问题所在。有人可以帮忙吗?这是代码

    Sub TableCat()
    Dim Key As String
    Worksheets("Table").Activate
    Call RemoveDupes
    Do Until ActiveCell.Value = ""
     TableMode = Application.Trim(ActiveCell.Value)
     TableId = Application.Trim(ActiveCell.Offset(0, -1).Value)
     ControlID = Application.Trim(ActiveCell.Offset(0, -2).Value)
     Key = ControlID & TableId
     ActiveCell.Offset(0, 1).Value = TableCatCalling(Key, TableMode)
    ActiveCell.Offset(1, 0).Select
    Loop
    End Sub

功能

    Function TableCatCalling(Key As String, Mode As String) As String
    Dim CatCell As Range
    Dim searchRange As Range
    Worksheets("CCM Analysisv2").Activate
    Set searchRange = Range("C1", Range("C1").End(xlDown))
    Set CatCell = searchRange.Find(what:=Key, lookAt:=xlWhole)
    If CatCell Is Nothing Then
     TableCatCalling = " "
    Else
     If TableMode Like "New" Then
        TableCatCalling = CatCell.Offset(0, -1).Value
     End If
    End If
    Worksheets("Table").Activate
    End Function

1 个答案:

答案 0 :(得分:0)

您的问题似乎是因为您将Variant类型的变量传递给ByRef类型的String(默认情况下)参数。

你应该养成总是声明变量的习惯。使用Option Explicit作为代码模块的第一行强制您执行此操作。

以下代码:

  • 声明TableMode(加上一些未声明的其他变量),
  • 修复了函数中使用从未声明或赋值的变量(TableMode)的错误。
  • 摆脱了ActivateActiveCell的大部分用途(通过引入名为CurrentCell的特定变量)和Select

希望它能解决你的问题。

'Use "Option Explicit" to force you to declare variables
Option Explicit

Sub TableCat()
    'Declare variables that weren't declared before
    Dim TableMode As String
    Dim TableId As String
    Dim ControlID As String
    Dim CurrentCell As Range

    Dim Key As String
    'These two lines should be replaced with something which doesn't
    'use Activate and ActiveCell
    Worksheets("Table").Activate
    Set CurrentCell = ActiveCell

    RemoveDupes
    Do Until CurrentCell.Value = ""
        TableMode = Application.Trim(CurrentCell.Value)
        TableId = Application.Trim(CurrentCell.Offset(0, -1).Value)
        ControlID = Application.Trim(CurrentCell.Offset(0, -2).Value)
        Key = ControlID & TableId
        CurrentCell.Offset(0, 1).Value = TableCatCalling(Key, TableMode)
        Set CurrentCell = CurrentCell.Offset(1, 0)
    Loop
End Sub

Function TableCatCalling(Key As String, Mode As String) As String
    Dim CatCell As Range
    Dim searchRange As Range
    With Worksheets("CCM Analysisv2") 'Avoid activating the worksheet
        Set searchRange = .Range("C1", .Range("C1").End(xlDown))
    End With
    Set CatCell = searchRange.Find(what:=Key, lookAt:=xlWhole)
    If CatCell Is Nothing Then
        TableCatCalling = " "
    Else
        'This should refer to "Mode", not "TableMode"
        If Mode Like "New" Then
            TableCatCalling = CatCell.Offset(0, -1).Value
        End If
        'Note: You are not setting a return value if "Mode" is not like "New"
        'so your function will return "" by default
    End If
End Function