我对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
答案 0 :(得分:0)
我在MS Access中使用VBA遇到了类似的问题。我了解您正在使用VBA Excel,但我想我会发布个人经验,以防它有所帮助。
我有一个Access表单,带有一个可以打开另一个表单的按钮。该按钮使用VBA编码。如果在单击按钮时未打开VBA编辑器,则将打开新表单,但不会触发Form_Load()。如果单击按钮时VBA编辑器已打开,则Form_Load()会按预期触发。
我没有On Error Resume Next隐藏任何错误,并且我的所有变量都使用Option Explicit设置。
要解决此问题,我在整个代码中添加了多个msgbox,以缩小发生问题的位置。将msgbox添加到按钮的Click()事件之后,我意识到以某种方式重新建立了连接,并且不再需要打开VBA编辑器的代码。意识到这一点之后,我从代码中删除了msgbox,关闭了应用程序,在没有VBA编辑器的情况下重新打开了该应用程序,然后按预期执行了VBA。
不幸的是,我在进行故障排除时遇到了一个解决方案,因此我无法提供有关为何有效的解释,但是有时只要您使它再次起作用就不需要解释!
希望这会有所帮助。
答案 1 :(得分:0)
我遇到了同样的问题
但是,它的出现是由于首先使用了没有返回变量的消息框,然后将其扩展为包含返回,即ret = msgbox(
删除了消息框,代码工作,关闭并保存了项目,重新打开并添加了返回变量的消息框,现在即使没有打开编辑器也可以运行。