2012-03-22 31 views
0

最初に渡されるよりも多くの引数を返すようにExcel関数を取得しようとしています。 Excel VBAを使用してからしばらくしていますが、これを実行できるかどうか疑問に思っていましたか?ここに私が開発してきたコードがあります。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 
+4

パラメータByRefではなく、ByVal –

答えて

2

さらに変数の位置がSelectColumn機能について説明する(メモリ)への参照を意味ByRefの値を渡す、ティム・ウィリアムズのコメントを解明します。 SelectColumn関数の中で、これらの変数の1つが変更されると、関数はその参照の変数、つまり元の変数の値を変更します。

変数ByValを渡すと、その変数の複製が生成されます。これは、SelectColumn関数内にのみ存在します。 SelectColumn内でそれを変更すると、オリジナルではなくこの複製が変更されます。

編集:

宣言を変更する必要があります。次の宣言時:

Dim strRow, strCol As Integer 

をあなたは両方の可能整数にstrRowとstrColを期待し、しかし、strRowバリアントである、strColは、(これは非常に私をgoads)の整数です。これらを次のように宣言する必要があります。

Dim strRow as Integer, _ 
    strCol as integer 

他の宣言と同様に、

+0

を渡してください。私は変更を行った。しかし、今、私は "コンパイルエラー:ByRef引数型の不一致"が発生しています これはデータ型の不一致ですか? – buck1112

+1

改訂版の回答を参照してください(具体的に質問に関係していなくても、コードを含める必要がありますので、ここに記入する必要はありません)。 – mkingston

+0

あなたの助けに感謝します - 私は同じことをして、それが解決策であることを発見しました。 – buck1112

関連する問題