我正在尝试编写一个可以称为单元格公式的自定义函数。它基本上采用源范围、目标范围、排序范围以及数据应从何处求和作为输入参数。该函数从源范围复制并粘贴到目标范围。然后在排序范围的帮助下进行排序。之后,它应该对指定数量的列删除剩余数据后的数据求和
Function Sort_Column(MaCol As String, SortCol As String, TarCol As String, x As Long)
Dim OneRange As Range
Dim Ws As Worksheet`enter code here`
Set Ws = Workbooks("test4.xlsm").Worksheets("Sheet1")
Ws.Range(MaCol & "," & SortCol).Copy
Ws.Range(TarCol).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Set OneRange = Ws.Range(TarCol)
TarColNa = Right(TarCol, 1)
TarColRa = Right(TarCol, 1) & "2"
Set acell = Ws.Range(TarColRa)
OneRange.Sort Key1:=acell, Order1:=xlDescending, Header:=xlYes
TarCell = Range(TarColNa & 1).Column
LastRow = Cells(Rows.Count, TarCell).End(xlUp).Row
Y = x + 1
SumCol = "=SUM(" & TarColNa & Y & ":" & TarColNa & LastRow & ")"
Range(TarColNa & Y) = SumCol
Range(Left(TarCol, 1) & Y) = " All Other"
Range(Left(TarCol, 1) & Y + 1 & ":" & Left(TarCol, 1) & LastRow).ClearContents
Range(TarColNa & Y + 1 & ":" & TarColNa & LastRow).ClearContents
Range(Left(TarCol, 1) & "2").Select
End Function
不幸的是,上面的用户定义函数在尝试作为单元格公式时返回#VALUE
任何形式的帮助表示赞赏。
先感谢您