优化代码VBA优秀

时间:2014-07-02 11:44:03

标签: excel vba excel-vba

我不知道是否有人能够理解这段代码。因为我知道它非常草率:/。我是VBA的新手,所以我只知道有限的功能。这是我创建的线性插补器工具。它插入大约9000行,并在不同的列中打印数据(每列对应于单独表格中的表格。)我想知道是否有一种方法可以优化此代码..那么它可以运行得更快?

Option Explicit
Dim x(1 To 9000) As Double, y(1 To 9000) As Double, z1(1 To 9000) As Double, z2(1 To 9000) As Double, V(1 To 9000) As Double, x1 As Double, y1 As Double, x2 As Double, y2 As Double, I1(1 To 9000) As Double, I2(1 To 9000) As Double, R1(1 To 9000) As Double, R2(1 To 9000) As Double, a As Double, b As Double, c As Double, d As Double, Result(1 To 9000) As Double

Dim i As Integer, j As Integer, k As Integer, p As Integer, q As Integer, r As Integer, s As Integer, t As Integer, hp As Integer
Dim ws As Sheets

Private Sub CommandButton1_Click()
Set ws = ThisWorkbook.Sheets(Array("Double Energy -FF", "EF8", "EF10", "EF12", "EF14", "EF16", "EF18"))

For hp = 2 To 7
For k = 1 To 9000

 With ws(1)
  V(k) = .Cells(k + 2, 6).Value
 End With


 With ws(hp)

 If (V(k) <= 0.5) And (V(k) <> 0) Then
  x(k) = 0.5
  x1 = .Cells(2, 1).Value
  z1(k) = .Cells(k + 2, 1).Value
 End If


For i = 1 To 6

    If (.Cells(2, i).Value <= V(k)) And (V(k) <= .Cells(2, i + 1).Value) And Not (IsEmpty(V(k))) Then
    p = i
    x1 = .Cells(2, p).Value
    x2 = .Cells(2, p + 1).Value
    x(k) = V(k)
    z1(k) = .Cells(k + 2, p).Value
    z2(k) = .Cells(k + 2, p + 1).Value

    End If
    Next i


 If V(k) = Empty Then

  Result(k) = 0

 Else

 R1(k) = (z2(k) - z1(k)) / (x2 - x1)
 R2(k) = (x(k) - x1) * R1(k)
 Result(k) = R2(k) + z1(k)

 End If
 End With

 With ws(1)
  .Cells(k + 2, hp + 6).Value = Result(k)
 End With

 Next k
 Next hp
End Sub

1 个答案:

答案 0 :(得分:0)

例如:

V(k)不依赖于 h。因此您可以通过 h 将其从循环中移除

寻找其他机会从 h 循环中删除计算。