gpt4 book ai didi

vba - Excel 2013 VBA 按排名排序

转载 作者:行者123 更新时间:2023-12-04 21:05:03 25 4
gpt4 key购买 nike

所以这里是问题:

我需要插入输入到模板的值。听起来很容易,对吧?问题是,这些值并不总是按数字顺序排列,所以它弄乱了我的插值函数。我的插值函数输入 x 值(在本例中为 HP,这是需要以数字方式排序的参数,而不会弄乱与之相关的数据)、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

enter image description here

最佳答案

如果这有帮助,您可以在 excel 中对范围进行排序(“主页”选项卡 >“排序和过滤器”> ...)
我录制了一个简单的宏(在给定的选定范围内对 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 显式。

函数返回值,您可能想要声明(至少对我们来说更容易阅读)
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

也许您可以发布工作表的屏幕截图以提供帮助。
晚上好

关于vba - Excel 2013 VBA 按排名排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24270863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com