Sunday, 15 September 2013

Select Case Function asking for Return Value

Select Case Function asking for Return Value

I am trying to write to Select Case functions, one for two excel
worksheets and one for a range of cells. The functions are used to index
the sheets and ranges for a sorting procedure. However, the Functions are
asking me for a return value, and I am not sure what value to return. I
can't return my parameter. Basically, I want my sort sub to loop through
both sheets and both ranges and sort the sheets in the select ranges in
the function.
If there is a better approach, I am also open to suggestions.
Here is my code:
Dim xlWB As Excel.Workbook =
CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
Dim xlWSEmployee As Excel.Worksheet =
CType(CType(xlWB.Sheets("byEmployee"), Excel.Worksheet), Excel.Worksheet)
Dim xlWSPosition As Excel.Worksheet =
CType(CType(xlWB.Sheets("byPosition"), Excel.Worksheet), Excel.Worksheet)
Public Function SheetsToSort(Index As Long) As Excel.Worksheet
Select Case Index
Case 1 : SheetsToSort = xlWSEmployee
Case 2 : SheetsToSort = xlWSPosition
End Select
***Return (?)***
End Function
Public Function GetRange(index As Long) As Excel.Range
Select Case index
Case 1 : GetRange = xlWSEmployee.Range("A1")
Case 2 : GetRange = xlWSPosition.Range("W1")
End Select
***Return (?)***
End Function
Sub Sort_Sheets()
Dim refSheets As Excel.Worksheet
Dim sortRange As Excel.Range
Dim x As Long
'This is Step 6 when the frmDataImportSplash is activated.
'The key formulas only work if the data is sorted properly.
The procedure
'below sorts each sheet by the required field. Look at the
public_Declarations module
'for the pass byRef.
For x = 1 To 2 Step 1
refSheets = SheetsToSort(x)
sortRange = GetRange(x)
With refSheets
.Sort.SortFields.Clear()
.Sort.SortFields.Add(Key:=(refSheets.sortRange),
SortOn:=XlSortOn.xlSortOnValues,
Order:=Excel.XlSortOrder.xlAscending,
DataOption:=XlsortDataoption.xlSortNormal)
With .Sort
.Header = XlYesNoGuess.xlYes
.MatchCase = False
.Orientation = Excel.XlSortOrientation.xlSortColumns
.SortMethod = Excel.XlSortMethod.xlPinYin
.Apply()
End With
End With
Next x
End Sub

No comments:

Post a Comment