【问题标题】:How to use a complicated For...Each... loop (or a better method)?如何使用复杂的 For...Each... 循环(或更好的方法)?
【发布时间】:2014-04-25 23:23:48
【问题描述】:

我找到了一个可以解决我的问题的想法,即 For...Each... 循环。但是,它比典型的循环要复杂一些。

我有三个模块。

  1. 使用后缀 i 连接原始文件名,从“-001”开始,每个循环加一。

  2. 连接刚刚创建的新文件路径,添加后缀 i,从“-001”开始,每个循环加一。

  3. 使用该程序将旧文件替换为名为 Autodesk Inventor 的程序中的新文件。

问题是我需要第三个模块来替换组件,然后告诉模块一和模块二移动到下一个 i。我认为 for....each... 循环可能能够做到这一点,但我不确定如何使其工作,因为它将 i 从其他两个模块而不是它自己的模块中引用。有人有什么想法吗?

我可以尝试从我的三个模块中发布我的代码,但由于某种原因,格式现在不听我的。

无论如何,有人要求我发布它。希望它会重新格式化自己。

模块 1:

    Option Explicit

    Public Sub OldNameiLoop()

    Dim i As Double
    Dim NameStr2 As String
    Dim OldNamePath As String

    NameStr2 = Renamer.Old_Name_Display.Text
    OldNamePath = NameStr & "-" & Right("00" & i, 3) & ".ipt"

    Do While i < 99
    i = i + 1
    If 'Something Happens Here' Then
    '3-character string created by using the Right() function
    Next i
    Else: Exit Sub
    End If

    Loop
    End Sub



模块 2:

    Option Explicit

    Public Function NewNameiLoop()

    Dim i As Double
    Dim NameStr As String
    Dim NewNamePath As String


    NameStr = Renamer.New_Name.Text
    NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"

    Do While i < 99                               'Counts with the file name up to -099
    i = i + 1
        If 'Something happens here' Then
    Loop

        Else: Exit Function

        End If

    End Function



模块 3:

    Option Explicit

    Public Function ReplaceComponent()

    Dim oOccurrence As ComponentOccurrence
    Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
    oOccurrence.Replace NewNamePath, True

    End Function

这里有更多信息: Inventor Forum




我已将它们全部合并为:

Option Explicit
Public i As Integer


Public Function ReplaceComponent()


Dim NameStr As String
Dim NewNamePath As String

Dim NameStr2 As String
Dim OldNamePath As String


NameStr = Renamer.New_Name.Text
NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"


NameStr2 = Renamer.Old_Name_Display.Text
OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"



Dim oOccurrence As ComponentOccurrence
Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
oOccurrence.Replace NewNamePath, True

Do While i < 99
i = i + 1

Loop

End Function

但它现在卡住了错误 91。是我错误地进行了更改还是这是一个全新的问题?这是错误行..

Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath

编辑 2(来自 Inventor 自定义论坛):

Sub ReplaceComponent()
   Dim NameStr As String
   Dim NewNamePath As String

   Dim NameStr2 As String
   Dim OldNamePath As String

   For i = 0 To 99 Step 1
      NameStr = Renamer.New_Name.Text
      NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"

  NameStr2 = Renamer.Old_Name_Display.Text
  OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"

  Dim oOccurrence As ComponentOccurrence 
  For Each oOcc As ComponentOccurrence in ThisApplication.ActiveDocument.ComponentDefinition.Occurrences 
     If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then 
        Set oOccurrence = oOcc 
        Exit For
     End If 
  Next oOcc

  'Then you can replace
  oOccurrence.Replace NewNamePath, True
   Next i
End Sub

这仍然无法正常工作。我收到“预期:在”错误,但它越来越近了!

Link to Forum

【问题讨论】:

  • 原来是我的屏幕对比度。今天早上我换了。这里是! :)
  • 我一直在试验,所以看起来有点傻。但基本上,这用于显示文件路径的 MsgBox,因此我可以确保它正确连接名称。

标签: vba loops file-io foreach


【解决方案1】:

似乎简单的答案是声明一个公共变量i。

Public i As Integer

在您的模块 3,函数 ReplaceComponent 中,您可以在函数末尾设置 i=i+1,并在 Sub OldNameiLoop 和 Function NewNameiLoop 中进一步使用此变量。

确保从 Sub OldNameiLoop 和 Function NewNameiLoop 中删除 Dim i As Double

我相信不需要使用 for-each 循环。


编辑:更详细我会建议这样的东西来改进您的最新版本:

Option Explicit



Public Function ReplaceComponent()

Dim i as integer 'no need to declare public if you put everything in one function
Dim NameStr As String
Dim NewNamePath As String

Dim NameStr2 As String
Dim OldNamePath As String


NameStr = Renamer.New_Name.Text
NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"


NameStr2 = Renamer.Old_Name_Display.Text
OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt" 'not sure if correct, I think you need to add Renamer.Path_Text.text here just like for your NewNamePath above.



Dim oOccurrence As ComponentOccurrence
Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath 
'Not sure why you get the error, but maybe because of what you I commented above for   
'OldNamePath. Otherwise post the error here as well, including the contents of OldNamePath 
'at the moment of the error.
oOccurrence.Replace NewNamePath, True

Do While i < 99 'This entire do while loop does nothing in your function except for adding
i = i + 1       'up i untill it is 99. Then it just exits your function. If you want to
                'repeat the entire process 99 times, you want to put this first line right below
Loop            'the last Dim-line

End Function

【讨论】:

  • 解决了在多个模块中使用i的问题,太好了!!!我仍然需要让我的 newnameiloop 和 oldnameiloop 来听 i + 1 ......关于如何做到这一点的任何想法?我只使用 VBA 几个星期。抱歉新手问题!
  • 你不需要让你的 newnameiloop 和 oldnameiloop 听任何东西。 i=i+1 在 ReplaceComponent 函数的末尾将您的公共变量增加 1。然后在 newnameiloop 和 oldnameiloop 中使用该公共变量,因此已经使用增加的 i (i+1)。试试看,如果它仍然不起作用,请告诉我。
  • 我尝试将它们全部合并到一个模块中,因为它一直丢失“NewNamePath”。我在上面写了我的更改以及它引起的问题。 ://
  • 编辑了我的答案以响应您的新代码。阅读我的 cmets 并尝试按照建议改进您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2014-10-14
  • 2022-08-04
  • 1970-01-01
相关资源
最近更新 更多