2017-06-22 13 views
0

2つの文字列引数を渡して文字列型を返す関数を作成しようとしていますが、同じ関数をサブ関数を通じて呼び出しています。私がチェックしている間に議論のミスマッチを示唆する議論に誤りがあり、議論が正しい。問題がどこにあるのか分かりません。誰か助けてもらえますか?ここでは、コード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'を使っています。これはおそらく 'Mode'を意図しています。繰り返しますが、 'Option Explicit'はそのタイプミスについて教えてくれるでしょうし、コードがあなたが期待していることをしていない理由を頭に入れて何時間も頭を悩ます前に修正しなければなりません。 – YowE3K

答えて

0

あなたの問題はあなたがタイプStringByRef(デフォルト)パラメータにタイプVariantの変数を渡しているためであると思わ機能です。

常に変数を宣言する習慣を身に付ける必要があります。コードモジュールの最初の行にOption Explicitを使用して強制的に実行します。

以下のコード:

  • TableModeを宣言(プラス宣言されていなかったいくつかの他の変数)、あなたが持っていた変数(TableMode)を使用していたあなたの関数で
  • フィックスアップエラー決して宣言されておらず、値が割り当てられていません。
  • CurrentCellと呼ばれる特定の変数を導入することで)ActivateActiveCellの用途のほとんどを取り除くと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 
関連する問題