迭代器未在循环顶部重置

huangapple go评论68阅读模式
英文:

Iterator not being reset at top of loop

问题

以下是翻译好的部分:

"对于一些背景信息,这是指的是2016年版本的Excel。这是模块中唯一的子程序。

嗨,我一直遇到一个问题,即运行VBA脚本曾经能够正常工作(对于相同版本),但现在在运行时导致Excel崩溃。有趣的是,相同的宏函数对其他用户的相同工作簿有效,但对我却无效。经过一些调试,我确定了导致崩溃的是哪一行,并解决了问题,但我仍然想知道为什么问题首次发生。

以下是代码部分的翻译:

"当n = 2时,代码在.Format行崩溃。在调试器中,发生这种情况时s的值为12,尽管.ChartObjects("Chart " & n).Chart.SeriesCollection.Count = 11。可以推测,崩溃发生是因为Excel试图格式化不存在的系列线。

在for语句之前添加s = 1解决了问题,但我不知道为什么s没有在For循环的顶部被重置为1。

有人能解释为什么会发生这种情况以及我如何在将来防止这种情况发生吗?"

英文:

For some context, this references the 2016 version of Excel. This is the only sub in the module.

Hello, I've been having a problem where running VBA script that used to work (for the same version) that now causes Excel to crash upon running. An interesting twist is that the same macro function works on the same workbook for other users but not for me. After some debugging, I identified which line was causing the crash and fixed the problem, but I am still wondering why the problem happened in the first place.

Sub graphing_name()
For each ws In ActiveWorkbook.Worksheets
   With ws
      for n = 1 To 10
         If .ChartObjects("Chart " & n).Chart.SeriesCollection.Count > 0 Then
            For s = 1 To .ChartObjects("Chart " & n).Chart.SeriesCollection.Count
               With .ChartObjects("Chart " & n).Chart.SeriesCollection(s)
                   .Format.Line.Weight = 2.25
               End With
            Next s
         End If
      Next n
   End With
Next ws
End Sub

When n = 2, the code was crashing on the .Format line. In the debugger, the value for s when this happened was 12 even though .ChartObjects("Chart " & n).Chart.SeriesCollection.Count = 11. Presumably, the crash was happening because Excel was trying to format a series line that doesn't exist.

Adding s = 1 in the line before the for statement solved the problem, but I have no idea why s was not being reset to 1 at the top of the For loop.

Could anyone explain why this happened and how I might be able to prevent this in the future?

答案1

得分: 0

声明所有变量,这些变量都是局部变量。尝试使用ws.ChartObjects("Chart " & n).Chart一次。我曾遇到过类似情况,其中"Chart" & n 并不总是产生相同的结果,但将n转换为字符串也会添加一些额外的空格,以至于找不到对象...

Sub graphing_name()
   Dim ws As Worksheet, n As Long, s As Long
   For Each ws In ThisWorkbook.Worksheets
      For n = 1 To 10
         ' 使用Trim来确保安全性
         With ws.ChartObjects("Chart " & Trim(n)).Chart
            If .SeriesCollection.Count > 0 Then
               For s = 1 To .SeriesCollection.Count
                  .SeriesCollection(s).Format.Line.Weight = 2.25
               Next
            End If
         End With
      Next
   Next ws
End Sub

你必须确保ChartObject的名称正好是"Chart 2"。或者你可以使用错误检查...

Sub graphing_name()
   Dim ws As Worksheet, n As Long, s As Long, cho As Object
   On Error Resume Next
   For Each ws In ThisWorkbook.Worksheets
      For n = 1 To 10
         Set cho = Nothing
         ' 使用Trim来确保安全性
         Set cho = ws.ChartObjects("Chart " & Trim(n))
         If cho Is Nothing Then
            MsgBox ("无法找到 " & """Chart " & Trim(n) & """")
         Else
            With cho.Chart
               If .SeriesCollection.Count > 0 Then
                  For s = 1 To .SeriesCollection.Count
                     .SeriesCollection(s).Format.Line.Weight = 2.25
                  Next
               End If
            End With
         End If
      Next
   Next ws
   On Error GoTo 0
End Sub
英文:

Declare all variables, in your case they are all local variables. Try using ws.ChartObjects("Chart " & n).Chart once. I've run into a similar case where "Chart" & n didn't always produce the same result but converting n to string also put some extra space so it didn't find the object...

Sub graphing_name()
   Dim ws As Worksheet, n As Long, s As Long
   For Each ws In ThisWorkbook.Worksheets
      For n = 1 To 10
         ' use Trim for safety 
         With ws.ChartObjects("Chart " & Trim(n)).Chart
            If .SeriesCollection.Count > 0 Then
               For s = 1 To .SeriesCollection.Count
                  .SeriesCollection(s).Format.Line.Weight = 2.25
               Next
            End If
         End With
      Next
   Next ws
End Sub

You must be sure that the name of the ChartObject is exactly "Chart 2".
Or you can use error checking...

Sub graphing_name()
   Dim ws As Worksheet, n As Long, s As Long, cho As Object
   On Error Resume Next
   For Each ws In ThisWorkbook.Worksheets
      For n = 1 To 10
         Set cho = Nothing
         ' use Trim for safety
         Set cho = ws.ChartObjects("Chart " & Trim(n))
         If cho Is Nothing Then
            MsgBox ("Cannot find " & """Chart " & Trim(n) & """")
         Else
            With cho.Chart
               If .SeriesCollection.Count > 0 Then
                  For s = 1 To .SeriesCollection.Count
                     .SeriesCollection(s).Format.Line.Weight = 2.25
                  Next
               End If
            End With
         End If
      Next
   Next ws
   On Error GoTo 0
End Sub

huangapple
  • 本文由 发表于 2023年6月29日 01:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定