在debugging模式(步骤)或自动模式下,VBA中的行为不同

我有一个Excelmacros使表中的第一列的超链接。

当我手动模式下一步一步地运行它,它工作正常,超链接创build。

当我运行它在自动模式下,它运行没有任何错误,但超链接不创build。

我不知道为什么…?

Public Sub aktualisieren() Call Column_MakeHyperlinks(1) End Sub 

接着

  Sub Column_MakeHyperlinks(Blatt) ' ### Start Tabellenanalyse ### ' - Länge ' - Start ' - Ende On Error Resume Next Set WS = Sheets(Blatt) ' Set Tabelle = WS.ListObjects(1) ' Get Table Anzahl = Tabelle.ListRows.Count ' Number of Rows in Table Start = Tabelle.ListRows(1).Range.Row ' First Row in Table Ende = Tabelle.ListRows(Tabelle.ListRows.Count).Range.Row ' Last Row in Table sMsg = "There are " & Anzahl & " Rows in '" & Tabelle.Name & "'. " sMsg = sMsg & DNL & "Start in Row " & Start sMsg = sMsg & NL & ", Ende in Row " & Ende 'MsgBox sMsg, vbInformation, UCase(sTableName) ' Activate for Text-Output ' ### Ende Tabellenanalyse ### iRow = Start ' Start at first Row with content iCol = 1 ' Column A ' Parameters, which should be concatenated to the link are in column A WS.Hyperlinks.Delete Do While WS.Cells(iRow, iCol).Value <> "" ' create Hyperlink (fixed prefix and dynamic parameter Temp = WS.Cells(iRow, iCol).Value 'WS.Cells(iRow, iCol).Select ' Not necessary, only to make active cell visible in manual mode WS.Hyperlinks.Add Anchor:=WS.Cells(iRow, iCol), Address:="https://www.google.de/?gws_rd=ssl#q=" & ActiveSheet.Cells(iRow, iCol).Value, _ TextToDisplay:=Temp, _ ScreenTip:="https://www.google.de/?gws_rd=ssl#q=" & ActiveSheet.Cells(iRow, iCol).Value 'move to the next row iRow = iRow + 1 Loop End Sub 

谁能解释我为什么? 提前致谢!

对于我来说,你的代码工作,超链接被创build,但似乎是“不活跃”。 我补充说:

  WS.Cells(iRow, iCol).Value = WS.Cells(iRow, iCol).Value 

之前的iRow增量解决了超链接“活动”的问题。
另外你在listobject列的单元格中迭代的方式应该是这样的:

 For Each c In Tabelle.ListColumns("column_name").DataBodyRange Temp = c.Value WS.Hyperlinks.Add Anchor:=c, Address:="https://www.google.de/?gws_rd=ssl#q=" & c.Value, _ TextToDisplay:=Temp, _ ScreenTip:="https://www.google.de/?gws_rd=ssl#q=" & c.Value c.Value = c.Value Next 

我发现我的问题:我从数据库的第一列中的数据。 当我进行刷新时,它将在后台运行,并且在数据仍然导入时创build链接。

我在“连接”菜单中find了一个checkbox“允许背景实现”,并取消选中它。 这和用户avb的答案解决了我的问题。

谢谢 !!