简体   繁体   English

单元格引用在Excel VBA中不起作用

[英]Cell referencing not working in excel vba

I wrote a function which takes inputs from a sheet, performs calculations, and enters the results on the same sheet. 我编写了一个函数,该函数从工作表获取输入,执行计算,然后在同一工作表上输入结果。 I did this to test the functionality of the code. 我这样做是为了测试代码的功能。

Now I need to have the code to have the same functionality, but to take input from 2 different sheets, and write to one of them. 现在,我需要使代码具有相同的功能,但要从2个不同的工作表中获取输入并写入其中之一。

Lets call the sheets "Data" and "Interface" 让我们将工作表称为“数据”和“接口”

I wrote the code initially using the Range object to reference cells and ranges, with no precursor. 我最初使用Range对象编写代码来引用单元格和范围,没有任何前体。 I am currently trying to write the code as a macro assigned to a button on one of the sheets. 我目前正在尝试将代码编写为分配给工作表之一上的按钮的macro

Things I've tried: 我尝试过的事情:

Activating the appropriate sheet and using ranges normally eg 激活适当的纸张并正常使用范围,例如

Worksheets("Data").Activate
pressure = Range("S2").Value

This causes odd errors, such as: 这会导致奇怪的错误,例如:

unable to get match property of the worksheetfunction class 无法获取worksheetfunction类的match属性

Fully qualifying all Range objects eg 完全限定所有Range对象,例如

ThisWorkbook.Sheets("Data").Range("A1")

Same error as above. 与上述相同的错误。

Referencing certain cells using named ranges: 使用命名范围引用某些单元格:

pressure = Range("Pressure").Value

From what I've read, this should work. 根据我的阅读,这应该有效。 I have no idea why it doesn't. 我不知道为什么没有。

Is there a good, or preferred, way to do this? 有没有好的或首选的方式来做到这一点?

Full code (2 functions, 1 sub): 完整代码(2个功能,1个子):

Function InterpolateAll(methane As Double, pressure As Double, temp As Double, column As Integer, m_range As Range, p_range As Range, t_range As Range, m1 As Double, m2 As Double, t1 As Double, t2 As Double, t3 As Double, t4 As Double, p1 As Double, p_1_index As Integer, p2 As Double, p_2_index As Integer, p3 As Double, p_3_index As Integer, p4 As Double, p_4_index As Integer, p5 As Double, p_5_index As Integer, p6 As Double, p_6_index As Integer, p7 As Double, p_7_index As Integer, p8 As Double, p_8_index As Integer) As Double

Worksheets("Data").Activate

Dim v1 As Double
Dim v2 As Double
Dim v3 As Double
Dim v4 As Double
Dim v5 As Double
Dim v6 As Double
Dim v7 As Double
Dim v8 As Double
Dim vr1 As Double
Dim vr2 As Double
Dim vr3 As Double
Dim vr4 As Double
Dim vrr1 As Double
Dim vrr2 As Double

v1 = cells(p_1_index, column)
v2 = cells(p_2_index, column)
v3 = cells(p_3_index, column)
v4 = cells(p_4_index, column)
v5 = cells(p_5_index, column)
v6 = cells(p_6_index, column)
v7 = cells(p_7_index, column)
v8 = cells(p_8_index, column)
vr1 = Interpolate(pressure, p1, p2, v1, v2)
vr2 = Interpolate(pressure, p3, p4, v3, v4)
vr3 = Interpolate(pressure, p5, p6, v5, v6)
vr4 = Interpolate(pressure, p7, p8, v7, v8)
vrr1 = Interpolate(temp, t1, t2, vr1, vr2)
vrr2 = Interpolate(temp, t3, t4, vr3, vr4)
vrrr = Interpolate(methane, m1, m2, vrr1, vrr2)

InterpolateAll = vrrr

End Function

Function Interpolate(a_desired As Double, a1 As Double, a2 As Double, p1 As Double, p2 As Double)

Interpolate = (a_desired - a1) / (a2 - a1) * (p2 - p1) + p1

End Function





Private Sub CommandButton3_Click()

Dim m1 As Double
Dim m2 As Double
Dim t1 As Double
Dim t2 As Double
Dim t3 As Double
Dim t4 As Double
Dim p1 As Double
Dim p2 As Double
Dim p3 As Double
Dim p4 As Double
Dim p5 As Double
Dim p6 As Double
Dim p7 As Double
Dim p8 As Double

Worksheets("Data").Activate

pressure = Range("S2").Value
temp = Range("R2").Value
percent_methane = Range("Q2").Value

start_range = Range("A1")


Dim m_range As Range
Dim p_range As Range
Dim t_range As Range
Dim m_start As Range
Dim p_start As Range
Dim t_start As Range


