我正在尝试写入Select Case函数,一个用于两个excel工作表,另一个用于一系列单元格。这些函数用于索引排序过程的工作表和范围。但是,函数要求我返回值,我不确定要返回什么值。我无法返回参数。基本上,我希望我的sort sub遍历两个工作表和两个范围,并在函数的选择范围内对工作表进行排序。
如果有更好的方法,我也愿意接受建议。
这是我的代码:
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
答案 0 :(得分:1)
尝试使用Return
语句而不是旧版FunctionName = ReturnValue
,然后,如果您超越Select
语句,肯定会出错:
Public Function SheetsToSort(Index As Long) As Excel.Worksheet
Select Case Index
Case 1 : Return xlWSEmployee
Case 2 : Return xlWSPosition
End Select
Throw New ArgumentOutOfRangeException("Index")
End Function
Public Function GetRange(index As Long) As Excel.Range
Select Case index
Case 1 : Return xlWSEmployee.Range("A1")
Case 2 : Return xlWSPosition.Range("W1")
End Select
Throw New ArgumentOutOfRangeException("index")
End Function