1

我需要一些有关 Excel VBA 的帮助。我正在从 Excel 和另一个应用程序来回切换,然后从另一个应用程序复制、粘贴到 Excel 中。我已经完成了该过程,但是我需要有关如何粘贴到当前处于活动状态的任何单元格中的建议,右选项卡,然后在该行的末尾向下一行,然后从 column 重新开始D。实际上,这是我需要在 Excel 应用程序中执行的确切过程的列表:

  1. [数字格式] 粘贴到当前活动单元格中(将始终在D:D列中)
  2. Tab 右移一个单元格
  3. [日期格式:“d-mmm”] 今天的日期
  4. 右标签
  5. [文字] 粘贴
  6. 右标签
  7. [会计] 粘贴
  8. 右标签
  9. 在该列中键入字母“X”
  10. 向下输入一行,从该D列开始。

在所有这些步骤之间,我找出了与其他应用程序交互的大部分代码。但我对此也有一个问题——在该应用程序中,我正在运行以下语句:

With ATC.ActiveSession(ATC 只是引用应用程序的类型库来与其他应用程序交互)

与我With每次应用程序来回切换复制和粘贴时都结束语句相反,我需要做什么作为with使用 excel 库的语句?

我不想发生的事情的例子:

Sub New_ATS()

    Set ATC = GetObject(, "ATWin32.AccuTerm")

    AppActivate "AccuTerm 2K2"
    With ATC.ActiveSession
        .InputMode = 1
        .SetSelection 6, 15, 12, 15
        .Copy
        .InputMode = 0
    End With
    AppActivate "Microsoft Excel"
        Selection.Paste '(not complete, part of question)
        Selection.Offset 1, 0 'again, part of the question
    AppActivate "AccuTerm 2K2"
    With ATC.ActiveSession
        .InputMode = 1
        .SetSelection 14, 3, 20, 3
        .Copy
        .InputMode = 0
    End With
    AppActivate "Microsoft Excel"
    ' .... end of partial code (continues on)
End Sub

但相反,我想“链接”这些With语句,但我不知道我会用什么语句来指向 Excel 的 VBA。这就是我想要的:

Sub New_ATS()

    Set ATC = GetObject(, "ATWin32.AccuTerm")

    AppActivate "AccuTerm 2K2"
    With ATC.ActiveSession
        .InputMode = 1
        .SetSelection 6, 15, 12, 15
        .Copy
        .InputMode = 0
        With Excels_Statement '?????
            AppActivate "Microsoft Excel"
            Selection.Paste '(not complete, part of question)
            Selection.Offset 1, 0 'again, part of the question
            AppActivate "AccuTerm 2K2"
            With ATC.ActiveSession
                .InputMode = 1
                .SetSelection 14, 3, 20, 3
                .Copy
                .InputMode = 0
                With Excels_Statement '????
                    AppActivate "Microsoft Excel"
                End With
            End With
        End With
    End With

    ' .... end of partial code (continues on)
End Sub
4

1 回答 1

1

我没有安装 AccuTerm,但我认为您可以粘贴到 Excel 中,而无需每次都激活它。您可以使用最少的类型分配一个对象变量,而不是使用 With 块……这不是变量命名的最佳实践,但它可以解决问题。声明特定类型的变量将使您能够访问 Excel 的库。

这就是我的想法......(部分测试,所以你可能需要稍微调整一下)

Sub New_ATS()

    Set ATC = GetObject(, "ATWin32.AccuTerm")
    Dim Sesh as ATWin32.AccuTerm.Session  'Not sure this exists
    Dim XL as Excel.Range

    AppActivate "AccuTerm 2K2"
    Set Sesh = ATC.ActiveSession

    Sesh.InputMode = 1
    Sesh.SetSelection 6, 15, 12, 15
    Sesh.Copy
    Sesh.InputMode = 0

    'AppActivate "Microsoft Excel" - don't need it
    Set XL = application.activecell
    XL.PasteSpecial
    Set XL = XL.offset(0,1)

    'AppActivate "AccuTerm 2K2" - no need, still active
    Sesh.InputMode = 1               'Once this is set, do you need to set it again?
    Sesh.SetSelection 14, 3, 20, 3
    Sesh.Copy
    Sesh.InputMode = 0               'Once this is set, do you need to set it again?

    XL.PasteSpecial
    XL.NumberFormat = "General"      'Bullet #1
    Set XL = XL.offset(1,0)

    '...and so on...

    XL.PasteSpecial
    XL.NumberFormat = "d-mmm"        'Bullet #3
    Set XL = XL.offset(1,0)

    XL.PasteSpecial
    XL.NumberFormat = "@"            'Bullet #5
    Set XL = XL.offset(1,0)

    XL.PasteSpecial
    XL.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)"      'Bullet #7
    Set XL = XL.offset(1,0)

    XL = "X"                          'Bullet #9

    'When you've reached the end of the row
    Set XL = XL.offset(1, 4 - XL.Column)  'Since col D = 4
    'And repeat your procedure

End Sub
于 2016-07-15T02:27:58.380 回答