Excel 2013 VBA按排名sorting

所以这里是问题:

我需要插入input到模板的值。 听起来很简单,对吧? 那么问题是,这些值并不总是按照数字顺序,所以它会扰乱我的插值函数。 我的插值函数有一个x值的input(在这种情况下,HP,这是需要对数据进行sorting而不会弄乱与其有关的数据的参数),y值(你想在?HPfind的参数)和x_value在哪里你想findy参数。 一些y参数是方程式,所以我想不出一种方法来完全重新排列列而不会搞乱数据。 我认为应该有一种方法来引用HP中的HP排名,select相关的y值,并使用这些值进行插值。 这就是说,我似乎无法弄清楚如何去做。 我一直在研究一些代码。 我从来没有真正使用VBA,所以它一直在为我的钱跑。 我到目前为止的代码如下:

Function organize(ByVal x As Object) ' Declarations for finding Ranks Dim Array_Size As Integer Array_Size = Application.WorksheetFunction.Count(x.cells) + Application.WorksheetFunction.CountBlank(x.cells) Dim Ranks As Variant ReDim Ranks(1 To Array_Size) Dim j As Integer 'initiate counter For j = 1 To Array_Size If x.cells(j).Value <> 0 Then Ranks(j) = Application.WorksheetFunction.Rank_Eq(x.cells(i).Value, x.cells, 1) 'assign rank to i'th position in array End If Next j organize = Ranks() End Function Function lin_2xy(ByVal x1 As Single, ByVal y1 As Single, _ ByVal x2 As Single, ByVal y2 As Single, _ ByVal x_value As Single) If x1 = x2 Then lin_2xy = [#N/A] Else lin_2xy = y1 + (y2 - y1) / (x2 - x1) * (x_value - x1) End If End Function Function Sort_Then_Interpolate(ByVal x As Object, ByVal y As Object, ByVal x_value As Single) 'Declarations for interpolating Dim i As Integer ' counter. Dim Current_x As Single ' x value Dim Next_x As Single ' next higher x value Dim Current_y As Single ' y value Dim Next_y As Single ' next higher y value Dim LFound As Boolean ' = true if found. Dim matrix() As Variant matrix = organize(x.cells) LFound = False For i = 1 To UBound(matrix) match_value = Application.WorksheetFunction.Match(i, matrix, 0) next_match = Application.WorksheetFunction.Match(i + 1, matrix, 0) Current_x = x.cells(match_value).Value Next_x = x.cells(next_match).Value Current_y = y.cells(match_value).Value Next_y = y.cells(next_match).Value If ((Current_x - x_value) * (Next_x - x_value) <= 0#) Then Sort_Then_Interpolate = lin_2xy(Current_x, Current_y, Next_x, Next_y, x_value) LFound = True End If Next i If (LFound = False) Then Sort_Then_Interpolate = [#N/A] End Function 

在这里输入图像说明

如果这可以帮助,你可以sorting在Excel中的范围(“家庭”选项卡>“sorting和filter”…)我logging了一个简单的macros(sortingA到Z在给定的选定范围内):

 Sub Macro3() ' This is an example code, from the record tool in excel, can be much upgraded... ' Sort A to Z ' change range to your needings ' Range("A23:F27").Select ActiveWorkbook.Worksheets("Epée batarde").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Epée batarde").Sort.SortFields.Add Key:=Range( _ "A23"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Epée batarde").Sort .SetRange Range("A23:F27") .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub 

我认为你使用了太多的工作表function,但实际上我没有完全实现你的目标…在你的模块开始时显式使用Option。

函数返回值,你可能想要声明(至less更容易阅读)

 Function NameFunc (byval argument1 as Range, ...) as bolean 'or something 

LFound = True后,也许你可以添加退出。

 Array_Size = Application.WorksheetFunction.Count(x.cells) + Application.WorksheetFunction.CountBlank(x.cells) 

真的是这样写的:

 'assuming x is a range or a worksheet, and not an object like you said, its like calling a chicken a bird) 'if x is a worksheet: with x Array_Size = .cells( .rows.count,1).end(xlup).rows end with 'if x is a range : Array_Size = x.rows.count 'for counting rows Array_Size = x.cells.count 'for counting number of cells 

也许你可以发布工作表的屏幕截图来帮助。 祝你晚上愉快