0

我正在为客户创建一个电子表格来管理他的 ALM。我根据客户的要求在 Excel 和 VBA 下开发了它。一张“数据”计算所有 vba 函数。如果我手动计算每个单元格都可以正常工作,但如果我运行宏它没有。你有解决方案吗?如果需要,我可以发布整个文件,以便进行更好的调查。

一开始所有计算都在excel单元格中,但我为每个表创建了专用函数,因为保存时文件太大。

Public Sub Main()

    Dim i, nb_tableaux                  As Integer
    Dim j, lignemax, BarWidth           As Long
    Dim ProgressPercentage              As Double
    Dim echeancier, nomtableau          As String
    Dim ws_data                         As Worksheet
    Dim c As Range

    Me.ProgressLabel.Caption = "Initialisation terminée. "
    Set ws_data = Sheets("Data")
    lignemax = ws_data.Range("DATA").Rows.Count

    Application.ScreenUpdating = True
    Application.EnableEvents = True

    nb_tableaux = 17

    For i = 1 To nb_tableaux
        echeancier = tab_Tableaux(i, 0)
        nomtableau = tab_Tableaux(i, 1)
        Me.ProgressLabel.Caption = "En cours : " & echeancier
        ws_data.Range(nomtableau).Calculate
        'With Worksheets("Data")
            For j = 1 To lignemax
                For Each c In ws_data.Range(nomtableau).Rows(j)
                    formulaToCopy = c.Formula
                    c.ClearContents
                    c.Value = formulaToCopy
                    DoEvents
                Next
                Me.ProgressLabel.Caption = "En cours : " & echeancier & ", " & Format(j / lignemax, "0.0%") & " completed"
                Me.Repaint
            Next j
        'End With
        Me.Bar.Width = i * 200 / nb_tableaux
        Me.Bar.Caption = Format(i / nb_tableaux, "0%") & " completed"
    Next i

    Application.ScreenUpdating = False
    Application.EnableEvents = False

End Sub
4

1 回答 1

0

考虑到您为我之前的答案提供的建议后,代码效果更好,但仍然不适用于某些范围。我的问题来自对函数中参数的错误计算。实际上,我使用 ligne=activecell.row - 8 来获取要计算的范围的 ligne。但是如果我手动执行它会起作用,因为实际的单元格已被激活,但当我多次调用该函数时却不行,因为我无法激活每个单元格,这对于电子表格来说太长了。

如何使用编写函数的单元格的正确地址计算 ligne?

我希望我足够清楚。对不起我的英语不好。

公共函数 Taux_Mois(ByVal mMois 作为范围,ByVal sScenario 作为范围)

Dim ligne                           As Long

ligne = ActiveCell.row - 8

Select Case (Range("DATA[Flag]").Cells(ligne).Value = 0) Or (Range("DATA[frequence fixing]").Cells(ligne).Value = 0)
Case True
    Taux_Mois = 0
    Exit Function
Case False
    Dim index_taux  As Integer
    Dim ajust       As Long
    index_taux = CInt(Range("DATA[Indexation ID]").Cells(ligne).Value)

    If index_taux = 1 Then
        ajust = 0
    Else
        Dim ajust1, dernierfixingt0, freqfixing As Integer
        dernierfixingt0 = Range("DATA[Dernier fixing t0]").Cells(ligne).Value
        freqfixing = Range("DATA[frequence fixing]").Cells(ligne).Value
        ajust1 = (Int((mMois.Value - dernierfixingt0) / freqfixing) * freqfixing)
        ajust = Worksheets("Market Data").Range("Taux_" & sScenario.Value).Offset(12 + dernierfixingt0 + ajust1, 1 + index_taux).Value
    End If
    Taux_Mois = Range("DATA[facteur taux (TVA, base)]").Cells(ligne).Value * (ajust + Range("DATA[Spread / Taux]").Cells(ligne).Value / 10000)
    Exit Function
End Select

结束功能

于 2019-11-05T17:07:56.740 回答