Set m_range = Range("A:A")
Set t_range = Range("B:B")
Set p_range = Range("C:C")
Set m_start = Range("A1")
Set t_start = Range("B1")
Set p_start = Range("C1")

m1 = m_range.Item(WorksheetFunction.Match(percent_methane, m_range))
m_range_1_top = WorksheetFunction.Match(percent_methane, m_range)
m_range_1_bottom = WorksheetFunction.Match(m1, m_range, 0)


m2 = m_range.Item(m_range_1_top + 1)
m_range_2_bottom = m_range_1_top + 1
m_range_2_top = WorksheetFunction.Match(m2, m_range)



t1 = t_range.Item(WorksheetFunction.Match(temp, Range(cells(m_range_1_bottom, 2), cells(m_range_1_top, 2))) + m_range_1_bottom - 1)

t_range_1_top = WorksheetFunction.Match(temp, Range(cells(m_range_1_bottom, 2), cells(m_range_1_top, 2))) + m_range_1_bottom - 1
t_range_1_bottom = WorksheetFunction.Match(t_range.Item(t_range_1_top), Range(cells(m_range_1_bottom, 2), cells(m_range_1_top, 2)), 0) + m_range_1_bottom - 1

t2 = t_range.Item(t_range_1_top + 1)

t_range_2_bottom = t_range_1_top + 1
t_range_2_top = WorksheetFunction.Match(t2, Range(cells(m_range_1_bottom, 2), cells(m_range_1_top, 2))) + m_range_1_bottom - 1

t_range_3_top = WorksheetFunction.Match(temp, Range(cells(m_range_2_bottom, 2), cells(m_range_2_top, 2))) + m_range_2_bottom - 1
t_range_3_bottom = WorksheetFunction.Match(t_range.Item(t_range_3_top), Range(cells(m_range_2_bottom, 2), cells(m_range_2_top, 2)), 0) + m_range_2_bottom - 1
t3 = t_range.Item(t_range_3_bottom)

t4 = t_range.Item(t_range_3_top + 1)

t_range_4_bottom = t_range_3_top + 1
t_range_4_top = WorksheetFunction.Match(t4, Range(cells(m_range_2_bottom, 2), cells(m_range_2_top, 2))) + m_range_2_bottom - 1

p_1_index = WorksheetFunction.Match(pressure, Range(cells(t_range_1_bottom, 3), cells(t_range_1_top, 3))) + t_range_1_bottom - 1
p1 = p_range.Item(p_1_index)

p_2_index = p_1_index + 1
p2 = p_range.Item(p_2_index)

p_3_index = WorksheetFunction.Match(pressure, Range(cells(t_range_2_bottom, 3), cells(t_range_2_top, 3))) + t_range_2_bottom - 1
p3 = p_range.Item(p_3_index)

p_4_index = p_3_index + 1
p4 = p_range.Item(p_4_index)

p_5_index = WorksheetFunction.Match(pressure, Range(cells(t_range_3_bottom, 3), cells(t_range_3_top, 3))) + t_range_3_bottom - 1
p5 = p_range.Item(p_5_index)

p_6_index = p_5_index + 1
p6 = p_range.Item(p_6_index)

p_7_index = WorksheetFunction.Match(pressure, Range(cells(t_range_4_bottom, 3), cells(t_range_4_top, 3))) + t_range_4_bottom - 1
p7 = p_range.Item(p_7_index)

p_8_index = p_7_index + 1
p8 = p_range.Item(p_8_index)

var1 = InterpolateAll(CDbl(percent_methane), CDbl(pressure), CDbl(temp), 4, m_range, p_range, t_range, CDbl(m1), CDbl(m2), CDbl(t1), CDbl(t2), CDbl(t3), CDbl(t4), CDbl(p1), CInt(p_1_index), p2, CInt(p_2_index), p3, CInt(p_3_index), p4, CInt(p_4_index), p5, CInt(p_5_index), p6, CInt(p_6_index), p7, CInt(p_7_index), p8, CInt(p_8_index))
Range("P4") = var1

End Sub

Skip activating your sheets, and declare the values by using a full address WITH the worksheet. 跳过激活工作表的步骤,并在工作表中使用完整地址声明值。

With ThisWorkbook.Sheets("Data")

    v1 = .Cells(p_1_index, column)
    v2 = .Cells(p_2_index, column)
    v3 = .Cells(p_3_index, column)
    v4 = .Cells(p_4_index, column)
    v5 = .Cells(p_5_index, column)
    v6 = .Cells(p_6_index, column)
    v7 = .Cells(p_7_index, column)
    v8 = .Cells(p_8_index, column)

    Set m_range = .Range("A:A")
    Set t_range = .Range("B:B")
    Set p_range = .Range("C:C")
    Set m_start = .Range("A1")
    Set t_start = .Range("B1")
    Set p_start = .Range("C1")

