仅当Visual Basic编辑器处于打开状态时,代码才运行

问题描述 投票:0回答:1

我对VBA有一个非常奇怪的问题。我试图在activeworkbook上列出循环引用,并且为此编写了以下代码。仅当我按ALT + F11时才有效。因此,如果打开了“ VBA编辑器”窗口,则代码可以正确运行,但无法正常工作。顺便说一句,代码在Addin的模块中,我从功能区调用它。您可能会看到下面的代码。非常感谢您的帮助。 Bruteforce解决方案有效。我希望有人能找到比我更好的解决方案。

Type SaveRangeCir
    Val As Variant
    Addr As String
    Preaddress As String
    Shtname As String
    Workbname As String
End Type
Public OldCir() As SaveRangeCir
Sub DonguselBasvurulariBul(control As IRibbonControl)

Dim wba As Workbook
Dim ws As Worksheet
Dim wsa As Worksheet
Dim sht As Worksheet
Dim sht2 As Worksheet
Dim dummy As Worksheet
Dim Item As Range
Dim crcell As Range
Dim cll As Range

un = "Sayin " & Environ("UserName")

muyarcirc = MsgBox("Lutfen Oncelikle Dosyanizi Kaydedin" & vbNewLine & vbNewLine & _
"-->> Dosyanizi Kaydettiniz mi?", vbExclamation + vbYesNo, un)

If muyarcirc = vbno Then
    muyar2 = MsgBox("Dongusel Basvuru Arama Islemi Iptal Edildi", vbInformation, un)
    Exit Sub
End If

'BruteForce Solution
With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .EnableEvents = False

    .VBE.MainWindow.Visible = True
    .Wait Now + TimeValue("00:00:01")
    .VBE.MainWindow.Visible = False
End With

On Error Resume Next

Set wba = ActiveWorkbook
Set wsa = wba.ActiveSheet

wba.Worksheets.Add

Set dummy = ActiveSheet

For Each sht2 In wba.Sheets
    If sht2.Name = "Dongusel Basvurular" Then
        sht2.Delete
    End If
Next sht2

wba.Worksheets.Add

Set ws = wba.ActiveSheet

dummy.Delete

With ws
    .Name = "Dongusel Basvurular"
    .Range("A1") = "Dongusel Basvuru Hucresi"
    .Range("B1") = "Dongusel Basvuru Hucresi Formul Degeri"
    .Range("C1") = "Bagli Oldugu Alan"
    .Range("D1") = "Bulundugu Sayfa"
    .Range("E1") = "Bulundugu Dosya"
End With

With wba
    For Each sht In .Worksheets
        If sht.CodeName <> ws.CodeName Then
            sht.Activate
            crcell = Nothing

        Do
            Set crcell = sht.CircularReference

            If Not crcell Is Nothing Then
                ReDim Preserve OldCir(1 To crcell.Precedents.Cells.Count)

                i = 0
                For Each cll In crcell.Precedents
                    i = i + 1
                    OldCir(i).Addr = cll.Address
                    OldCir(i).Val = cll.Formula
                    OldCir(i).Preaddress = cll.Precedents.Address
                    OldCir(i).Shtname = cll.Parent.Name
                    OldCir(i).Workbname = cll.Parent.Parent.Name
                    cll.Value = cll.Value
                Next cll
                For j = LBound(OldCir) To UBound(OldCir)
                    lr = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                    ws.Cells(lr, 1) = OldCir(j).Addr
                    ws.Cells(lr, 2) = "'" & OldCir(j).Val
                    ws.Cells(lr, 3) = OldCir(j).Preaddress
                    ws.Cells(lr, 4) = OldCir(j).Shtname
                    ws.Cells(lr, 5) = OldCir(j).Workbname
                    ws.Hyperlinks.Add Anchor:=ws.Cells(lr, 1), Address:="", SubAddress:=ws.Cells(lr, 4) & "!" & ws.Cells(lr, 1), _
                    ScreenTip:="Dongusel Basvuru Hucresini Gormek icin Tiklayiniz"
                Next j
            Else
                GoTo skipsheet
            End If

            Erase OldCir
            Set crcell = sht.CircularReference

        Loop While crcell.Cells.Count > 0

        lr2 = ws.Cells(Rows.Count, 1).End(xlUp).Row
        For m = 2 To lr2
            If ActiveSheet.Name <> ws.Cells(lr2, "D") Then
                wba.Sheets(ws.Cells(m, "D")).Activate
            End If
            Range(ws.Cells(m, 1)).Formula = "=" & Right(ws.Cells(m, 2), Len(ws.Cells(m, 2)) - 1)
        Next m
    End If
skipsheet:
Next sht

If ws.Range("A2") = "" Then
    ws.Delete
    wsa.Activate
    m1 = MsgBox("Aktif Dosyada Dongusel Basvuru Bulunamadi", vbInformation, "Sayin " & Environ("UserName"))
Else
    ws.Activate
    ws.Range("A1:E1").EntireColumn.AutoFit
End If
End With

Erase OldCir
Set crcell = Nothing

With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub
excel vba circular-reference
1个回答
0
投票

我在MS Access中使用VBA遇到了类似的问题。我了解您正在使用VBA Excel,但我想我会发布个人经验,以防它有所帮助。

我有一个带有一个打开另一个表单的按钮的Access表单。该按钮使用VBA编码。如果在单击按钮时未打开VBA编辑器,则将打开新表单,但不会触发Form_Load()。如果单击按钮时VBA编辑器已打开,则Form_Load()会按预期触发。

我没有继续出错时继续隐藏任何错误,并且我的所有变量都使用Option Explicit设置。

为了解决该问题,我在整个代码中添加了多个msgbox,以缩小发生问题的位置。将msgbox添加到按钮的Click()事件之后,我意识到以某种方式重新建立了连接,并且不再需要打开VBA编辑器的代码。意识到这一点之后,我从代码中删除了msgbox,关闭了应用程序,在没有VBA编辑器的情况下重新打开了该应用程序,然后按预期执行了VBA。

不幸的是,我在进行故障排除时遇到了一个解决方案,因此我无法提供有关为何有效的解释,但是有时只要您使它再次起作用就不需要解释!

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.