所以这就是问题所在:
我需要插入输入到模板的值。听起来很简单吧?问题是,这些值并不总是按数字顺序排列,因此它会影响我的插值函数。我的插值函数有一个x值的输入(在这种情况下是惠普,这是需要在数字上排序而不会弄乱与之相关的数据的参数),y值(你要在?HP找到的参数)和x_value您要在其中找到y参数。一些y参数是方程式,所以我想不出一种方法来完全重新排序列而不会弄乱数据。我认为应该有一种方法来引用集合中HP的等级,选择相关的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
答案 0 :(得分:0)
如果可以提供帮助,您可以在Excel中对范围进行排序(&#39; Home&#39; Tab&gt;&#39; Sort&amp; Filter&gt; ...) 我录制了一个简单的宏(在给定的选定范围内排序A到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
我认为你使用了太多的工作表功能,但实际上我并没有完全实现你的目标...... 在模块的开头使用Option explicit。
函数返回值,您可能想要声明(更容易阅读,至少对我们来说)
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
也许您可以发布工作表的屏幕截图来提供帮助。 祝你晚安愉快