excel 使用索引匹配函数时,VBA代码无法识别表中的某些数字

egmofgnx  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(186)

我有一个值表,这些值是在VBA代码后面的长计算的因子。为了提取正确的值,我在代码中使用了索引匹配函数。
然而,我不得不稍微修改一下代码,以便能够在值之间进行插值。
除非我的RoundUp值是0.3或0.8,负的和正的,否则代码工作正常。
当psi_up = 0.3时,即使在表中也会出现错误1004。
当psi_down = 0.3时,没有问题,索引匹配函数也不会导致问题。
这是我使用过的代码,也是我需要从中提取值的表:
尺寸alpha_0为双倍尺寸psi为双倍
psi_down尺寸为双倍尺寸psi_up尺寸为双倍
将alpha_down调整为双倍大小将alpha_up调整为双倍大小
变暗xp01为双倍变暗xp02为双倍变暗xp1为双倍变暗xp03为双倍变暗xp04为双倍变暗xp2为双倍
尺寸C1_0为双倍

If MyA = 0 And MyB = 0 Then
    psi = 0
    Else
    psi = MyA / MyB
    End If
               

    If My0 >= Abs(MyB) Then
    alpha_0 = MyB / My0
    Else
    alpha_0 = My0 / MyB
    End If
       

    'Parameter to Interpolate 
    
    alpha_down = WorksheetFunction.RoundDown(alpha_0, 1)
    psi_down = WorksheetFunction.RoundDown(psi, 1)
                                
    alpha_up = WorksheetFunction.RoundUp(alpha_0, 1)
    psi_up = WorksheetFunction.RoundUp(psi, 1)
    
                            
    If psi_up = 1 Then
    psi_down = 0.9
    End If

    If psi = 1 Then
    psi_down = 0.9
    End If
                            
    If psi_down = psi_up Then
    psi_up = psi_down + 0.1
    End If
                            
    If alpha_0 = -1 Then
    alpha_down = -0.9
    End If

    If alpha_down = alpha_up Then
    alpha_up = alpha_down - 0.1
    End If
    
    If alpha_0 = -0 Then
    alpha_0 = 0
    End If
                                            
                                            
' My0 = 0


    If My0 = 0 Then
                                            
    xp01 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I61:T71"), _
    Application.WorksheetFunction.Match(alpha_0, Sheets("C1,0 & C2,0").Range("I61:I71"), 0), _
    Application.WorksheetFunction.Match(psi_down, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                
    xp02 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I61:T71"), _
    Application.WorksheetFunction.Match(alpha_0, Sheets("C1,0 & C2,0").Range("I61:I71"), 0), _
    Application.WorksheetFunction.Match(psi_up, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                            
                                
    C1_0 = xp01 + (xp02 - xp01) * ((psi - psi_down) / (psi_up - psi_down))
    
    
    
                                                                                     

'If My0 >= abs(MyB)
                                            
    ElseIf My0 >= Abs(MyB) Then
                                                                 
                                
    xp01 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I50:T61"), _
    Application.WorksheetFunction.Match(alpha_down, Sheets("C1,0 & C2,0").Range("I50:I61"), 0), _
    Application.WorksheetFunction.Match(psi_down, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                               
    xp02 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I50:T61"), _
    Application.WorksheetFunction.Match(alpha_down, Sheets("C1,0 & C2,0").Range("I50:I61"), 0), _
    Application.WorksheetFunction.Match(psi_up, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
    
                                            
                                
    xp1 = xp01 + (xp02 - xp01) * ((psi - psi_down) / (psi_up - psi_down))
                                            
                                            
    xp03 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I50:T61"), _
    Application.WorksheetFunction.Match(alpha_up, Sheets("C1,0 & C2,0").Range("I50:I61"), 0), _
    Application.WorksheetFunction.Match(psi_down, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                            
                               
    xp04 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I50:T61"), _
    Application.WorksheetFunction.Match(alpha_up, Sheets("C1,0 & C2,0").Range("I50:I61"), 0), _
    Application.WorksheetFunction.Match(psi_up, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                            
                                            
    xp2 = xp03 + (xp04 - xp03) * ((psi - psi_down) / (psi_up - psi_down))
                                            
                                            
                                            
    C1_0 = xp1 + (xp2 - xp1) * ((Abs(alpha_0) - Abs(alpha_down)) / ((Abs(alpha_up) - Abs(alpha_down))))
                                
                                
                                
                                            
' abs(MyB) > My0
                                                                                  
                                            
    ElseIf My0 < Abs(MyB) Then

                                            
    xp01 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I61:T71"), _
    Application.WorksheetFunction.Match(alpha_down, Sheets("C1,0 & C2,0").Range("I61:I71"), 0), _
    Application.WorksheetFunction.Match(psi_down, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                
    xp02 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I61:T71"), _
    Application.WorksheetFunction.Match(alpha_down, Sheets("C1,0 & C2,0").Range("I61:I71"), 0), _
    Application.WorksheetFunction.Match(psi_up, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                            
                                
    xp1 = xp01 + (xp02 - xp01) * ((psi - psi_down) / (psi_up - psi_down))
                                            
                                          
    xp03 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I61:T71"), _
    Application.WorksheetFunction.Match(alpha_up, Sheets("C1,0 & C2,0").Range("I61:I71"), 0), _
    Application.WorksheetFunction.Match(psi_down, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                            
    xp04 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I61:T71"), _
    Application.WorksheetFunction.Match(alpha_up, Sheets("C1,0 & C2,0").Range("I61:I71"), 0), _
    Application.WorksheetFunction.Match(psi_up, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))
                                            
                                            
    xp2 = xp03 + (xp04 - xp03) * ((psi - psi_down) / (psi_up - psi_down))
                                            
                                            
    C1_0 = xp1 + (xp2 - xp1) * ((Abs(alpha_0) - Abs(alpha_down)) / ((Abs(alpha_up) - Abs(alpha_down))))
                        
    Else
               

End If

enter image description hereenter image description here
我正在失去头发,试图解决这个问题,所以这是我第一次尝试在网上寻求帮助。
非常感谢你读到这篇文章,也许能帮我一把!!!

0md85ypi

0md85ypi1#

看起来您的范围对于行的搜索是不正确的,假设您的右上角单元格在I50中,整个Array表(包括轴)从I50:T71开始,您的行轴从I50:I71开始,您的列轴从I50:T50开始。
所以你的index index函数应该是这样的:

xp01 = _
    WorksheetFunction.Index(Sheets("C1,0 & C2,0").Range("I50:T71"), _
    Application.WorksheetFunction.Match(alpha_down, Sheets("C1,0 & C2,0").Range("I50:I71"), 0), _
    Application.WorksheetFunction.Match(psi_down, Sheets("C1,0 & C2,0").Range("I50:T50"), 0))

相关问题