简体   繁体   English

从VBA界面运行宏时,Excel崩溃,但使用按钮时可以工作

[英]Excel crashes when macro is run from VBA interface, but works when using a button

I have a macro that takes different ranges, performs calculations with values in them, and then copies the value to a cell to be available for use to formulas in the worksheet. 我有一个采用不同范围的宏,使用其中的值执行计算,然后将值复制到单元格中以供工作表中的公式使用。 I want this macro to run when values in certain ranges are modified. 我希望在修改某些范围内的值时运行此宏。 I use this same code in different worksheets, where I initially placed an ActiveX button. 我在不同的工作表中使用了相同的代码,最初在其中放置了ActiveX按钮。 The problem is that when I put the macro in a worksheet_calculate or Worksheet_SelectionChange Excel crashes alltogether. 问题是,当我将宏放入worksheet_calculate或Worksheet_SelectionChange时,Excel一起崩溃。 At first I assumed I was causing infinite iterations, but after using EventEnable = FALSE and EnableCalculations = FALSE to avoid that behavior Excel still crashed. 起初我以为我会引起无限次迭代,但是在使用EventEnable = FALSE和EnableCalculations = FALSE来避免该行为后,Excel仍然崩溃了。 I erased the macros in SelectionChange and Calculate, and I further inspected the issue and found that when the macro is run from the VBA interface Excel also crashes, but it works with the ActiveX button in the worksheets that makes reference to the macro. 我擦除了SelectionChange和Calculate中的宏,并进一步检查了该问题,发现从VBA接口运行宏时,Excel也崩溃了,但它与工作表中引用该宏的ActiveX按钮一起使用。 I just can't seem to find what is wrong with the code, and why it runs perfectly when accessed through the button but crashes when run from the VBA interface. 我似乎似乎找不到代码的问题,以及为什么在通过按钮访问时代码可以完美运行,而从VBA界面运行时却崩溃。

Here is the code, please help me find what is wrong, or what I can I try so that it runs from VBA because calling it or running it from the interface will crash Excel as of now. 这是代码,请帮助我找出问题所在,或者我可以尝试使其从VBA运行,因为从现在开始调用或从界面运行它将使Excel崩溃。

Sub performCalculations()
Dim revenue As Double
Dim expenses As Double
Dim i As Integer
Dim j As Integer
Dim count As Integer
Dim aConst As Double
Dim tempConst As Long
Dim bla As Range
Dim curve As Range
Dim price As Range
Dim finalOut As Range
Dim a As Double
Dim tempString As String


Set bla = ActiveWorkbook.ActiveSheet.Range("D16:D75")
Set curve = ActiveWorkbook.ActiveSheet.Range("G2:WH11")
Set price = ActiveWorkbook.ActiveSheet.Range("G162:WH164")
Set finalOut = ActiveWorkbook.ActiveSheet.Range("G83:WH141")
tempString = ActiveWorkbook.ActiveSheet.Name
tempString = Left(tempString, InStr(1, tempString, " ", vbTextCompare) - 1)
a = ActiveWorkbook.Sheets(tempString).Cells(10, 8).Value
aConst = a / 1000


For i = 1 To bla.Rows.count
    count = 1
    For j = 1 To curve.Columns.count
        If j < i Then
            finalOut.Cells(i, j) = 0
        Else
            If bla.Cells(i, 1) = 0 Then
                finalOut.Cells(i, j) = 0
            Else
                tempConst = curve.Cells(2, count) * aConst * price.Cells(2, j)
                revenue = bla.Cells(i, 1) * (curve.Cells(1, count) * price.Cells(1, j) + curve.Cells(3, count) * price.Cells(3, j))
                expenses = bla.Cells(i, 1) * curve.Cells(4, count) + bla.Cells(i, 1) * curve.Cells(5, count) + bla.Cells(i, 1) * _
                    curve.Cells(6, count) + bla.Cells(i, 1) * curve.Cells(7, count) + bla.Cells(i, 1) * curve.Cells(10, count)
                expenses = expenses + revenue * curve.Cells(9, count) + tempConst * curve.Cells(8, count)
                revenue = revenue + tempConst
                finalOut(i, j) = revenue - expenses
                count = count + 1
            End If
            If i > 1 Then
                finalOut.Cells(i, j) = finalOut.Cells(i, j) + finalOut.Cells(i - 1, j)
            End If
        End If
    Next j
Next i


End Sub

I am running Excel 2013, and Windows 7 Professional 我正在运行Excel 2013和Windows 7 Professional

I am not sure if this is the final answer for your problem but one thing could be affecting you macro, the use of Active Objects such as ActiveWorkbook, ActiveSheet and so on. 我不确定这是否是您问题的最终答案,但可能会影响到您的宏,使用ActiveObject(例如ActiveWorkbook,ActiveSheet等)的一件事。

My advice for you is to reference the objects you have to use so that if your excel file is not selected or if you select the wrong worksheet, the code still works. 我的建议是引用必须使用的对象,这样,如果未选择excel文件或选择了错误的工作表,该代码仍然有效。 To do that, please check the example below: 为此,请检查以下示例:

Dim workingSheet as Worksheet
Dim auxWorksheet as Worksheet

Set workingSheet = ThisWorkbook.Worksheets("The name of the worksheet here")
Set auxWorksheet = ThisWorkbook.Worksheets(tempString)

Set bla = workingSheet.Range("D16:D75")
Set curve = workingSheet.Range("G2:WH11")
Set price = workingSheet.Range("G162:WH164")
Set finalOut = workingSheet.Range("G83:WH141")
tempString = workingSheet.Name
a = auxWorksheet.Cells(10, 8).Value

Please give it a try and give some feedback so that if it does not work, I can keep trying to help you. 请尝试一下并提供一些反馈,以便如果它无法正常工作,我将继续努力为您提供帮助。

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

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