End With

Run the debugger and put a breakpoint where the WorksheetFunctions start to occur. 运行调试器,并在工作表函数开始出现的地方放置一个断点。 Then step through them, watching the LOCALS window to see the changes. 然后逐步浏览它们,查看“本地”窗口以查看更改。 You will see what is wrong if there is still a problem. 如果仍然有问题,您将看到哪里出了问题。

A common mistake is neglecting to subordinate all of the appropriate .Range and .Cells references within a With ... End With block. 一个常见的错误是忽略了With ... End With块中所有适当的.Range.Cells引用的下属。 Since you are using .Cells references to construct your .Range references, this becomes very important. 由于您使用的是.Cells引用来构造.Range引用,因此这非常重要。 Please review this rewrite; 请查看此重写; I've added some comments but the majority was simple referencing and reorganization. 我添加了一些评论,但大多数只是简单的引用和重组。

'Never a good idea to use a reserved word like 'column' as a variable. I changed to 'colm'
Function InterpolateAll(methane As Double, pressure As Double, temp As Double, _
  colm As Long, m_range As Range, p_range As Range, t_range As Range, m1 As Double, _
  m2 As Double, t1 As Double, t2 As Double, t3 As Double, t4 As Double, p1 As Double, _
  p_1_index As Long, p2 As Double, p_2_index As Long, p3 As Double, p_3_index As Long, _
  p4 As Double, p_4_index As Long, p5 As Double, p_5_index As Long, p6 As Double, _
  p_6_index As Long, p7 As Double, p_7_index As Long, p8 As Double, p_8_index As Long) As Double

    Dim v1 As Double, v2 As Double, v3 As Double, v4 As Double
    Dim v5 As Double, v6 As Double, v7 As Double, v8 As Double
    Dim vr1 As Double, vr2 As Double, vr3 As Double, vr4 As Double
    Dim vrr1 As Double, vrr2 As Double, vrrr As Double

    With Worksheets("Data")
        v1 = .Cells(p_1_index, colm)
        v2 = .Cells(p_2_index, colm)
        v3 = .Cells(p_3_index, colm)
        v4 = .Cells(p_4_index, colm)
        v5 = .Cells(p_5_index, colm)
        v6 = .Cells(p_6_index, colm)
        v7 = .Cells(p_7_index, colm)
        v8 = .Cells(p_8_index, colm)
    End With

    vr1 = Interpolate(pressure, p1, p2, v1, v2)
    vr2 = Interpolate(pressure, p3, p4, v3, v4)
    vr3 = Interpolate(pressure, p5, p6, v5, v6)
    vr4 = Interpolate(pressure, p7, p8, v7, v8)
    vrr1 = Interpolate(temp, t1, t2, vr1, vr2)
    vrr2 = Interpolate(temp, t3, t4, vr3, vr4)
    vrrr = Interpolate(methane, m1, m2, vrr1, vrr2)

    InterpolateAll = vrrr

End Function

Function Interpolate(a_desired As Double, a1 As Double, a2 As Double, p1 As Double, p2 As Double)

    Interpolate = (a_desired - a1) / (a2 - a1) * (p2 - p1) + p1

End Function

