2012-09-27 52 views
0

我想讓這個子工作,但沒有分配operationalRange變量。儘管函數selectBodyRow(bodyName)正常工作。如何從Sub/Function調用填充Range變量?

Sub sortRows(bodyName As String, ByRef wksht As Worksheet) 
    Dim operationalRange As Range 

    Set operationalRange = selectBodyRow(bodyName) 


    Debug.Print "Sorting Worksheet: " & wksht.Name 

    If Not operationalRange Is Nothing Then 
    operationalRange.Select 
    Debug.Print "Sorting " & operationalRange.Count & "Rows." 
    ActiveWorkbook.Worksheets(wksht.Name).Sort.SortFields.Clear 
    ActiveWorkbook.Worksheets(wksht.Name).Sort.SortFields.Add Key:=operationalRange, _ 
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 
    ActiveWorkbook.Worksheets(wksht.Name).Sort.SortFields.Add Key:=operationalRange, _ 
      SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets(wksht.Name).Sort 
      .SetRange operationalRange 
      .Header = xlGuess 
      .MatchCase = False 
      .Orientation = xlTopToBottom 
      .SortMethod = xlPinYin 
      .Apply 
    End With 
    Else 
    MsgBox "Body is not being Set" 
    End If 
End Sub 

小組由上述子稱爲是:

Function selectBodyRow(bodyName As String) As Range 
Dim rangeStart As String, rangeEnd As String 
Dim selectionStart As Range, selectionEnd As Range 
Dim result As Range, srchRng As Range, cngrs As Variant 

If bodyName = "WEST" Then 
    rangeStart = "<-WEST START->" 
    rangeEnd = "<-WEST END->" 
ElseIf bodyName = "EAST" Then 
    rangeStart = "<-EAST START->" 
    rangeEnd = "<-EAST END->" 
End If 

Set srchRng = Range("A:A") 
srchRng.Select 

Set selectionStart = srchRng.Find(What:=rangeStart, After:=ActiveCell, LookIn _ 
     :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False) 

Set selectionEnd = srchRng.Find(What:=rangeEnd, After:=ActiveCell, LookIn _ 
     :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False) 

Set result = Range(selectionStart.Offset(1, 0), selectionEnd.Offset(-1, 0)) 
result.EntireRow.Select 
End Function 
+2

你必須返回'selectBodyRow'中的內容...... – Passerby

+0

啊。什麼是正確的語法?返回似乎是爲錯誤保留的。 –

回答

2

嘗試,如路人建議,給你的函數設定爲範圍

set selectBodyRow=result 

如:

Function selectBodyRow(bodyName As String) As Range 
Dim rangeStart As String, rangeEnd As String 
Dim selectionStart As Range, selectionEnd As Range 
Dim result As Range, srchRng As Range, cngrs As Variant 

If bodyName = "WEST" Then 
    rangeStart = "<-WEST START->" 
    rangeEnd = "<-WEST END->" 
ElseIf bodyName = "EAST" Then 
    rangeStart = "<-EAST START->" 
    rangeEnd = "<-EAST END->" 
End If 

Set srchRng = Range("A:A") 
srchRng.Select 

Set selectionStart = srchRng.Find(What:=rangeStart, After:=ActiveCell, LookIn _ 
     :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False) 

Set selectionEnd = srchRng.Find(What:=rangeEnd, After:=ActiveCell, LookIn _ 
     :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False) 

Set result = Range(selectionStart.Offset(1, 0), selectionEnd.Offset(-1, 0)) 
result.EntireRow.Select 

set selectBodyRow=result 
'or set selectBodyRow=result.entirerow 
End Function 
+0

明白了。謝謝。 我知道這些都是基本的問題。 我在過去的兩週裏一直在試用自己的VBA。 –

+0

祝你好運,這就是我已經做了15年的工作 – nutsch

+0

葉神人。聽起來很痛苦。 –