【问题标题】:Passing an integer argument in VBA在 VBA 中传递整数参数
【发布时间】:2014-12-17 16:32:15
【问题描述】:

我想要做的是在自动更新和更新之间传递一个整数参数,以便每次 For RowNumber 增加一个,它将该值存储在自动更新中,然后关闭并重新打开更新,然后继续计算它所在的行号离开了。这就是我到目前为止所拥有的。如何让 Panel.xls 打开和关闭?

    Public RowNumber As Integer
Public LoopCount As Integer
    Sub auto_open()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.PrintCommunication = False
    Dim PanelFilePath As String
    Dim PanelFileName As String
    Dim PanelLocation As String
    Dim PanelWB As Workbook
        PanelFilePath = "D:\umc\UMC Production Files\Automation Files\"
        PanelFileName = "Panel.xls"
        PanelLocation = PanelFilePath & Dir$(PanelFilePath & PanelFileName)
        RowNumber = 0
        For LoopCount = 0 To 7
        If LoopCount < 7 Then
         Set PanelWB = Workbooks.Open(Filename:=PanelLocation, UpdateLinks:=3)
             PanelWB.RunAutoMacros Which:=xlAutoOpen
             Application.Run "Panel.xls!Update"
             PanelWB.Close
        End If
         Next LoopCount
        Call Shell("D:\umc\UMC Production Files\Automation Files\Auto.bat", vbNormalFocus)
    Application.Quit
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.PrintCommunication = True
    End Sub

    Function Update(LoopCount As Integer)
    getRowNumber = LoopCount
    End Function

Panel.xls!更新

 Sub Update()
Dim AutoUpdateTargetFile As String
Dim AutoUpdateWB As Workbook
For RowNumber = 1 To (Range("AutoUpdate.File").Rows.Count - 1)
    If (Range("AutoUpdate.File").Rows(RowNumber) <> "") Then
        AutoUpdateTargetFile = Range("Sys.Path") & Range("Client.Path").Rows(RowNumber) & Range("AutoUpdate.Path ").Rows(RowNumber) & Range("AutoUpdate.File").Rows(RowNumber)
        Set AutoUpdateWB = Workbooks.Open(Filename:=AutoUpdateTargetFile, UpdateLinks:=3)
            AutoUpdateWB.RunAutoMacros Which:=xlAutoOpen
            Application.Run "Auto_Update.xls!Flat"
            AutoUpdateWB.Close
    End If
    Next RowNumber
End Sub

【问题讨论】:

  • 您考虑过使用Global 变量吗?
  • 所以我会让 RowNumber 成为全局变量?
  • 对不起,我的意思是 Public 变量。您可以将 RowNumber 设为 Public 变量,然后在其他工作簿中引用它。请参阅this 示例。
  • 谢谢!现在我只需要弄清楚如何设置该功能。 =)
  • 这个问题有点误导,因为核心问题不是传递整数参数,而是正确使用Public var。最好的问候,

标签: vba excel excel-2010 excel-2013


【解决方案1】:

1)。您已声明 Public var:

Public RowNumber As Integer

所以删除 Sub 中相同 var 的本地声明

Dim RowNumber As Integer 'remove

2)。关于“如何传递参数”的第二个问题,请参阅以下示例,演示将参数传递给 Sub ByValByRef 的两个选项:

 Sub Example(ByVal Num1 As Integer, ByRef Num2 As Integer)
 'code
 End Sub

希望这会有所帮助。最好的问候,

【讨论】:

  • 解决了这个问题,但我的问题仍然是如何传递参数。
猜你喜欢
  • 2013-10-15
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 2013-08-09
  • 2013-08-09
  • 2014-11-21
  • 2021-02-06
  • 1970-01-01
相关资源
最近更新 更多