2012-03-22 2 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

ByVal이 아닌 ByRef를 전달하십시오. –

답변

2

더욱 SelectColumn 함수에 대하여 설명한다 변수의 (메모리)의 위치에 대한 참조를 의미하는 ByRef 값을 전달 팀 윌리엄스 '주석 해명. SelectColumn 함수에서 변수 중 하나가 변경되면 함수는 해당 참조에서 변수의 값, 즉 원래 변수를 변경합니다.

ByVal 변수를 전달하면 해당 변수의 복제본이 생성됩니다. 이것은 SelectColumn 함수 내에 만 존재합니다. SelectColumn에서 변경하면 원본이 아닌이 복제본이 변경됩니다.

편집 :

신고 내용을 변경해야합니다. 다음과 같은 선언 할 때 :

Dim strRow, strCol As Integer 

당신은 둘 다 할 수 정수로 strRow 및 strCol을 기대하지만, strRow는 strCol는 정수 (이 크게 저를 찌르는 채찍) 변형입니다. 다음과 같이 선언해야합니다.

Dim strRow as Integer, _ 
    strCol as integer 

다른 선언과 마찬가지로.

+0

많은 감사합니다. 나는 변화를 만들었다. 그러나 이제 "컴파일 오류 : ByRef 인수 형식이 일치하지 않습니다." 데이터 형식이 맞지 않습니까? – buck1112

+1

수정 된 답변을 참조하십시오 (질문에 특별히 관련되지는 않지만 코드를 포함시켜야하므로 여기에 넣지 않아도됩니다). – mkingston

+0

도움을 주셔서 감사합니다. 동일한 결과를 얻었습니다. – buck1112