2017-06-22 56 views
0

我想寫一個函數,傳遞兩個字符串參數並返回字符串類型,我通過一個子調用相同的函數。我收到了一個錯誤提示參數不匹配的論據,而我檢查了這些參數是正確的。看不清楚問題出在哪裏。有人可以幫忙嗎?下面是代碼在VBA中調用函數

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 
+4

聲明你的變量。 (使用'Option Explicit'作爲你的代碼模塊的第一行來強制你這麼做。)聲明'TableMode As String',我認爲你的問題會消失(假設你得到一個「ByRef參數類型不匹配」的錯誤在'TableCatCalling(Key,TableMode)')。 – YowE3K

+0

嘗試將'Key'和'Mode'更改爲'iKey'和'iMode' – Quint

+0

另請注意,您在函數中使用未定義的變量'TableMode'。這可能是爲了「模式」。再次,'Option Explicit'會告訴你這個錯字,並迫使你在你花費數小時抓住你的頭腦之前解決它,想知道爲什麼代碼沒有做到你期望的。 – YowE3K

回答

0

您的問題似乎是因爲您傳遞Variant類型的變量String類型的ByRef(默認)參數的函數。

你應該養成總是聲明變量的習慣。使用Option Explicit作爲您的代碼模塊的第一行,強制您這樣做。

下面的代碼:

  • 聲明TableMode(加上沒有宣佈其他幾個變量),
  • 修復了錯誤的功能,你使用它有一個變量(TableMode)從未被聲明或賦值。
  • 得到,ActiveCell(通過引入稱爲CurrentCell特定的變量)和Select

希望這可以解決您的問題,去掉大部分的Activate的用途。

'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