Private Sub ComButt()

    Dim m1 As Double, m2 As Double
    Dim t1 As Double, t2 As Double, t3 As Double, t4 As Double
    Dim p1 As Double, p2 As Double, p3 As Double, p4 As Double
    Dim p5 As Double, p6 As Double, p7 As Double, p8 As Double
    Dim m_range As Range, p_range As Range, t_range As Range
    Dim m_start As Range, p_start As Range, t_start As Range
    Dim m_range_1_top As Long, m_range_1_bottom As Long, m_range_2_top As Long, m_range_2_bottom As Long
    Dim t_range_1_top As Long, t_range_1_bottom As Long, t_range_2_top As Long, t_range_2_bottom As Long
    Dim t_range_3_top As Long, t_range_3_bottom As Long, t_range_4_top As Long, t_range_4_bottom As Long
    Dim p_1_index As Long, p_2_index As Long, p_3_index As Long, p_4_index As Long
    Dim p_5_index As Long, p_6_index As Long, p_7_index As Long, p_8_index As Long
    Dim pressure As Double, temp As Double, percent_methane As Double
    Dim var1 As Double

    With Worksheets("Data")
        pressure = .Range("S2").Value
        temp = .Range("R2").Value
        percent_methane = .Range("Q2").Value

        'this sets start_range as a value, not the A1 cell as a range. Maybe set start_range = .Range("A1")
        'in any event, it doesn't appear to be used after this so commented out
        'start_range = .Range("A1")
        'set start_range = .Range("A1")

        Set m_range = .Range("A:A")
        Set t_range = .Range("B:B")
        Set p_range = .Range("C:C")
        Set m_start = .Range("A1")
        Set t_start = .Range("B1")
        Set p_start = .Range("C1")

        m1 = m_range.Item(WorksheetFunction.Match(percent_methane, m_range))
        m_range_1_top = WorksheetFunction.Match(percent_methane, m_range)
        m_range_1_bottom = WorksheetFunction.Match(m1, m_range, 0)

        m2 = m_range.Item(m_range_1_top + 1)
        ' again, are _top and _bottom transposed here?
        m_range_2_top = WorksheetFunction.Match(m2, m_range)
        m_range_2_bottom = m_range_1_top + 1

        'ALL of the range and cell references need to be referenced to the With ... End With
        t1 = t_range.Item(WorksheetFunction.Match(temp, .Range(.Cells(m_range_1_bottom, 2), .Cells(m_range_1_top, 2))) + m_range_1_bottom - 1)
        t_range_1_top = WorksheetFunction.Match(temp, .Range(.Cells(m_range_1_bottom, 2), .Cells(m_range_1_top, 2))) + m_range_1_bottom - 1
        t_range_1_bottom = WorksheetFunction.Match(t_range.Item(t_range_1_top), .Range(.Cells(m_range_1_bottom, 2), .Cells(m_range_1_top, 2)), 0) + m_range_1_bottom - 1

        t2 = t_range.Item(t_range_1_top + 1)
        t_range_2_bottom = t_range_1_top + 1
        t_range_2_top = WorksheetFunction.Match(t2, .Range(.Cells(m_range_1_bottom, 2), .Cells(m_range_1_top, 2))) + m_range_1_bottom - 1

        t_range_3_top = WorksheetFunction.Match(temp, .Range(.Cells(m_range_2_bottom, 2), .Cells(m_range_2_top, 2))) + m_range_2_bottom - 1
        t_range_3_bottom = WorksheetFunction.Match(t_range.Item(t_range_3_top), .Range(.Cells(m_range_2_bottom, 2), .Cells(m_range_2_top, 2)), 0) + m_range_2_bottom - 1
        t3 = t_range.Item(t_range_3_bottom)

        t4 = t_range.Item(t_range_3_top + 1)
        t_range_4_bottom = t_range_3_top + 1
        t_range_4_top = WorksheetFunction.Match(t4, .Range(.Cells(m_range_2_bottom, 2), .Cells(m_range_2_top, 2))) + m_range_2_bottom - 1

        p_1_index = WorksheetFunction.Match(pressure, .Range(.Cells(t_range_1_bottom, 3), .Cells(t_range_1_top, 3))) + t_range_1_bottom - 1
        p1 = p_range.Item(p_1_index)

        p_2_index = p_1_index + 1
        p2 = p_range.Item(p_2_index)

        p_3_index = WorksheetFunction.Match(pressure, .Range(.Cells(t_range_2_bottom, 3), .Cells(t_range_2_top, 3))) + t_range_2_bottom - 1
        p3 = p_range.Item(p_3_index)

        p_4_index = p_3_index + 1
        p4 = p_range.Item(p_4_index)

        p_5_index = WorksheetFunction.Match(pressure, .Range(.Cells(t_range_3_bottom, 3), .Cells(t_range_3_top, 3))) + t_range_3_bottom - 1
        p5 = p_range.Item(p_5_index)

        p_6_index = p_5_index + 1
        p6 = p_range.Item(p_6_index)

        p_7_index = WorksheetFunction.Match(pressure, .Range(.Cells(t_range_4_bottom, 3), .Cells(t_range_4_top, 3))) + t_range_4_bottom - 1
        p7 = p_range.Item(p_7_index)

        p_8_index = p_7_index + 1
        p8 = p_range.Item(p_8_index)

        var1 = InterpolateAll(CDbl(percent_methane), CDbl(pressure), CDbl(temp), 4, m_range, p_range, t_range, CDbl(m1), CDbl(m2), CDbl(t1), CDbl(t2), CDbl(t3), CDbl(t4), CDbl(p1), CInt(p_1_index), p2, CInt(p_2_index), p3, CInt(p_3_index), p4, CInt(p_4_index), p5, CInt(p_5_index), p6, CInt(p_6_index), p7, CInt(p_7_index), p8, CInt(p_8_index))
        .Range("P4") = var1
    End With

    Set m_range = Nothing
    Set t_range = Nothing
    Set p_range = Nothing
    Set m_start = Nothing
    Set t_start = Nothing
    Set p_start = Nothing
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM